diff --git a/requirements.txt b/requirements.txt index 800c8e4..40281dc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ simplemediawiki irc python-daemon kitchen +python-twitter diff --git a/statusbot/bot.py b/statusbot/bot.py index 1e4eb3a..80a314f 100644 --- a/statusbot/bot.py +++ b/statusbot/bot.py @@ -30,6 +30,12 @@ user=StatusBot password=password url=https://wiki.example.com/w/api.php pageid=1781 + +[twitter] +consumer_key=consumer_key +consumer_secret=consumer_secret +access_token_key=access_token +access_token_secret=access_token_secret """ import argparse @@ -45,6 +51,8 @@ import simplemediawiki import datetime import re import ssl +import textwrap +import twitter try: import daemon.pidlockfile @@ -77,6 +85,37 @@ class UpdateInterface(object): pass +class Tweet(UpdateInterface): + + def __init__(self, config): + self.consumer_key = config.get('twitter', 'consumer_key') + self.consumer_secret = config.get('twitter', 'consumer_secret') + self.access_token_key = config.get('twitter', 'access_token_key') + self.access_token_secret = config.get('twitter', 'access_token_secret') + self.api = twitter.Api( + consumer_key=self.consumer_key, + consumer_secret=self.consumer_secret, + access_token_key=self.access_token, + access_token_secret=self.access_token_secret) + + def update(self, msg): + # Limit tweets to 120 characters to facilitate retweets + for tweet in textwrap.wrap(msg, 120): + self.api.PostUpdate(tweet) + + def alert(self, msg=None): + self.update(msg) + + def notice(self, msg=None): + self.update(msg) + + def log(self, msg=None): + pass + + def ok(self, msg=None): + self.update("Everything back to normal") + + class StatusPage(UpdateInterface): alert_re = re.compile(r'{{CI Alert\|(.*?)}}') item_re = re.compile(r'^\* (.*)$') @@ -346,6 +385,8 @@ def _main(configpath): config.get('ircbot', 'nicks').split(',')] publishers = [StatusPage(config), AlertFile(config)] + if config.has_section('twitter'): + publishers.append(Tweet(config)) bot = StatusBot(channels, nicks, publishers, config.get('ircbot', 'nick'),