Add twitter support to statusbot

Similar to replicating to github for the convenience of our
developer community, sending statuses ALSO to twitter seems
like a potentially low-impact way to communicate status on
an additional channel.

Change-Id: Ib4bdbb335e4bc12d75d5f0ec2a1b95c8a6b2e7d5
This commit is contained in:
Monty Taylor 2014-05-07 12:04:30 -07:00
parent fa09dd310b
commit f0e6e8a2ef
2 changed files with 41 additions and 0 deletions

View File

@ -2,3 +2,4 @@ simplemediawiki
irc irc
python-daemon python-daemon
kitchen kitchen
python-twitter

View File

@ -30,6 +30,12 @@ user=StatusBot
password=password password=password
url=https://wiki.example.com/w/api.php url=https://wiki.example.com/w/api.php
pageid=1781 pageid=1781
[twitter]
consumer_key=consumer_key
consumer_secret=consumer_secret
access_token_key='access_token
access_token_secret='access_token_secret
""" """
import argparse import argparse
@ -45,6 +51,7 @@ import simplemediawiki
import datetime import datetime
import re import re
import ssl import ssl
import twitter
try: try:
import daemon.pidlockfile import daemon.pidlockfile
@ -77,6 +84,37 @@ class UpdateInterface(object):
pass 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 xrange(0, len(msg), 120):
self.api.PostUpdate(msg[tweet:tweet + 120])
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): class StatusPage(UpdateInterface):
alert_re = re.compile(r'{{CI Alert\|(.*?)}}') alert_re = re.compile(r'{{CI Alert\|(.*?)}}')
item_re = re.compile(r'^\* (.*)$') item_re = re.compile(r'^\* (.*)$')
@ -346,6 +384,8 @@ def _main(configpath):
config.get('ircbot', 'nicks').split(',')] config.get('ircbot', 'nicks').split(',')]
publishers = [StatusPage(config), publishers = [StatusPage(config),
AlertFile(config)] AlertFile(config)]
if config.has_section('twitter'):
publishers.append(Tweet(config))
bot = StatusBot(channels, nicks, publishers, bot = StatusBot(channels, nicks, publishers,
config.get('ircbot', 'nick'), config.get('ircbot', 'nick'),