diff --git a/bottools/methods.py b/bottools/methods.py index b654965..8cffde4 100644 --- a/bottools/methods.py +++ b/bottools/methods.py @@ -36,10 +36,14 @@ def twoExceptions(e, message): 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:]) + try: + feature = commands[args[0][1:]] + try: + feature(bot, update, args[1:]) + except: + feature(bot, update) + except: + update.callback_query.reply_text(bottools.strings.unknownCommand) def mentionHelper(bot, update): args = update.message.text.split() @@ -49,9 +53,11 @@ def mentionHelper(bot, update): try: feature(bot, update, args[2:]) except: + print(e) feature(bot, update) - except: - update.message.reply_text(bottools.strings.unknown) + except Exception as e: + logging.exception(e) + update.message.reply_text(bottools.strings.unknownCommand) # Actual methods: # --------------- @@ -222,6 +228,20 @@ def reply(bot, update, args): bottools.methods.explicitTweet(bot, update, pargs, reply) +def quote(bot, update, args): + try: + reply = bottools.methods.getTweetID(args[0], update.message.chat_id) + two = bottools.methods.getTwo(update.message) + otweet = twitools.getTweet(reply) + sender = otweet.user.screen_name + + args += ["https://twitter.com/%s/status/%i" % (sender, int(reply))] + except: + update.message.reply_text(bottools.strings.cantfind % args[0]) + raise + + bottools.methods.explicitTweet(bot, update, args) + def retweet(bot, update, args): message = update.message or update.callback_query.message for tweet in args: @@ -253,27 +273,28 @@ def tweet(bot, update): # Timelines -def tweetMessage(status, message): +def tweetMessage(status, cid, bot): db = dbtools.dbHelper() try: - db.executeQuery("SELECT MAX(nr) FROM timelines WHERE cid = %i;" % int(message.chat_id)) + db.executeQuery("SELECT MAX(nr) FROM timelines WHERE cid = %i;" % int(cid)) 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.executeQuery("INSERT INTO timelines VALUES(%i, %i, %i);" % (cid, 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) + telegram.InlineKeyboardButton("Reply", switch_inline_query_current_chat = "/reply %i " % i), + telegram.InlineKeyboardButton("Quote",switch_inline_query_current_chat = "/quote %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) + bot.sendMessage(chat_id = cid, 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]): @@ -291,7 +312,7 @@ def timeline(bot, update, args = [10]): two = bottools.methods.getTwo(update.message) for status in two.api.home_timeline(count=count): - tweetMessage(status, update.message) + tweetMessage(status, update.message.chat_id, bot) except tweepy.error.TweepError as e: bottools.methods.twoExceptions(e, update.message) @@ -384,6 +405,7 @@ commands = { "login": auth, "logout": unauth, "mentionstream": mentionstream, + "quote": quote, "reply": reply, "restart": restart, "retweet": retweet, @@ -402,6 +424,7 @@ pargs = [ emergency, follow, like, + quote, reply, retweet, timeline, diff --git a/bottools/strings.py b/bottools/strings.py index 8de3074..3210586 100644 --- a/bottools/strings.py +++ b/bottools/strings.py @@ -33,7 +33,8 @@ Additionally, you will be able to use the following commands: *Tweet functions* * /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. +* /quote TWEET - Quote 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. If you do not include any mentions, I will take care of this for you. * /retweet TWEET - Retweet a tweet. You will need to pass a tweet ID (TWEET) as returned by /timeline. * /togglementions - Turn on/off notifications when you are mentioned in a tweet. * /toggletweet - Turn automatic tweeting of messages on/off. Useful in groups. diff --git a/twitools/streaming.py b/twitools/streaming.py index b22324c..a2fbd60 100644 --- a/twitools/streaming.py +++ b/twitools/streaming.py @@ -1,4 +1,4 @@ -import dbtools, html, tweepy +import bottools.methods, dbtools, html, tweepy class BotStreamListener(tweepy.StreamListener): def __init__(self, bot, cid, *args, **kwargs): @@ -7,17 +7,7 @@ class BotStreamListener(tweepy.StreamListener): self.cid = cid def on_status(self, status): - db = dbtools.dbHelper() - try: - db.executeQuery("SELECT MAX(nr) FROM timelines WHERE cid = %i;" % self.cid) - i = int(db.getNext()[0]) + 1 - except: - i = 1 - - db.executeQuery("INSERT INTO timelines VALUES(%i, %i, %i);" % (self.cid, i, status.id)) - db.commit() - - self.bot.sendMessage(chat_id=self.cid, 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))) + bottools.methods.tweetMessage(status, self.cid, self.bot) def on_error(self, status): if status == 420: