You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.4 KiB

2 years ago
from mastodon import Mastodon
import re
2 years ago
import os
import time
2 years ago
import sys
import os.path
def get_data(notification):
2 years ago
notification_id = notification.id
2 years ago
if notification.type != 'admin.sign_up':
2 years ago
print(f'dismissing notification {notification_id}')
2 years ago
mastodon.notifications_dismiss(notification_id)
2 years ago
return
2 years ago
account_id = notification.account.id
2 years ago
username = notification.account.acct
2 years ago
toot_text = f'{mastodon_hostname} dona la benvinguda a:\n\n'
2 years ago
toot_text += f'@{username}!\n\n'
toot_text += "Gràcies per escollir-nos!\n"
toot_text += "Ens agradaria que et presentessis una mica. Gràcies de nou!"
mastodon.status_post(toot_text, in_reply_to_id=None, )
print(f'Replied notification {notification_id}')
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)
# Load configuration from config file
config_filepath = "config/config.txt"
mastodon_hostname = get_parameter("mastodon_hostname", config_filepath)
# 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
2 years ago
def get_parameter( parameter, file_path ):
2 years ago
# 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)
2 years ago
###############################################################################
# main
2 years ago
2 years ago
if __name__ == '__main__':
2 years ago
mastodon, mastodon_hostname = log_in()
bot_notifications = mastodon.notifications()
for notif in bot_notifications:
get_data(notif)