114 lines
3.5 KiB
Python
114 lines
3.5 KiB
Python
# MODPOL GOVBOT
|
|
# A Python bridge between Modpol and Mastodon
|
|
|
|
## Initialize Modpol ##
|
|
|
|
import lupa
|
|
|
|
# Create a Lua runtime
|
|
lua = lupa.LuaRuntime(unpack_returned_tuples=True)
|
|
|
|
# Load your Lua script
|
|
script_path = 'login_mastodon.lua'
|
|
with open(script_path, 'r') as f:
|
|
lua.execute(f.read())
|
|
|
|
print("Loaded Modpol into Govbot")
|
|
|
|
# To do: load all override files
|
|
|
|
# If your Lua script defines functions, you can call them from Python
|
|
#result = lua.globals().your_function_name() # Replace with your actual function name
|
|
|
|
#print("Result from Lua:", result)
|
|
|
|
|
|
## Initialize Mastodon ##
|
|
|
|
from mastodon import Mastodon
|
|
# ^ https://mastodonpy.readthedocs.io/en/stable/#
|
|
import time
|
|
import datetime
|
|
# ^ https://github.com/HikaruSama233/mastodon_autoreply_bot_template/blob/main/bot_template.py
|
|
|
|
# Variables
|
|
## Character maximum for the server
|
|
char_max = 500
|
|
|
|
# Connect to Mastodon
|
|
govbot = Mastodon(
|
|
client_id='modpol_mastodon/creds/clientcred.secret',
|
|
access_token = 'modpol_mastodon/creds/usercred.secret')
|
|
|
|
print("Signed in to Mastodon")
|
|
|
|
# Handle notification events
|
|
def handle_event(event):
|
|
print("{} Handling notification ".format(datetime.datetime.now())
|
|
+ str(event['id']))
|
|
if 'mention' in event['type']:
|
|
mention_username = event['account']['username']
|
|
print(str(event['id']) + ": mention from @"
|
|
+ mention_username)
|
|
# Command handling
|
|
# perhaps move this to a separate Interfaces file
|
|
if "!dashboard" in event['status']['content']:
|
|
govbot.status_reply(event['status'],
|
|
"[dashboard here]")
|
|
print("{} Replied to ".format(datetime.datetime.now())
|
|
+ str(event['id']))
|
|
# To do: if DM
|
|
else:
|
|
govbot.status_reply(event['status'],
|
|
"Thanks for mentioning me!")
|
|
print("{} Replied to ".format(datetime.datetime.now())
|
|
+ str(event['id']))
|
|
## ADD OTHER IF STATEMENTS HERE
|
|
else:
|
|
govbot.status_reply(event['status'],
|
|
"I'm not sure how to parse that.")
|
|
print("{} Posted error message ".format(datetime.datetime.now())
|
|
+ str(event['id']))
|
|
|
|
# Check notifications
|
|
events_queue = {}
|
|
def check_events():
|
|
notifications = govbot.notifications(mentions_only=True)
|
|
current_time = datetime.datetime.now()
|
|
if len(notifications) > 0:
|
|
print("{} Total notifications {}".format(current_time, len(notifications)))
|
|
events_queue = notifications
|
|
while len(events_queue) > 0:
|
|
id = events_queue[0]['id']
|
|
handle_event(events_queue.pop(0)) # removes event from queue
|
|
govbot.notifications_dismiss(id)
|
|
elif str(current_time.minute) == '0':
|
|
print("{} No notifications".format(current_time))
|
|
|
|
# Initialization
|
|
|
|
# Delete past posts (while we are testing)
|
|
## Get account information
|
|
account_info = govbot.account_verify_credentials()
|
|
## Extract and print account ID
|
|
acc_id = account_info['id']
|
|
## Fetch the bot's posts
|
|
old_posts = govbot.account_statuses(id=acc_id)
|
|
## Delete
|
|
for post in old_posts:
|
|
govbot.status_delete(id=post.id)
|
|
# Delete past notifications
|
|
govbot.notifications_clear()
|
|
|
|
print("{} Initialized".format(datetime.datetime.now()))
|
|
|
|
first_post = "Commands:"
|
|
first_post += "\n!dashboard: View dashboard"
|
|
govbot.status_post(first_post[:char_max])
|
|
|
|
while 1:
|
|
# Check every 5 seconds
|
|
time.sleep(5)
|
|
if len(events_queue) == 0:
|
|
check_events()
|