twitools/bottools/methods.py

799 lines
22 KiB
Python
Raw Normal View History

import ast, dbtools, html, io, logging, moviepy.editor, os, PIL.Image, random, re, setuptools, string, bottools.strings, sys, telegram.ext, telegram, time, twitools, bottools.streaming, bottools.thread, urllib.request, tweepy
2017-03-22 00:53:51 +00:00
def getTwo(message):
try:
return twitools.twoBotHelper(message.chat_id)
except ValueError:
message.reply_text(bottools.strings.noauth)
except tweepy.error.TweepError as e:
raise
def twoExceptions(e, message):
text = {
32: bottools.strings.badToken,
36: bottools.strings.selfSpam,
64: bottools.strings.accountSuspended,
88: bottools.strings.rateLimit,
89: bottools.strings.badToken,
99: bottools.strings.badToken,
130: bottools.strings.overload,
131: bottools.strings.twitterError,
161: bottools.strings.followLimit,
185: bottools.strings.tweetLimit,
186: bottools.strings.longTweet,
187: bottools.strings.dupTweet,
205: bottools.strings.rateLimit,
226: bottools.strings.automatedTweet,
271: bottools.strings.selfMute,
272: bottools.strings.notMuted,
323: bottools.strings.multipleGIFs,
326: bottools.strings.accountLocked,
354: bottools.strings.longTweet
2017-03-27 12:04:36 +00:00
}.get(e.api_code, bottools.strings.twoFail)
2017-03-22 00:53:51 +00:00
2017-03-27 12:04:36 +00:00
message.reply_text(text)
2017-03-22 00:53:51 +00:00
2017-03-26 18:30:51 +00:00
def silence(bot, update):
pass
2017-03-28 13:46:14 +00:00
2017-03-24 00:48:40 +00:00
def callback(bot, update):
args = update.callback_query.data.split()
2017-03-24 12:08:47 +00:00
try:
feature = commands[args[0][1:]]
try:
2017-03-26 18:30:51 +00:00
status = feature(bot, update, args[1:])
2017-03-24 12:08:47 +00:00
except:
2017-03-26 18:30:51 +00:00
status = feature(bot, update)
except Exception as e:
update.callback_query.message.reply_text(bottools.strings.unknownCommand)
2017-03-26 18:30:51 +00:00
return
if status:
tweetMessage(status, update.callback_query.message.chat_id, bot, callback = update.callback_query)
2017-03-24 00:48:40 +00:00
2017-03-28 13:46:14 +00:00
def captionHelper(bot, update):
if update.message.caption.startswith("@%s /"):
args = update.message.caption.split()[1:]
else:
args = update.message.caption.split()
2017-04-06 20:48:45 +00:00
try:
if args[0].startswith("/"):
2017-03-28 14:08:06 +00:00
try:
2017-04-06 20:48:45 +00:00
feature = commands[args[0][1:]]
try:
feature(bot, update, args[1:])
except:
feature(bot, update)
2017-03-28 14:08:06 +00:00
except:
2017-04-06 20:48:45 +00:00
update.message.reply_text(bottools.strings.unknownCommand)
else:
raise ValueError()
except:
2017-03-28 14:08:06 +00:00
explicitTweet(bot, update, args)
2017-03-28 13:46:14 +00:00
2017-03-24 00:48:40 +00:00
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)
2017-03-24 12:08:47 +00:00
except Exception as e:
update.message.reply_text(bottools.strings.unknownCommand)
2017-03-24 00:48:40 +00:00
2017-03-22 00:53:51 +00:00
# Actual methods:
# ---------------
def start(bot, update):
update.message.reply_text(bottools.strings.start % {"name": setuptools.botname()})
2017-03-22 00:53:51 +00:00
def fish(bot, update):
dbtools.dbHelper().addFish(update.message.chat_id)
update.message.reply_text(bottools.strings.fishThanks)
def getTweetID(tlid, cid):
try:
db = dbtools.dbHelper()
db.executeQuery("SELECT tid FROM timelines WHERE nr = %i AND cid = %i;" % (int(tlid), int(cid)))
return db.getNext()[0]
except Exception:
raise ValueError("No such tweet in timeline")
def toggleTweet(bot, update):
try:
update.message.reply_text(bottools.strings.toggleTweet % (bottools.strings.toggleTweetOn if dbtools.dbHelper().toggleTweet(update.message.chat_id) else bottools.strings.toggleTweetOff))
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
def toggleConfirmations(bot, update):
try:
update.message.reply_text(bottools.strings.toggleConfirmations % (bottools.strings.toggleTweetOn if dbtools.dbHelper().toggleConfirmations(update.message.chat_id) else bottools.strings.toggleTweetOff))
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
2017-03-22 00:53:51 +00:00
def unknown(bot, update):
update.message.reply_text(bottools.strings.unknownCommand)
2017-03-27 17:08:02 +00:00
def makeMenu(buttons, columns = 2, header = None, footer = None):
menu = [buttons[i:i + columns] for i in range(0, len(buttons), columns)]
if header:
menu.insert(0, header)
if footer:
menu.append(footer)
return menu
2017-03-22 00:53:51 +00:00
# Authentication process
2017-03-26 18:30:51 +00:00
def shareLocation(bot, update):
2017-03-27 12:04:36 +00:00
buttons = [
telegram.KeyboardButton(text = bottools.strings.shareLocationAgree, request_location = True),
]
rmo = telegram.ReplyKeyboardMarkup(makeMenu(buttons, 1))
2017-03-26 18:30:51 +00:00
update.message.reply_text(bottools.strings.shareLocation, reply_markup = rmo)
2017-03-27 12:04:36 +00:00
def unsetLocation(bot, update):
dbtools.dbHelper().storeLocation(update.message.chat_id, "NULL", "NULL")
update.message.reply_text(bottools.strings.unsetLocation)
def noLocation(bot, update):
update.message.reply_text(bottools.strings.noLocation, reply_markup=telegram.ReplyKeyboardRemove())
2017-03-26 18:30:51 +00:00
def storeLocation(bot, update):
cid = update.message.chat_id
lat = update.message.location.latitude
lon = update.message.location.longitude
dbtools.dbHelper().storeLocation(cid, lat, lon)
2017-03-27 12:04:36 +00:00
update.message.reply_text(bottools.strings.storeLocation, reply_markup=telegram.ReplyKeyboardRemove())
2017-03-22 00:53:51 +00:00
def auth(bot, update):
message = update.message or update.callback_query.message
2017-03-22 00:53:51 +00:00
db = dbtools.dbHelper()
cid = message.chat_id
2017-03-22 00:53:51 +00:00
2017-03-27 15:39:52 +00:00
auth = tweepy.OAuthHandler(setuptools.cke(), setuptools.cse())
url = auth.get_authorization_url()
2017-03-27 15:39:52 +00:00
dbtools.dbHelper().storeToken(cid, auth.request_token)
2017-03-22 00:53:51 +00:00
rmo = telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton("Verify", switch_inline_query_current_chat = "/verify")]])
message.reply_text(bottools.strings.auth % url, reply_markup=rmo)
2017-03-22 00:53:51 +00:00
def verify(bot, update, args):
db = dbtools.dbHelper()
cid = update.message.chat_id
2017-03-27 15:39:52 +00:00
try:
2017-03-22 00:53:51 +00:00
auth = tweepy.OAuthHandler(setuptools.cke(), setuptools.cse())
2017-03-27 15:39:52 +00:00
auth.request_token = ast.literal_eval(db.getToken(cid))
2017-03-22 00:53:51 +00:00
try:
auth.get_access_token(args[0])
2017-03-27 15:39:52 +00:00
db.storeUser(cid, auth.access_token, auth.access_token_secret)
2017-03-22 00:53:51 +00:00
update.message.reply_text(bottools.strings.verify)
except Exception as e:
2017-03-27 15:39:52 +00:00
db.deleteToken(update.message.chat_id)
2017-03-22 00:53:51 +00:00
update.message.reply_text(bottools.strings.verifyfail)
2017-03-28 09:07:33 +00:00
found = None
for a in dbtools.dbHelper().accounts(cid):
try:
if twitools.twObject(ato=a[0], ase=a[1]).whoami().strip("@").lower() == args[0].strip("@").lower():
found = a
break
except:
pass
if found:
dbtools.dbHelper().deleteUserByAto(found[0])
2017-03-27 15:39:52 +00:00
except:
2017-03-22 00:53:51 +00:00
update.message.reply_text(bottools.strings.verifyimp)
def unauth(bot, update):
2017-03-27 17:08:02 +00:00
global mentionstreams
ato = dbtools.dbHelper().ato(update.message.chat_id)
if ato in mentionstreams:
mentionstreams.pop(ato).disconnect()
2017-03-22 00:53:51 +00:00
dbtools.dbHelper().deleteUser(update.message.chat_id)
update.message.reply_text(bottools.strings.unauth % setuptools.url())
2017-03-27 15:39:52 +00:00
def switch(bot, update, args):
message = update.message or update.callback_query.message
cid = message.chat_id
found = None
for a in dbtools.dbHelper().accounts(cid):
try:
2017-03-27 17:08:02 +00:00
if twitools.twObject(ato=a[0], ase=a[1]).whoami().strip("@").lower() == args[0].strip("@").lower():
2017-03-27 15:39:52 +00:00
found = a
break
except:
pass
if found:
dbtools.dbHelper().setActive(cid, a[0])
message.reply_text(bottools.strings.switch % bottools.methods.getTwo(message).whoami().strip("@"))
2017-03-28 09:07:33 +00:00
try:
return getTwo(message).getTweet(getTweetID(args[1], cid))
2017-03-28 13:46:14 +00:00
except:
pass
2017-03-27 15:39:52 +00:00
else:
message.reply_text(bottools.strings.noauth)
def accounts(bot, update):
accounts = []
for a in dbtools.dbHelper().accounts(update.message.chat_id):
try:
accounts += [twitools.twObject(ato = a[0], ase = a[1]).whoami().strip("@")]
except:
pass
buttons = []
for account in accounts:
buttons += [telegram.InlineKeyboardButton("@%s" % account, callback_data = "/switch %s" % account)]
2017-03-27 15:39:52 +00:00
buttons += [telegram.InlineKeyboardButton("Login with Twitter", callback_data = "/login")]
2017-03-27 15:39:52 +00:00
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)
2017-03-27 15:39:52 +00:00
2017-03-22 00:53:51 +00:00
# User methods
def follow(bot, update, args):
try:
two = bottools.methods.getTwo(update.message)
for user in args:
two.api.create_friendship(screen_name = user)
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
def unfollow(bot, update, args):
try:
two = bottools.methods.getTwo(update.message)
for user in args:
two.api.destroy_friendship(screen_name = user)
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
# Tweet methods
def explicitTweet(bot, update, args, reply = None):
try:
two = bottools.methods.getTwo(update.message)
if update.message.photo or update.message.document or update.message.video or update.message.sticker:
fid = update.message.document.file_id if update.message.document else update.message.sticker.file_id if update.message.sticker else update.message.video.file_id if update.message.video else update.message.photo[-1].file_id
path = bot.getFile(fid).file_path
media = urllib.request.urlopen(path)
mobj = io.BytesIO(media.read())
filename = path.split("/")[-1]
if filename.split(".")[-1].lower() == "webp":
out = io.BytesIO()
PIL.Image.open(mobj).convert('RGB').save(out, format="JPEG")
filename = "%s.jpg" % filename.split(".")[0]
if update.message.document and filename.split(".")[-1].lower() == "mp4":
temp = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(32))
with open("tmp/%s.%s" % (temp, filename.split(".")[-1]), "wb") as f:
f.write(mobj.getvalue())
moviepy.editor.VideoFileClip("tmp/%s.%s" % (temp, filename.split(".")[-1])).resize(0.3).write_gif("tmp/%s.gif" % temp)
filename = "%s.gif" % temp
out = open("tmp/%s.gif" % temp, "rb")
else:
out = mobj
status = two.api.update_with_media(filename, ' '.join(args), in_reply_to_status_id=reply, file=out)
2017-03-22 00:53:51 +00:00
out.close()
else:
try:
status = two.tweet(' '.join(args), reply)
except Exception as e:
logging.exception(e)
2017-03-26 18:30:51 +00:00
bottools.methods.tweetMessage(status, update.message.chat_id, bot)
2017-03-22 00:53:51 +00:00
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
def getMentions(tweet, two, args = []):
sender = tweet.user.screen_name
2017-04-06 20:43:32 +00:00
if "@%s" % sender.strip("@") in args:
return []
2017-03-22 00:53:51 +00:00
try:
text = tweet.full_text
except:
text = tweet.text
mentions = []
for m in re.split('[^\w@]+', text):
try:
if m[0] == "@" and m[0].strip() != "" and not (m in mentions or m in args or m.strip() == "" or m == "@%s" % two.whoami().strip("@") or m == "@%s" % sender.strip("@")):
mentions += [m]
except:
pass
2017-04-06 20:43:32 +00:00
if not (sender.strip("@") == two.whoami().strip("@") or "@%s" % sender.strip("@") in mentions):
mentions = ["@%s" % sender.strip("@")] + mentions
return mentions
def reply(bot, update, args):
try:
reply = bottools.methods.getTweetID(args[0], update.message.chat_id)
two = bottools.methods.getTwo(update.message)
otweet = two.getTweet(reply)
mentions = getMentions(otweet, two, args)
pargs = mentions + args[1:]
2017-03-22 00:53:51 +00:00
except:
update.message.reply_text(bottools.strings.cantfind % args[0])
raise
2017-03-22 00:53:51 +00:00
bottools.methods.explicitTweet(bot, update, pargs, reply)
2017-03-22 00:53:51 +00:00
2017-03-24 12:08:47 +00:00
def quote(bot, update, args):
try:
reply = bottools.methods.getTweetID(args[0], update.message.chat_id)
two = bottools.methods.getTwo(update.message)
2017-03-28 09:07:33 +00:00
otweet = two.getTweet(reply)
2017-03-24 12:08:47 +00:00
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
2017-03-25 12:06:01 +00:00
bottools.methods.explicitTweet(bot, update, args[1:])
2017-03-24 12:08:47 +00:00
2017-03-22 00:53:51 +00:00
def retweet(bot, update, args):
2017-03-24 00:48:40 +00:00
message = update.message or update.callback_query.message
2017-03-26 18:30:51 +00:00
two = bottools.methods.getTwo(message)
2017-03-22 00:53:51 +00:00
for tweet in args:
try:
2017-03-24 00:48:40 +00:00
tid = bottools.methods.getTweetID(tweet, message.chat_id)
2017-03-26 18:30:51 +00:00
two.api.retweet(tid)
2017-03-22 00:53:51 +00:00
except ValueError:
2017-03-24 00:48:40 +00:00
message.reply_text(bottools.strings.cantfind % tweet)
2017-03-22 00:53:51 +00:00
except tweepy.error.TweepError as e:
2017-03-24 00:48:40 +00:00
bottools.methods.twoExceptions(e, message)
2017-03-22 00:53:51 +00:00
if update.callback_query:
time.sleep(0.5)
return two.getTweet(tid)
2017-03-26 18:30:51 +00:00
2017-03-24 14:34:53 +00:00
def thread(bot, update, args):
message = update.message or update.callback_query.message
tid = bottools.methods.getTweetID(args[0], message.chat_id)
2017-03-28 13:46:14 +00:00
two = getTwo(message)
2017-03-24 14:34:53 +00:00
i = 0
try:
count = int(args[1])
except:
count = 5
2017-03-28 09:07:33 +00:00
tweets = [two.getTweet(tid)]
2017-03-24 14:34:53 +00:00
if tweets[-1].in_reply_to_status_id:
nexttid = tweets[-1].in_reply_to_status_id
while i < count:
2017-03-28 09:07:33 +00:00
tweets += [two.getTweet(nexttid)]
2017-03-24 14:34:53 +00:00
if not tweets[-1].in_reply_to_status_id:
break
nexttid = tweets[-1].in_reply_to_status_id
i += 1
2017-03-27 12:18:36 +00:00
tweets.reverse()
2017-03-24 14:34:53 +00:00
for tweet in tweets:
2017-03-24 16:02:21 +00:00
tweetMessage(tweet, message.chat_id, bot)
2017-03-24 14:34:53 +00:00
2017-03-22 00:53:51 +00:00
def like(bot, update, args):
2017-03-24 00:48:40 +00:00
message = update.message or update.callback_query.message
2017-03-26 18:30:51 +00:00
two = bottools.methods.getTwo(message)
for tweet in args:
try:
tid = bottools.methods.getTweetID(tweet, message.chat_id)
two.api.create_favorite(tid)
except ValueError:
message.reply_text(bottools.strings.cantfind % tweet)
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, message)
if update.callback_query:
time.sleep(0.5)
return two.getTweet(tid)
2017-03-26 18:30:51 +00:00
def unlike(bot, update, args):
message = update.message or update.callback_query.message
two = bottools.methods.getTwo(message)
2017-03-22 00:53:51 +00:00
for tweet in args:
try:
2017-03-24 00:48:40 +00:00
tid = bottools.methods.getTweetID(tweet, message.chat_id)
2017-03-26 18:30:51 +00:00
two.api.destroy_favorite(tid)
2017-03-22 00:53:51 +00:00
except ValueError:
2017-03-24 00:48:40 +00:00
message.reply_text(bottools.strings.cantfind % tweet)
2017-03-22 00:53:51 +00:00
except tweepy.error.TweepError as e:
2017-03-24 00:48:40 +00:00
bottools.methods.twoExceptions(e, message)
2017-03-22 00:53:51 +00:00
if update.callback_query:
time.sleep(0.5)
return two.getTweet(tid)
2017-03-26 18:30:51 +00:00
2017-03-22 00:53:51 +00:00
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)
2017-03-26 18:30:51 +00:00
except tweepy.error.TweepError as e:
2017-03-22 00:53:51 +00:00
bottools.methods.twoExceptions(e, update.message)
# Timelines
2017-03-27 17:08:02 +00:00
def tweetMessage(status, cid, bot, force = False, callback = None, notified = None):
2017-03-24 00:48:40 +00:00
db = dbtools.dbHelper()
try:
text = status.text
except:
text = status.full_text
try:
two = twitools.twoBotHelper(cid)
except tweepy.error.TweepError as e:
logging.exception(e)
2017-03-24 00:48:40 +00:00
if not callback:
if status.user.screen_name.strip("@") == two.whoami().strip("@") and not db.getCStatus(cid):
return
2017-03-24 00:48:40 +00:00
2017-03-26 18:30:51 +00:00
if not callback:
try:
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);" % (cid, i, status.id))
db.commit()
else:
i = int(callback.message.text.split()[1].strip(":"))
2017-03-26 18:30:51 +00:00
buttons = []
2017-03-27 17:08:02 +00:00
header = None
if notified:
if notified != db.ato(cid):
ase = db.aseByAto(notified)
oac = twitools.twObject(ato=notified, ase=ase).whoami()
2017-03-27 17:08:02 +00:00
2017-03-28 09:07:33 +00:00
header = [telegram.InlineKeyboardButton(bottools.strings.messageSwitch % oac, callback_data = "/switch %s %i" % (oac, i))]
2017-03-26 18:30:51 +00:00
if status.favorited:
buttons += [telegram.InlineKeyboardButton("Unlike", callback_data = "/unlike %i" % i)]
else:
buttons += [telegram.InlineKeyboardButton("Like", callback_data = "/like %i" % i)]
if status.retweeted:
buttons += [telegram.InlineKeyboardButton("Retweeted", callback_data = "/silence")]
else:
buttons += [telegram.InlineKeyboardButton("Retweet", callback_data = "/retweet %i" % i)]
buttons += [
telegram.InlineKeyboardButton("Reply", switch_inline_query_current_chat = "/reply %i %s" % (i, ' '.join(getMentions(status, two)))),
telegram.InlineKeyboardButton("Quote", switch_inline_query_current_chat = "/quote %i " % i)
2017-03-24 00:48:40 +00:00
]
2017-03-24 14:34:53 +00:00
if status.in_reply_to_status_id:
buttons += [telegram.InlineKeyboardButton("View Thread", callback_data = "/thread %i" % i)]
try:
rmu = telegram.InlineKeyboardMarkup(makeMenu(buttons, header=header))
except Exception as e:
logging.exception(e)
2017-03-24 00:48:40 +00:00
2017-03-26 18:30:51 +00:00
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 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(text)), reply_markup=rmu)
2017-03-26 18:30:51 +00:00
def trends(bot, update, args):
try:
count = int(args[0])
except:
count = 5
two = getTwo(update.message)
lat, lon = dbtools.dbHelper().getLocation(update.message.chat_id)
lt = []
try:
if lat:
woeid = two.api.trends_closest(lat, lon)[0]["woeid"]
else:
woeid = 1
trends = two.api.trends_place(woeid)[0]['trends']
2017-03-27 17:08:02 +00:00
buttons = []
2017-03-26 18:30:51 +00:00
2017-03-27 17:08:02 +00:00
for trend in trends[:count]:
buttons += [telegram.InlineKeyboardButton(trend['name'], callback_data = "/search %s" % trend['name'])]
2017-03-26 18:30:51 +00:00
2017-03-27 17:08:02 +00:00
rmo = telegram.InlineKeyboardMarkup(makeMenu(buttons))
update.message.reply_text(bottools.strings.trends, reply_markup = rmo)
2017-03-26 18:30:51 +00:00
except tweepy.error.TweepError as e:
twoExceptions(e, update.message)
2017-03-24 00:48:40 +00:00
2017-03-24 16:02:21 +00:00
def search(bot, update, args):
2017-03-27 17:08:02 +00:00
message = update.message or update.callback_query.message
2017-03-24 16:02:21 +00:00
try:
count = int(args[0])
query = ' '.join(args[1:])
except:
count = 5
query = ' '.join(args)
try:
2017-03-27 17:08:02 +00:00
two = bottools.methods.getTwo(message)
2017-03-24 16:02:21 +00:00
lt = []
2017-03-24 16:02:21 +00:00
for tweet in two.api.search(q=query, rpp=count, result_type="recent")[:count]:
lt += [tweet]
lt.reverse()
for tweet in lt:
2017-03-27 17:08:02 +00:00
tweetMessage(tweet, message.chat_id, bot)
2017-03-24 16:02:21 +00:00
except tweepy.error.TweepError as e:
2017-03-27 17:08:02 +00:00
bottools.methods.twoExceptions(e, message)
2017-03-24 16:02:21 +00:00
def user(bot, update, args):
try:
count = int(args[1])
except:
count = 5
try:
two = bottools.methods.getTwo(update.message)
lt = []
2017-03-24 21:46:43 +00:00
for status in two.api.user_timeline(args[0], count = count):
lt += [status]
lt.reverse()
for status in lt:
2017-03-26 18:30:51 +00:00
tweetMessage(status, update.message.chat_id, bot, True)
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
def selfTweets(bot, update, args):
try:
count = int(args[0])
except:
count = 5
bottools.methods.user(bot, update, [bottools.methods.getTwo(update.message).whoami(), count])
2017-03-24 00:48:40 +00:00
2017-03-22 00:53:51 +00:00
def timeline(bot, update, args = [10]):
try:
count = int(args[0])
except:
count = 10
db = dbtools.dbHelper()
db.executeQuery("DELETE FROM timelines WHERE cid = %i;" % int(update.message.chat_id))
2017-03-24 00:48:40 +00:00
db.commit()
2017-03-22 00:53:51 +00:00
try:
two = bottools.methods.getTwo(update.message)
lt = []
2017-03-22 00:53:51 +00:00
for status in two.api.home_timeline(count=count):
lt += [status]
lt.reverse()
for status in lt:
2017-03-26 18:30:51 +00:00
tweetMessage(status, update.message.chat_id, bot, True)
2017-03-22 00:53:51 +00:00
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
2017-03-23 13:18:52 +00:00
# Streaming
2017-03-23 16:32:06 +00:00
2017-03-23 13:18:52 +00:00
mentionstreams = {}
2017-03-27 17:08:02 +00:00
def makeStream(bot, cid, ato, ase):
# two = twitools.twObject(ato=ato, ase=ase)
# stream = tweepy.Stream(auth = two.auth, listener = bottools.streaming.BotStreamListener(bot, cid, ato))
# stream.filter(track=["%s" % two.whoami().strip("@")], async=True)
stream = bottools.thread.BotStreamListener(bot, cid, ato)
2017-03-23 16:01:05 +00:00
return stream
try:
for u in dbtools.dbHelper().mentionsOn():
2017-03-27 17:08:02 +00:00
mentionstreams[u[1]] = makeStream(telegram.Bot(token=setuptools.token()), u[0], u[1], u[2])
2017-03-23 16:01:05 +00:00
except Exception as e:
print(e)
2017-03-23 13:18:52 +00:00
def mentionstream(bot, update):
global mentionstreams
2017-03-23 16:01:05 +00:00
try:
2017-03-27 17:08:02 +00:00
message = update.message or update.callback_query.message
db = dbtools.dbHelper()
cid = message.chat_id
db.toggleMentions(cid)
ato = db.ato(cid)
ase = db.ase(cid)
2017-03-23 16:01:05 +00:00
2017-03-27 17:08:02 +00:00
if ato in mentionstreams:
mentionstreams.pop(ato).disconnect()
2017-03-23 16:01:05 +00:00
update.message.reply_text(bottools.strings.toggleMentions % bottools.strings.toggleTweetOff)
else:
2017-03-27 17:08:02 +00:00
mentionstreams[ato] = makeStream(bot, cid, ato, ase)
2017-03-23 16:01:05 +00:00
update.message.reply_text(bottools.strings.toggleMentions % bottools.strings.toggleTweetOn)
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
2017-03-23 13:18:52 +00:00
# Admin interaction
def togglebroadcasts(bot, update):
try:
update.message.reply_text(bottools.strings.toggleBroadcasts % (bottools.strings.toggleTweetOn if dbtools.dbHelper().toggleBroadcasts(update.message.chat_id) else bottools.strings.toggleTweetOff))
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
2017-03-23 12:05:48 +00:00
# Administrator
def isadmin(message):
two = bottools.methods.getTwo(message)
if two.whoami().strip("@") == setuptools.admin().strip("@"):
return True
return False
def restart(bot, update):
global mentionstreams
2017-03-23 12:05:48 +00:00
if bottools.methods.isadmin(update.message):
update.message.reply_text(bottools.strings.restart)
while len(mentionstreams) > 0:
so = mentionstreams.pop(list(mentionstreams)[0])
so.disconnect()
2017-03-23 12:05:48 +00:00
os.execl(sys.executable, sys.executable, *sys.argv)
else:
bottools.methods.unknown(bot, update)
2017-03-23 16:01:05 +00:00
2017-03-23 16:32:06 +00:00
def broadcast(bot, update, args):
if bottools.methods.isadmin(update.message):
for u in dbtools.dbHelper().broadcastUsers():
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):
2017-03-23 16:32:06 +00:00
if bottools.methods.isadmin(update.message):
for u in dbtools.dbHelper().allUsers():
try:
bot.sendMessage(chat_id = u, text = ' '.join(args))
except:
logging.exception("Could not send emergency broadcast.")
2017-03-23 16:32:06 +00:00
else:
bottools.methods.unknown(bot, update)
2017-03-24 00:48:40 +00:00
# Variables
commands = {
2017-03-27 15:39:52 +00:00
"accounts": accounts,
2017-03-24 00:48:40 +00:00
"auth": auth,
"broadcast": broadcast,
"emergency": emergency,
"fish": fish,
"follow": follow,
"help": start,
"like": like,
"login": auth,
"logout": unauth,
"mentionstream": mentionstream,
2017-03-27 12:04:36 +00:00
"nolocation": noLocation,
2017-03-24 12:08:47 +00:00
"quote": quote,
2017-03-24 00:48:40 +00:00
"reply": reply,
"restart": restart,
"retweet": retweet,
2017-03-24 16:02:21 +00:00
"search": search,
"self": selfTweets,
2017-03-26 18:30:51 +00:00
"sharelocation": shareLocation,
"silence": silence,
2017-03-24 00:48:40 +00:00
"start": start,
2017-03-27 15:39:52 +00:00
"switch": switch,
2017-03-24 14:34:53 +00:00
"thread": thread,
2017-03-24 00:48:40 +00:00
"timeline": timeline,
"togglebroadcasts": togglebroadcasts,
"toggleconfirmations": toggleConfirmations,
2017-03-24 00:48:40 +00:00
"togglementions": mentionstream,
"toggletweet": toggleTweet,
2017-03-26 18:30:51 +00:00
"trends": trends,
2017-03-24 00:48:40 +00:00
"tweet": explicitTweet,
"unauth": unauth,
"unfollow": unfollow,
2017-03-26 18:30:51 +00:00
"unlike": unlike,
2017-03-27 12:04:36 +00:00
"unsetlocation": unsetLocation,
"user": user,
2017-03-24 00:48:40 +00:00
"verify": verify
}
pargs = [
broadcast,
emergency,
follow,
like,
2017-03-24 12:08:47 +00:00
quote,
2017-03-24 00:48:40 +00:00
reply,
retweet,
2017-03-24 16:02:21 +00:00
search,
selfTweets,
2017-03-27 15:39:52 +00:00
switch,
2017-03-24 14:34:53 +00:00
thread,
2017-03-24 00:48:40 +00:00
timeline,
toggleTweet,
2017-03-26 18:30:51 +00:00
trends,
unlike,
user,
2017-03-24 00:48:40 +00:00
explicitTweet,
unfollow,
verify
]