From 59b4bcd54872b08c628bc59fe39009ae54482d6f Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Mon, 20 Mar 2017 21:40:02 +0100 Subject: [PATCH] Add follow and unfollow methods, fix reply method --- strings.py | 2 ++ telegrambot.py | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/strings.py b/strings.py index 2c81238..d987468 100644 --- a/strings.py +++ b/strings.py @@ -27,6 +27,7 @@ After authentication, you will be able to tweet from your account. Just drop me Additionally, you will be able to use the following commands: +* /follow USER - Follow a user. * /like TWEET - Like a tweet. You will need to pass a tweet ID (TWEET) as returned by /timeline. * /reply TWEET TEXT - Reply TEXT to a tweet. You will need to pass a tweet ID (TWEET) as returned by /timeline. * /retweet TWEET - Retweet a tweet. You will need to pass a tweet ID (TWEET) as returned by /timeline. @@ -34,6 +35,7 @@ Additionally, you will be able to use the following commands: * /toggletweet - Turn automatic tweeting of messages on/off. Useful in groups. * /tweet TEXT - Explicitly tweet TEXT even if automatic tweeting is off (/toggletweet). * /unauth - Revoke your authenticaton and stop using %s. +* /unfollow USER - Unfollow a user. Have fun!''' diff --git a/telegrambot.py b/telegrambot.py index 1c73b6d..a89f0b4 100755 --- a/telegrambot.py +++ b/telegrambot.py @@ -20,6 +20,7 @@ def twoExceptions(e, message): 131: strings.twitterError, 161: strings.followLimit, 185: strings.tweetLimit, + 186: strings.tweetLimit, 187: strings.dupTweet, 205: strings.rateLimit, 226: strings.automatedTweet, @@ -86,6 +87,22 @@ def fish(bot, update): dbtools.dbHelper().addFish(update.message.chat_id) update.message.reply_text(strings.fishThanks) +def follow(bot, update, args): + try: + two = getTwo(update.message) + for user in args: + two.api.create_friendship(screen_name = user) + except tweepy.error.TweepError as e: + twoExceptions(e, update.message) + +def unfollow(bot, update, args): + try: + two = getTwo(update.message) + for user in args: + two.api.destroy_friendship(screen_name = user) + except tweepy.error.TweepError as e: + twoExceptions(e, update.message) + def getTweetID(tlid, cid): try: db = dbtools.dbHelper() @@ -125,12 +142,11 @@ def explicitTweet(bot, update, args, reply = None): else: out = mobj - - two.api.update_with_media(filename, update.message.caption, file=out) + two.api.update_with_media(filename, update.message.caption, reply, file=out) out.close() else: - two.tweet(' '.join(args)) + two.tweet(' '.join(args), reply) except tweepy.error.TweepError as e: logging.exception("Meh.") @@ -215,6 +231,7 @@ if __name__ == "__main__": updater.dispatcher.add_handler(telegram.ext.CommandHandler("auth", auth)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("fish", fish)) + updater.dispatcher.add_handler(telegram.ext.CommandHandler("follow", follow, pass_args=True)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("help", start)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("like", like, pass_args=True)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("reply", reply, pass_args=True)) @@ -225,6 +242,7 @@ if __name__ == "__main__": updater.dispatcher.add_handler(telegram.ext.CommandHandler("toggletweet", toggleTweet)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("tweet", explicitTweet, pass_args=True)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("unauth", unauth)) + updater.dispatcher.add_handler(telegram.ext.CommandHandler("unfollow", unfollow, pass_args=True)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("verify", verify, pass_args=True)) updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.text, tweet))