diff --git a/bottools/methods.py b/bottools/methods.py index e3d8321..5a505b9 100644 --- a/bottools/methods.py +++ b/bottools/methods.py @@ -45,8 +45,9 @@ def callback(bot, update): status = feature(bot, update, args[1:]) except: status = feature(bot, update) - except: - update.callback_query.reply_text(bottools.strings.unknownCommand) + except Exception as e: + logging.exception(e) + update.callback_query.message.reply_text(bottools.strings.unknownCommand) return if status: @@ -134,13 +135,17 @@ def storeLocation(bot, update): update.message.reply_text(bottools.strings.storeLocation, reply_markup=telegram.ReplyKeyboardRemove()) def auth(bot, update): + message = update.message or update.callback_query.message db = dbtools.dbHelper() - cid = update.message.chat_id + cid = message.chat_id auth = tweepy.OAuthHandler(setuptools.cke(), setuptools.cse()) - update.message.reply_text(bottools.strings.auth % auth.get_authorization_url()) + url = auth.get_authorization_url() dbtools.dbHelper().storeToken(cid, auth.request_token) + rmo = telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton("Verify", switch_inline_query_current_chat = "/verify")]]) + message.reply_text(bottools.strings.auth % url, reply_markup=rmo) + def verify(bot, update, args): db = dbtools.dbHelper() cid = update.message.chat_id @@ -203,15 +208,19 @@ def accounts(bot, update): buttons = [] - if len(accounts) != 0: - for account in accounts: - buttons += [telegram.InlineKeyboardButton("@%s" % account, callback_data = "/switch %s" % account)] + for account in accounts: + buttons += [telegram.InlineKeyboardButton("@%s" % account, callback_data = "/switch %s" % account)] - rmo = telegram.InlineKeyboardMarkup(bottools.methods.makeMenu(buttons, 1)) - update.message.reply_text(bottools.strings.accounts % getTwo(update.message).whoami(), reply_markup = rmo) + buttons += [telegram.InlineKeyboardButton("Login with Twitter", callback_data = "/login")] - else: - update.message.reply_text(bottools.strings.noauth) + rmo = telegram.InlineKeyboardMarkup(bottools.methods.makeMenu(buttons, 1)) + + try: + name = getTwo(update.message).whoami() + except: + name = bottools.strings.accountNobody + + update.message.reply_text(bottools.strings.accounts % name, reply_markup = rmo) # User methods @@ -342,7 +351,9 @@ def retweet(bot, update, args): except tweepy.error.TweepError as e: bottools.methods.twoExceptions(e, message) - return twitools.getTweet(tid) + if update.callback_query: + time.sleep(0.5) + return two.getTweet(tid) def thread(bot, update, args): message = update.message or update.callback_query.message @@ -384,7 +395,9 @@ def like(bot, update, args): except tweepy.error.TweepError as e: bottools.methods.twoExceptions(e, message) - return twitools.getTweet(tid) + if update.callback_query: + time.sleep(0.5) + return two.getTweet(tid) def unlike(bot, update, args): message = update.message or update.callback_query.message @@ -398,12 +411,16 @@ def unlike(bot, update, args): except tweepy.error.TweepError as e: bottools.methods.twoExceptions(e, message) - return twitools.getTweet(tid) + if update.callback_query: + time.sleep(0.5) + return two.getTweet(tid) def tweet(bot, update): try: if dbtools.dbHelper().getTStatus(update.message.chat_id): bottools.methods.explicitTweet(bot, update, [update.message.text]) + except ValueError: + update.message.reply_text(bottools.strings.noauth) except tweepy.error.TweepError as e: bottools.methods.twoExceptions(e, update.message) @@ -438,11 +455,12 @@ def tweetMessage(status, cid, bot, force = False, callback = None, notified = No buttons = [] header = None - if notified and notified != dbtools.dbHelper().ato(cid): - ase = db.aseByAto(notified) - oac = twitools.twObject(ato=notified, ase=ase).whoami() + if notified: + if notified != db.ato(cid): + ase = db.aseByAto(notified) + oac = twitools.twObject(ato=notified, ase=ase).whoami() - header = [telegram.InlineKeyboardButton(bottools.strings.messageSwitch % oac, callback_data = "/switch %s" % oac)] + header = [telegram.InlineKeyboardButton(bottools.strings.messageSwitch % oac, callback_data = "/switch %s" % oac)] if status.favorited: buttons += [telegram.InlineKeyboardButton("Unlike", callback_data = "/unlike %i" % i)] @@ -456,19 +474,18 @@ def tweetMessage(status, cid, bot, force = False, callback = None, notified = No buttons += [ telegram.InlineKeyboardButton("Reply", switch_inline_query_current_chat = "/reply %i " % i), - telegram.InlineKeyboardButton("Quote",switch_inline_query_current_chat = "/quote %i " % i) + telegram.InlineKeyboardButton("Quote", switch_inline_query_current_chat = "/quote %i " % i) ] if status.in_reply_to_status_id: buttons += [telegram.InlineKeyboardButton("View Thread", callback_data = "/thread %i" % i)] - rmu = telegram.InlineKeyboardMarkup(makeMenu(buttons, header = header)) + rmu = telegram.InlineKeyboardMarkup(makeMenu(buttons, header=header)) if callback: bot.editMessageReplyMarkup(chat_id=callback.message.chat_id, message_id=callback.message.message_id, reply_markup=rmu) else: - - 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) + bot.sendMessage(chat_id = cid or callback.message.chat_id, 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 trends(bot, update, args): try: diff --git a/bottools/strings.py b/bottools/strings.py index afb636b..fc41c3c 100644 --- a/bottools/strings.py +++ b/bottools/strings.py @@ -7,12 +7,7 @@ You will receive a seven-digit PIN. Please send it to me like this: * /verify 1234567''' -authimp = '''You can't currently log in to a new account as you are either already logged in or I'm waiting for your verification code. - -If you wish to log in to a different account or start the login process from the top, please /logout first..''' - - -noauth = '''You are not logged in. Please use /login to sign in with Twitter.''' +noauth = '''You are not logged in. Please use /accounts to sign in with Twitter.''' start = '''Hey there! @@ -154,3 +149,5 @@ You can use the following accounts:''' switch = '''You are now logged in as @%s.''' messageSwitch = '''Switch to @%s''' + +accountNobody = '''... Well, actually nobody.''' diff --git a/twitools/__init__.py b/twitools/__init__.py index 69122d9..160219a 100644 --- a/twitools/__init__.py +++ b/twitools/__init__.py @@ -43,6 +43,9 @@ class twObject: return self.auth.access_token, self.auth.access_token_secret + def getTweet(self, tid): + return self.api.get_status(tid) + def getFollowerIDs(section = setuptools.TWITTER): ''' Returns 5,000 follower IDs at most ''' for id in list(twoHelper(section).api.followers_ids(screen_name=twObject().whoami())):