diff --git a/configs/plane1.ini.example b/configs/plane1.ini.example index 5c2a8f1..00be1d0 100644 --- a/configs/plane1.ini.example +++ b/configs/plane1.ini.example @@ -51,4 +51,10 @@ ACCESS_TOKEN = ENABLE = FALSE TITLE = Title Of Telegram message ROOM_ID = -100xxxxxxxxxx -BOT_TOKEN = xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \ No newline at end of file +BOT_TOKEN = xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + +[MASTODON] +ENABLE = TRUE +ACCESS_TOKEN = mastodonaccesstoken +APP_URL = mastodonappurl + diff --git a/defMastodon.py b/defMastodon.py new file mode 100644 index 0000000..ecf5ae2 --- /dev/null +++ b/defMastodon.py @@ -0,0 +1,39 @@ +def sendMastodon(photo, message, config): + from mastodon import Mastodon + sent = False + retry_c = 0 + while sent == False: + try: + bot = Mastodon( + access_token=config.get('MASTODON','ACCESS_TOKEN'), + api_base_url=config.get('MASTODON','APP_URL') + ) + mediaid = bot.media_post(photo, mime_type="image/jpeg") + sent = bot.status_post(message,None,mediaid,False, "Public") + except Exception as err: + print('err.args:') + print(err.args) + print(f"Unexpected {err=}, {type(err)=}") + print("\nString err:\n"+str(err)) + if retry_c > 4: + print('Mastodon attempts exceeded. Message not sent.') + break + elif str(err) == 'Unauthorized': + print('Invalid Mastodon bot token, message not sent.') + break + elif str(err) == 'Timed out': + retry_c += 1 + print('Mastodon timeout count: '+str(retry_c)) + pass + elif str(err)[:35] == '[Errno 2] No such file or directory': + print('Mastodon module couldn\'t find an image to send.') + break + elif str(err) == 'Media_caption_too_long': + print('Mastodon image caption lenght exceeds 1024 characters. Message not send.') + break + else: + print('[X] Unknown error. Message not sent.') + break + else: + print("Mastodon message successfully sent.") + return sent diff --git a/planeClass.py b/planeClass.py index 4b24edf..ca9122c 100644 --- a/planeClass.py +++ b/planeClass.py @@ -463,6 +463,11 @@ class Plane: from defTelegram import sendTeleg photo = open(self.map_file_name, "rb") sendTeleg(photo, message, self.config) + #Mastodon + if self.config.has_section('MASTODON') and self.config.getboolean('MASTODON', 'ENABLE'): + from defMastodon import sendMastodon + sendMastodon(self.map_file_name, message, self.config) + #Discord if self.config.getboolean('DISCORD', 'ENABLE'): role_id = self.config.get('DISCORD', 'ROLE_ID') if self.config.has_option('DISCORD', 'ROLE_ID') and self.config.get('DISCORD', 'ROLE_ID').strip() != "" else None @@ -816,6 +821,10 @@ class Plane: if self.config.has_option('META', 'ENABLE') and self.config.getboolean('META', 'ENABLE'): from meta_toolkit import post_to_meta_both post_to_meta_both(self.config.get("META", "FB_PAGE_ID"), self.config.get("META", "IG_USER_ID"), self.map_file_name, message, self.config.get("META", "ACCESS_TOKEN")) + #Mastodon + if self.config.has_section('MASTODON') and self.config.getboolean('MASTODON', 'ENABLE'): + from defMastodon import sendMastodon + sendMastodon(self.map_file_name, message, self.config) self.circle_history['triggered'] = True elif abs(total_change) <= 360 and self.circle_history["triggered"]: print("No Longer Circling, trigger cleared") @@ -880,4 +889,4 @@ class Plane: print(time_since_ra) if time_since_ra.seconds >= 600: print(ra_type) - self.recent_ra_types.pop(ra_type) \ No newline at end of file + self.recent_ra_types.pop(ra_type)