diff --git a/bottools/methods.py b/bottools/methods.py index 2d27e43..84f32ce 100644 --- a/bottools/methods.py +++ b/bottools/methods.py @@ -1,4 +1,4 @@ -import ast, dbtools, html, io, logging, moviepy.editor, os, PIL.Image, random, setuptools, string, bottools.strings, sys, telegram.ext, telegram, time, twitools, twitools.streaming, urllib.request, tweepy +import ast, dbtools, html, io, logging, moviepy.editor, os, PIL.Image, random, re, setuptools, string, bottools.strings, sys, telegram.ext, telegram, time, twitools, twitools.streaming, urllib.request, tweepy def getTwo(message): try: @@ -161,10 +161,39 @@ def explicitTweet(bot, update, args, reply = None): def reply(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 + + if not ("@%s" % sender.strip("@")) in [("@%s" % a.strip("@")) for a in args]: + mentions = [] + + for m in re.split('[^\w@]+', otweet.text): + try: + if m[0] == "@": + mentions += [m] + except: + pass + + for m in mentions: + if m in args + [("@%s" % two.whoami().strip("@"))] + [("@%s" % sender.strip("@"))]: + mentions.remove(m) + + else: + mentions = [] + + if "@%s" % sender.strip("@") != "@%s" % two.whoami().strip("@"): + first = ["@%s" % sender.strip("@")] + else: + first = [""] + + pargs = first + args[1:] + mentions + except: update.message.reply_text(bottools.strings.cantfind % args[0]) + raise - bottools.methods.explicitTweet(bot, update, args[1:], reply) + bottools.methods.explicitTweet(bot, update, pargs, reply) def retweet(bot, update, args): for tweet in args: @@ -276,13 +305,19 @@ def restart(bot, update): def broadcast(bot, update, args): if bottools.methods.isadmin(update.message): for u in dbtools.dbHelper().broadcastUsers(): - bot.sendMessage(chat_id = u, text = ' '.join(args)) + try: + bot.sendMessage(chat_id = u, text = ' '.join(args)) + except: + logging.exception("Could not send broadcast.") else: bottools.methods.unknown(bot, update) def emergency(bot, update, args): if bottools.methods.isadmin(update.message): for u in dbtools.dbHelper().allUsers(): - bot.sendMessage(chat_id = u, text = ' '.join(args)) + try: + bot.sendMessage(chat_id = u, text = ' '.join(args)) + except: + logging.exception("Could not send emergency broadcast.") else: bottools.methods.unknown(bot, update) diff --git a/dbtools/__init__.py b/dbtools/__init__.py index e87c2a0..a6eb58b 100644 --- a/dbtools/__init__.py +++ b/dbtools/__init__.py @@ -177,6 +177,11 @@ class dbObject: for u in self.getAll(): yield u[0] + def broadcastUsers(self): + self.executeQuery("SELECT cid FROM tokens WHERE broadcast;") + for u in self.getAll(): + yield u[0] + def allUsers(self): self.executeQuery("SELECT cid FROM tokens;") for u in self.getAll(): diff --git a/twitools/__init__.py b/twitools/__init__.py index 95235f8..58d31f5 100644 --- a/twitools/__init__.py +++ b/twitools/__init__.py @@ -56,10 +56,13 @@ def getNameByID(uid, section = setuptools.TWITTER): return twoHelper(section).api.get_user(uid).screen_name def getNamesByIDs(fids=getFollowerIDs(), section = setuptools.TWITTER): - for page in setuptools.paginate(fids, 100): - followers = twoHelper(section).api.lookup_users(user_ids=page) - for follower in followers: - yield {"id": follower.id, "name": follower.screen_name} + for page in setuptools.paginate(fids, 100): + followers = twoHelper(section).api.lookup_users(user_ids=page) + for follower in followers: + yield {"id": follower.id, "name": follower.screen_name} + +def getTweet(tid, section = setuptools.TWITTER): + return twoHelper(section).api.get_status(tid) def twoHelper(section = setuptools.TWITTER): try: