Add function to actually fetch users subscribed to broadcasts, IMPROVE REPLIES!!!

This commit is contained in:
Klaus-Uwe Mitterer 2017-03-23 20:23:31 +01:00
parent 6aba2e4396
commit dbd7e794d2
3 changed files with 51 additions and 8 deletions

View File

@ -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)

View File

@ -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():

View File

@ -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: