Refactoring in Telegram bot, transbot fix
This commit is contained in:
parent
fb5212bd2d
commit
ab0e32bf27
45
strings.py
45
strings.py
|
@ -48,6 +48,9 @@ Your data has been deleted. Of course, you can always just re-authenticate using
|
|||
It was great having you here. So long, and thanks for all the /fish!'''
|
||||
|
||||
|
||||
unknownCommand = '''Sorry, I didn't understand that command.'''
|
||||
|
||||
|
||||
verify = '''Thanks for authenticating. You can now use all of my features!'''
|
||||
|
||||
verifyfail = '''Oops, something went wrong during the authentication. Please try again:
|
||||
|
@ -55,3 +58,45 @@ verifyfail = '''Oops, something went wrong during the authentication. Please try
|
|||
* /auth'''
|
||||
|
||||
verifyimp = '''There is not currently an authentication process running for you. You may already be logged in, or you have not yet sent me an /auth command.'''
|
||||
|
||||
twoFail = '''Well, something went wrong. Try again later.'''
|
||||
|
||||
twoAuthFail = '''Could not authenticate you for some reason. Please try again later. If this problem persists, please try reauthenticating using /unauth and /auth.'''
|
||||
|
||||
toggleTweetOn = "on"
|
||||
|
||||
toggleTweetOff = "off"
|
||||
|
||||
fishThanks = '''Yummy! Thanks!'''
|
||||
|
||||
selfSpam = '''Don't tell me you just tried to report yourself for spam. Seriously?'''
|
||||
|
||||
accountSuspended = '''I'm afraid your account is suspended. I can't currently help you.'''
|
||||
|
||||
rateLimit = '''Whoa, buddy! Slow it down! You ran into a rate limit. Please try again later.'''
|
||||
|
||||
badToken = '''Oops. Something appears to be wrong with your credentials. Please try reauthenticating using /unauth and /auth.'''
|
||||
|
||||
overload = '''So, I've good good news and bad news. The bad news is, I was unable to process your request. The good news is, it isn't our fault. Seems like Twitter is experiencing higher load than usual. Please try again later.'''
|
||||
|
||||
twitterError = '''So, I've good good news and bad news. The bad news is, I was unable to process your request. The good news is, it isn't our fault. Seems like Twitter is having some technical difficulties. Please try again later.'''
|
||||
|
||||
unknownTweet = '''Well, it seems that this tweet doesn't exist any more. It was probably deleted. Sorry about that.'''
|
||||
|
||||
followLimit = '''Twitter doesn't allow you to follow more accounts. You may want to unfollow somebody else first or try to get more followers yourself. (Yeah, I know.)'''
|
||||
|
||||
tweetLimit = '''Twitter doesn't allow you to post any new tweets right now because you ran into some kind of limit. Unfortunately, I can't really tell you when you'll be able to tweet again either.'''
|
||||
|
||||
dupTweet = '''Didn't you just tweet that? If you're sure that you want to tweet this again, please wait a few minutes and try again.'''
|
||||
|
||||
automatedTweet = '''Twitter thinks you're a robot. I'm pretty sure that you're a human being, but they won't listen to me. Anyway, I can't process your request at this time. Sorry about that.'''
|
||||
|
||||
selfMute = '''Why would you want to mute yourself? Your tweets are great!'''
|
||||
|
||||
notMuted = '''It's great that you want to unmute this account. But you actually haven't muted them in the first place...'''
|
||||
|
||||
multipleGIFs = '''I don't understand it either, but Twitter doesn't allow uploading multiple images if one of them is an animated GIF... (It's /ˈɡɪf/, not /ˈdʒɪf/. Just saying.)'''
|
||||
|
||||
accountLocked = '''Your account was locked by Twitter for security reasons. Please log in at the Twitter website (https://twitter.com/) and follow the instructions there.'''
|
||||
|
||||
longTweet = '''Sorry, but this tweet is too long.'''
|
||||
|
|
|
@ -8,10 +8,40 @@ logger = logging.getLogger(__name__)
|
|||
def log(bot, update, error):
|
||||
logger.warn("Error %s caused by '%s'" % (error, update))
|
||||
|
||||
updater = telegram.ext.Updater(token=setuptools.token())
|
||||
def twoExceptions(code, message):
|
||||
text = {
|
||||
32: strings.badToken,
|
||||
36: strings.selfSpam,
|
||||
64: strings.accountSuspended,
|
||||
88: strings.rateLimit,
|
||||
89: strings.badToken,
|
||||
99: strings.badToken,
|
||||
130: strings.overload,
|
||||
131: strings.twitterError,
|
||||
161: strings.followLimit,
|
||||
185: strings.tweetLimit,
|
||||
187: strings.dupTweet,
|
||||
205: strings.rateLimit,
|
||||
226: strings.automatedTweet,
|
||||
271: strings.selfMute,
|
||||
272: strings.notMuted,
|
||||
323: strings.multipleGIFs,
|
||||
326: strings.accountLocked,
|
||||
354: strings.longTweet
|
||||
}.get(code, strings.twoFail)
|
||||
|
||||
def noauth(update):
|
||||
update.message.reply_text(strings.noauth)
|
||||
message.reply_text(text)
|
||||
|
||||
def getTwo(message):
|
||||
try:
|
||||
return twitools.twoBotHelper(message.chat_id)
|
||||
except ValueError:
|
||||
noauth(message)
|
||||
except tweepy.error.TweepError as e:
|
||||
twoExceptions(e.api_code, message)
|
||||
|
||||
def noauth(message):
|
||||
message.reply_text(strings.noauth)
|
||||
|
||||
def start(bot, update):
|
||||
update.message.reply_text(strings.start % (setuptools.botname(), setuptools.botname()))
|
||||
|
@ -54,18 +84,21 @@ def unauth(bot, update):
|
|||
|
||||
def fish(bot, update):
|
||||
dbtools.dbHelper().addFish(update.message.chat_id)
|
||||
update.message.reply_text("Yummy! Thanks! :3")
|
||||
update.message.reply_text(strings.fishThanks)
|
||||
|
||||
def explicitTweet(bot, update, args):
|
||||
two = twitools.twoBotHelper(update.message.chat_id)
|
||||
two.tweet(' '.join(args))
|
||||
try:
|
||||
two = getTwo(update.message)
|
||||
two.tweet(' '.join(args))
|
||||
except tweepy.error.TweepError as e:
|
||||
twoExceptions(e.api_code, update.message)
|
||||
|
||||
def tweet(bot, update):
|
||||
try:
|
||||
if dbtools.dbHelper().getTStatus(update.message.chat_id):
|
||||
explicitTweet(bot, update, [update.message.text])
|
||||
except:
|
||||
noauth(update)
|
||||
noauth(update.message)
|
||||
|
||||
def timeline(bot, update, args = [10]):
|
||||
try:
|
||||
|
@ -73,39 +106,44 @@ def timeline(bot, update, args = [10]):
|
|||
except:
|
||||
count = 10
|
||||
|
||||
two = twitools.twoBotHelper(update.message.chat_id)
|
||||
|
||||
for status in two.api.home_timeline(count=count):
|
||||
update.message.reply_text("%s (%s) at %s: %s" % (status.author.name, status.author.screen_name, status.created_at, status.text))
|
||||
try:
|
||||
two = getTwo(update.message)
|
||||
for status in two.api.home_timeline(count=count):
|
||||
update.message.reply_text("%s (%s) at %s: %s" % (status.author.name, status.author.screen_name, status.created_at, status.text))
|
||||
except tweepy.error.TweepError as e:
|
||||
twoExceptions(e.api_code, update.message)
|
||||
|
||||
def toggleTweet(bot, update):
|
||||
try:
|
||||
update.message.reply_text(strings.toggleTweet % ("on" if dbtools.dbHelper().toggleTweet(update.message.chat_id) else "off"))
|
||||
update.message.reply_text(strings.toggleTweet % (strings.toggleTweetOn if dbtools.dbHelper().toggleTweet(update.message.chat_id) else strings.toggleTweetOff))
|
||||
except:
|
||||
noauth(update)
|
||||
noauth(update.message)
|
||||
|
||||
def unknown(bot, update):
|
||||
update.message.reply_text("Sorry, I didn't understand that command.")
|
||||
update.message.reply_text(strings.unknownCommand)
|
||||
|
||||
def test(bot, update, args):
|
||||
print(args)
|
||||
unknown(bot, update)
|
||||
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("auth", auth))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("fish", fish))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("help", start))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("start", start))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("test", test, pass_args=True))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("timeline", timeline))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("toggletweet", toggleTweet))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("tweet", explicitTweet, pass_args=True))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("unauth", unauth))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("verify", verify, pass_args=True))
|
||||
if __name__ == "__main__":
|
||||
updater = telegram.ext.Updater(token=setuptools.token())
|
||||
|
||||
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.text, tweet))
|
||||
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.command, unknown))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("auth", auth))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("fish", fish))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("help", start))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("start", start))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("test", test, pass_args=True))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("timeline", timeline))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("toggletweet", toggleTweet))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("tweet", explicitTweet, pass_args=True))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("unauth", unauth))
|
||||
updater.dispatcher.add_handler(telegram.ext.CommandHandler("verify", verify, pass_args=True))
|
||||
|
||||
updater.dispatcher.add_error_handler(log)
|
||||
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.text, tweet))
|
||||
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.command, unknown))
|
||||
|
||||
updater.start_polling()
|
||||
updater.idle()
|
||||
updater.dispatcher.add_error_handler(log)
|
||||
|
||||
updater.start_polling()
|
||||
updater.idle()
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import html.parser, tweepy, os, setuptools, google.cloud.translate, twitools, re
|
||||
import httptools, html.parser, tweepy, os, setuptools, google.cloud.translate, twitools, re
|
||||
|
||||
lang = setuptools.getListSetting("Translate", "lang")
|
||||
ato = setuptools.getListSetting("Translate", "ato")
|
||||
|
@ -50,9 +50,9 @@ for status in timeline:
|
|||
|
||||
for s in split:
|
||||
if re.match(r'https?:\/\/[\S]*', s):
|
||||
out = " ".join([out, httptools.shortURL(s)])
|
||||
intext = " ".join([intext, httptools.shortURL(s)])
|
||||
else:
|
||||
out = " ".join([out, s])
|
||||
intext = " ".join([intext, s])
|
||||
tstring = translator.translate(intext, target_language=a[0])['translatedText'].replace("@", "@")
|
||||
|
||||
try:
|
||||
|
|
|
@ -72,4 +72,9 @@ def tweet(text, ref = 0, section = setuptools.TWITTER):
|
|||
|
||||
def twoBotHelper(cid):
|
||||
db = dbtools.dbHelper()
|
||||
return twObject(ato = db.ato(cid), ase = db.ase(cid))
|
||||
ato = db.ato(cid)
|
||||
ase = db.ase(cid)
|
||||
if ato:
|
||||
return twObject(ato = db.ato(cid), ase = db.ase(cid))
|
||||
else:
|
||||
raise ValueError("Unknown user %s" % cid)
|
||||
|
|
Loading…
Reference in a new issue