From acfb985d87681bba7e5bbe13bdba6fd6d92ea5e8 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Fri, 24 Mar 2017 01:48:40 +0100 Subject: [PATCH] Fix reply button --- bottools/methods.py | 110 ++++++++++++++++++++++++++++++++++++++------ telegrambot.py | 28 +++-------- 2 files changed, 102 insertions(+), 36 deletions(-) diff --git a/bottools/methods.py b/bottools/methods.py index c86c50c..b654965 100644 --- a/bottools/methods.py +++ b/bottools/methods.py @@ -33,6 +33,26 @@ def twoExceptions(e, message): message.reply_text(text or bottools.strings.twoFail) +def callback(bot, update): + args = update.callback_query.data.split() + + if args[0] == "/like": + bottools.methods.like(bot, update, args[1:]) + elif args[0] == "/retweet": + bottools.methods.retweet(bot, update, args[1:]) + +def mentionHelper(bot, update): + args = update.message.text.split() + + try: + feature = commands[args[1][1:]] + try: + feature(bot, update, args[2:]) + except: + feature(bot, update) + except: + update.message.reply_text(bottools.strings.unknown) + # Actual methods: # --------------- @@ -60,7 +80,7 @@ def toggleTweet(bot, update): def unknown(bot, update): update.message.reply_text(bottools.strings.unknownCommand) -def makeMenu(buttons, columns): +def makeMenu(buttons, columns = 2): return [buttons[i:i + columns] for i in range(0, len(buttons), columns)] # Authentication process @@ -203,24 +223,26 @@ def reply(bot, update, args): bottools.methods.explicitTweet(bot, update, pargs, reply) def retweet(bot, update, args): + message = update.message or update.callback_query.message for tweet in args: try: - tid = bottools.methods.getTweetID(tweet, update.message.chat_id) - bottools.methods.getTwo(update.message).api.retweet(tid) + tid = bottools.methods.getTweetID(tweet, message.chat_id) + bottools.methods.getTwo(message).api.retweet(tid) except ValueError: - update.message.reply_text(bottools.strings.cantfind % tweet) + message.reply_text(bottools.strings.cantfind % tweet) except tweepy.error.TweepError as e: - bottools.methods.twoExceptions(e, update.message) + bottools.methods.twoExceptions(e, message) def like(bot, update, args): + message = update.message or update.callback_query.message for tweet in args: try: - tid = bottools.methods.getTweetID(tweet, update.message.chat_id) - bottools.methods.getTwo(update.message).api.create_favorite(tid) + tid = bottools.methods.getTweetID(tweet, message.chat_id) + bottools.methods.getTwo(message).api.create_favorite(tid) except ValueError: - update.message.reply_text(bottools.strings.cantfind % tweet) + message.reply_text(bottools.strings.cantfind % tweet) except tweepy.error.TweepError as e: - bottools.methods.twoExceptions(e, update.message) + bottools.methods.twoExceptions(e, message) def tweet(bot, update): try: @@ -231,6 +253,29 @@ def tweet(bot, update): # Timelines +def tweetMessage(status, message): + db = dbtools.dbHelper() + + try: + db.executeQuery("SELECT MAX(nr) FROM timelines WHERE cid = %i;" % int(message.chat_id)) + i = int(db.getNext()[0]) + 1 + except: + i = 1 + + db.executeQuery("INSERT INTO timelines VALUES(%i, %i, %i);" % (message.chat_id, i, status.id)) + db.commit() + + buttons = [ + telegram.InlineKeyboardButton("Like", callback_data = "/like %i" % i), + telegram.InlineKeyboardButton("Retweet", callback_data = "/retweet %i" % i), + telegram.InlineKeyboardButton("Reply", switch_inline_query_current_chat = "/reply %i " % i) + ] + + rmu = telegram.InlineKeyboardMarkup(makeMenu(buttons)) + + message.reply_text("Tweet %i:\n%s (@%s) at %s:\n%s" % (i, status.author.name, status.author.screen_name, status.created_at, html.unescape(status.text)), reply_markup=rmu) + + def timeline(bot, update, args = [10]): try: count = int(args[0]) @@ -239,21 +284,18 @@ def timeline(bot, update, args = [10]): db = dbtools.dbHelper() db.executeQuery("DELETE FROM timelines WHERE cid = %i;" % int(update.message.chat_id)) + db.commit() try: i = 1 two = bottools.methods.getTwo(update.message) for status in two.api.home_timeline(count=count): - db.executeQuery("INSERT INTO timelines VALUES(%i, %i, %i);" % (update.message.chat_id, i, status.id)) - update.message.reply_text("Tweet %i:\n%s (@%s) at %s:\n%s" % (i, status.author.name, status.author.screen_name, status.created_at, html.unescape(status.text))) - i += 1 + tweetMessage(status, update.message) except tweepy.error.TweepError as e: bottools.methods.twoExceptions(e, update.message) - db.commit() - # Streaming mentionstreams = {} @@ -328,3 +370,43 @@ def emergency(bot, update, args): logging.exception("Could not send emergency broadcast.") else: bottools.methods.unknown(bot, update) + +# Variables + +commands = { + "auth": auth, + "broadcast": broadcast, + "emergency": emergency, + "fish": fish, + "follow": follow, + "help": start, + "like": like, + "login": auth, + "logout": unauth, + "mentionstream": mentionstream, + "reply": reply, + "restart": restart, + "retweet": retweet, + "start": start, + "timeline": timeline, + "togglebroadcasts": togglebroadcasts, + "togglementions": mentionstream, + "tweet": explicitTweet, + "unauth": unauth, + "unfollow": unfollow, + "verify": verify +} + +pargs = [ + broadcast, + emergency, + follow, + like, + reply, + retweet, + timeline, + toggleTweet, + explicitTweet, + unfollow, + verify +] diff --git a/telegrambot.py b/telegrambot.py index 8a5fae4..fefc386 100755 --- a/telegrambot.py +++ b/telegrambot.py @@ -11,28 +11,10 @@ def log(bot, update, error): if __name__ == "__main__": updater = telegram.ext.Updater(token=setuptools.token()) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("auth", bottools.methods.auth)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("broadcast", bottools.methods.broadcast, pass_args=True)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("emergency", bottools.methods.emergency, pass_args=True)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("fish", bottools.methods.fish)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("follow", bottools.methods.follow, pass_args=True)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("help", bottools.methods.start)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("like", bottools.methods.like, pass_args=True)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("login", bottools.methods.auth)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("logout", bottools.methods.unauth)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("mentionstream", bottools.methods.mentionstream)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("reply", bottools.methods.reply, pass_args=True)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("restart", bottools.methods.restart)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("retweet", bottools.methods.retweet, pass_args=True)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("start", bottools.methods.start)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("timeline", bottools.methods.timeline, pass_args=True)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("togglebroadcasts", bottools.methods.togglebroadcasts)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("togglementions", bottools.methods.mentionstream)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("toggletweet", bottools.methods.toggleTweet)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("tweet", bottools.methods.explicitTweet, pass_args=True)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("unauth", bottools.methods.unauth)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("unfollow", bottools.methods.unfollow, pass_args=True)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("verify", bottools.methods.verify, pass_args=True)) + for k, v in bottools.methods.commands.items(): + updater.dispatcher.add_handler(telegram.ext.CommandHandler(k, v, pass_args = True if v in bottools.methods.pargs else False)) + + updater.dispatcher.add_handler(telegram.ext.RegexHandler("^@TweepBot \/.*", bottools.methods.mentionHelper)) updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.text, bottools.methods.tweet)) updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.photo, bottools.methods.tweet)) @@ -41,6 +23,8 @@ if __name__ == "__main__": updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.command, bottools.methods.unknown)) updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.document, bottools.methods.tweet)) + updater.dispatcher.add_handler(telegram.ext.CallbackQueryHandler(bottools.methods.callback)) + updater.dispatcher.add_error_handler(log) updater.start_polling()