From 157f1bf36182aaa6b10795f08abca6b3c4314993 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Tue, 25 Jan 2011 19:36:24 -0600 Subject: [PATCH] Twitter: add command for a twitter-native reply --- modules/Twitter.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/modules/Twitter.py b/modules/Twitter.py index fe43d00..e2fa911 100644 --- a/modules/Twitter.py +++ b/modules/Twitter.py @@ -43,12 +43,14 @@ class Twitter(Module): tweetpattern = '^!twitter\s+tweet\s+(.*)' gettokenpattern = '^!twitter\s+gettoken$' authpattern = '^!twitter\s+auth\s+(\S+)$' + replytopattern = '^!twitter\s+replyto\s+(\S+)\s+(.*)' self.getstatusre = re.compile(getstatuspattern) self.getuserstatusre = re.compile(getuserstatuspattern) self.tweetre = re.compile(tweetpattern) self.gettokenre = re.compile(gettokenpattern) self.authre = re.compile(authpattern) + self.replytore = re.compile(replytopattern) # prep oauth magic self.consumer_key = 'N2aSGxBP8t3cCgWyF1B2Aw' @@ -102,6 +104,8 @@ class Twitter(Module): return self.twitter_getuserstatus(connection, event, nick, userhost, what, admin_unlocked) elif self.tweetre.search(what): 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): return self.twitter_gettoken(connection, event, nick, userhost, what, admin_unlocked) elif self.authre.search(what): @@ -162,6 +166,30 @@ class Twitter(Module): except twitter.TwitterError as 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): """Get an oauth token, so that the user may authenticate the bot."""