Twitter: add command for a twitter-native reply

This commit is contained in:
Brian S. Stephan 2011-01-25 19:36:24 -06:00
parent 1aba7c904d
commit 157f1bf361
1 changed files with 28 additions and 0 deletions

View File

@ -43,12 +43,14 @@ class Twitter(Module):
tweetpattern = '^!twitter\s+tweet\s+(.*)' tweetpattern = '^!twitter\s+tweet\s+(.*)'
gettokenpattern = '^!twitter\s+gettoken$' gettokenpattern = '^!twitter\s+gettoken$'
authpattern = '^!twitter\s+auth\s+(\S+)$' authpattern = '^!twitter\s+auth\s+(\S+)$'
replytopattern = '^!twitter\s+replyto\s+(\S+)\s+(.*)'
self.getstatusre = re.compile(getstatuspattern) self.getstatusre = re.compile(getstatuspattern)
self.getuserstatusre = re.compile(getuserstatuspattern) self.getuserstatusre = re.compile(getuserstatuspattern)
self.tweetre = re.compile(tweetpattern) self.tweetre = re.compile(tweetpattern)
self.gettokenre = re.compile(gettokenpattern) self.gettokenre = re.compile(gettokenpattern)
self.authre = re.compile(authpattern) self.authre = re.compile(authpattern)
self.replytore = re.compile(replytopattern)
# prep oauth magic # prep oauth magic
self.consumer_key = 'N2aSGxBP8t3cCgWyF1B2Aw' self.consumer_key = 'N2aSGxBP8t3cCgWyF1B2Aw'
@ -102,6 +104,8 @@ class Twitter(Module):
return self.twitter_getuserstatus(connection, event, nick, userhost, what, admin_unlocked) return self.twitter_getuserstatus(connection, event, nick, userhost, what, admin_unlocked)
elif self.tweetre.search(what): elif self.tweetre.search(what):
return self.twitter_tweet(connection, event, nick, userhost, what, admin_unlocked) return self.twitter_tweet(connection, event, nick, userhost, what, admin_unlocked)
elif self.replytore.search(what):
return self.twitter_replyto(connection, event, nick, userhost, what, admin_unlocked)
elif self.gettokenre.search(what): elif self.gettokenre.search(what):
return self.twitter_gettoken(connection, event, nick, userhost, what, admin_unlocked) return self.twitter_gettoken(connection, event, nick, userhost, what, admin_unlocked)
elif self.authre.search(what): elif self.authre.search(what):
@ -162,6 +166,30 @@ class Twitter(Module):
except twitter.TwitterError as e: except twitter.TwitterError as e:
return 'Couldn\'t tweet: ' + str(e) return 'Couldn\'t tweet: ' + str(e)
def twitter_replyto(self, connection, event, nick, userhost, what, admin_unlocked):
"""Reply to a tweet, in the twitter in_reply_to_status_id sense. Needs authentication."""
match = self.replytore.search(what)
if match:
status_id = match.group(1)
tweet = match.group(2)
if self.authed is False:
return 'You must be authenticated to tweet.'
if admin_unlocked is False:
return 'Only admins can tweet.'
replyee_tweet = self.twit.GetStatus(status_id)
target = replyee_tweet.user.screen_name.encode('utf-8', 'ignore')
try:
if self.twit.PostUpdate('@'+target+': '+tweet, in_reply_to_status_id=status_id) is not None:
return 'Tweet sent.'
else:
return 'Unknown error sending tweet.'
except twitter.TwitterError as e:
return 'Couldn\'t tweet: ' + str(e)
def twitter_gettoken(self, connection, event, nick, userhost, what, admin_unlocked): def twitter_gettoken(self, connection, event, nick, userhost, what, admin_unlocked):
"""Get an oauth token, so that the user may authenticate the bot.""" """Get an oauth token, so that the user may authenticate the bot."""