welcome/welcome.py

92 líneas
2.4 KiB
Python

2021-05-18 19:11:26 +02:00
from mastodon import Mastodon
import re
2021-05-18 19:11:26 +02:00
import os
import time
2021-05-18 19:11:26 +02:00
import sys
import os.path
def get_data(notification):
2021-05-19 14:43:27 +02:00
notification_id = notification.id
2021-05-19 14:43:27 +02:00
if notification.type != 'admin.sign_up':
2021-05-19 14:43:27 +02:00
print(f'dismissing notification {notification_id}')
2021-05-19 14:43:27 +02:00
mastodon.notifications_dismiss(notification_id)
2021-05-19 14:43:27 +02:00
return
2021-05-19 14:43:27 +02:00
account_id = notification.account.id
2021-05-19 14:43:27 +02:00
username = notification.account.acct
2021-05-19 14:43:27 +02:00
toot_text = f'{mastodon_hostname} dona la benvinguda a:\n\n'
2021-05-18 19:11:26 +02:00
toot_text += f'@{username}!\n\n'
2021-05-21 13:50:16 +02:00
toot_text += "Gràcies per escollir-nos!\n"
2021-05-21 13:50:16 +02:00
toot_text += "Ens agradaria que et presentessis una mica. Gràcies de nou!"
2021-05-21 13:50:16 +02:00
mastodon.status_post(toot_text, in_reply_to_id=None, )
2021-05-21 13:50:16 +02:00
print(f'Replied notification {notification_id}')
2021-05-21 13:50:16 +02:00
mastodon.notifications_dismiss(notification_id)
def log_in():
# Load secrets from secrets file
secrets_filepath = "secrets/secrets.txt"
uc_client_id = get_parameter("uc_client_id", secrets_filepath)
uc_client_secret = get_parameter("uc_client_secret", secrets_filepath)
uc_access_token = get_parameter("uc_access_token", secrets_filepath)
2021-05-21 13:50:16 +02:00
# Load configuration from config file
config_filepath = "config/config.txt"
mastodon_hostname = get_parameter("mastodon_hostname", config_filepath)
2021-05-21 13:50:16 +02:00
# Initialise Mastodon API
mastodon = Mastodon(
client_id = uc_client_id,
client_secret = uc_client_secret,
access_token = uc_access_token,
api_base_url = 'https://' + mastodon_hostname,
)
# Initialise access headers
headers={ 'Authorization': 'Bearer %s'%uc_access_token }
return mastodon, mastodon_hostname
2021-05-21 13:50:16 +02:00
2021-05-18 19:11:26 +02:00
def get_parameter( parameter, file_path ):
2021-05-18 19:11:26 +02:00
# Check if secrets file exists
if not os.path.isfile(file_path):
print("File %s not found, exiting."%file_path)
sys.exit(0)
# Find parameter in file
with open( file_path ) as f:
for line in f:
if line.startswith( parameter ):
return line.replace(parameter + ":", "").strip()
# Cannot find parameter, exit
print(file_path + " Missing parameter %s "%parameter)
sys.exit(0)
2021-05-19 14:43:27 +02:00
###############################################################################
# main
2021-05-18 19:11:26 +02:00
2021-05-19 14:43:27 +02:00
if __name__ == '__main__':
2021-05-18 19:11:26 +02:00
mastodon, mastodon_hostname = log_in()
2021-05-21 13:50:16 +02:00
bot_notifications = mastodon.notifications()
2021-05-21 13:50:16 +02:00
for notif in bot_notifications:
2021-05-21 13:50:16 +02:00
get_data(notif)