From 0263b3abda440e983b84beb8003d5adde34895b6 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Wed, 16 Nov 2022 08:10:16 +1100 Subject: [PATCH] Add mastodon support Add very simple mastodon support. Basically send a request with a bearer token. Change-Id: I345dfa5d68a5cbf4035db85be9883592f143dd93 --- statusbot/bot.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/statusbot/bot.py b/statusbot/bot.py index 8e1f88d..7f259d0 100755 --- a/statusbot/bot.py +++ b/statusbot/bot.py @@ -52,6 +52,11 @@ consumer_key=consumer_key consumer_secret=consumer_secret access_token_key=access_token_key access_token_secret=access_token_secret + +[mastodon] +host=https://example.com +token='mastodon_bearer_token' + """ import argparse @@ -294,6 +299,48 @@ class Tweet(UpdateInterface): self.update('\u2705\ufe0f Everything back to normal') +class Toot(UpdateInterface): + log = logging.getLogger("statusbot.toot") + + def __init__(self, config): + self.host = config.get('mastodon', 'host') + self.token = config.get('mastodon', 'token') + + def update(self, msg): + # NOTE(ianw) Toots are 500 characters, which should be enough. + # Figure out chaining some other time + url = '%s/api/v1/statuses' % (self.host) + data = {'status': msg} + + r = requests.post(url, data=data, + headers={ + 'Authorization': 'Bearer %s' % (self.token)}) + # not sure what to check for... + if r.status_code == 200: + json_data = r.json() + self.log('Tooted id: %s' % json_data['id']) + else: + self.log('Toot failed: %s' % r.status_code) + + def alert(self, msg=None): + # warning sign + self.update('\u26A0\ufe0f ' + msg) + + def notice(self, msg=None): + # pushpin (notice board) + self.update('\U0001f4cc ' + msg) + + def log(self, msg=None): + # wood (log) + self.update('\U0001fab5 ' + msg) + + def ok(self, msg=None): + if msg: + self.update('\u2705\ufe0f ' + msg) + else: + self.update('\u2705\ufe0f Everything back to normal') + + class StatusPage(UpdateInterface): alert_re = re.compile(r'{{CI Alert\|(.*?)}}') item_re = re.compile(r'^\* (.*)$') @@ -579,6 +626,8 @@ def _main(configpath): thankslog = ThanksPage(config, backend) if config.has_section('twitter'): publishers.append(Tweet(config)) + if config.has_section('mastodon'): + publishers.append(Toot(config)) if config.has_option('ircbot', 'use_sasl'): use_sasl = config.getboolean('ircbot', 'use_sasl')