twitools/bot.py

111 lines
3.8 KiB
Python
Raw Normal View History

2017-02-07 21:36:31 +00:00
#!/usr/bin/env python3
import ast, dbtools, logging, setuptools, strings, telegram.ext, twitools, tweepy
2017-02-07 21:36:31 +00:00
2017-02-07 22:11:59 +00:00
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
2017-02-08 13:58:09 +00:00
logger = logging.getLogger(__name__)
def log(bot, update, error):
logger.warn("Error %s caused by '%s'" % (error, update))
2017-02-07 22:11:59 +00:00
updater = telegram.ext.Updater(token=setuptools.token())
2017-02-07 21:54:22 +00:00
def noauth(update):
update.message.reply_text(strings.noauth)
2017-02-07 21:54:22 +00:00
def start(bot, update):
update.message.reply_text(strings.start % (setuptools.botname(), setuptools.botname()))
2017-02-08 11:26:25 +00:00
def auth(bot, update):
db = dbtools.dbHelper()
cid = update.message.chat_id
if not (db.ato(cid) or db.ase(cid)):
auth = tweepy.OAuthHandler(setuptools.cke(), setuptools.cse())
update.message.reply_text(strings.auth % auth.get_authorization_url())
dbtools.dbHelper().storeToken(cid, auth.request_token)
else:
update.message.reply_text(strings.authimp)
def verify(bot, update, args):
db = dbtools.dbHelper()
cid = update.message.chat_id
2017-02-07 21:54:22 +00:00
if db.ato(cid) and not db.ase(cid):
auth = tweepy.OAuthHandler(setuptools.cke(), setuptools.cse())
auth.request_token = ast.literal_eval(db.ato(cid))
try:
auth.get_access_token(args[0])
dbtools.dbHelper().storeUser(cid, auth.access_token, auth.access_token_secret)
update.message.reply_text(strings.verify)
except Exception as e:
dbtools.dbHelper().deleteUser(update.message.chat_id)
update.message.reply_text(strings.verifyfail)
else:
update.message.reply_text(strings.verifyimp)
2017-02-08 12:06:17 +00:00
def unauth(bot, update):
2017-02-08 12:12:05 +00:00
dbtools.dbHelper().deleteUser(update.message.chat_id)
update.message.reply_text(strings.unauth % setuptools.url())
2017-02-08 12:06:17 +00:00
2017-02-08 13:09:00 +00:00
def fish(bot, update):
2017-02-08 14:23:15 +00:00
dbtools.dbHelper().addFish(update.message.chat_id)
update.message.reply_text("Yummy! Thanks! :3")
def explicitTweet(bot, update, args):
2017-02-08 14:14:03 +00:00
update.message.reply_text("Ooops. Not implemented yet.")
def tweet(bot, update):
try:
if dbtools.dbHelper().getTStatus(update.message.chat_id):
explicitTweet(bot, update, update.message.text)
except:
noauth(update)
def timeline(bot, update, args = [10]):
try:
count = int(args[0])
except:
count = 10
two = twitools.twoHelper(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))
2017-02-08 13:09:00 +00:00
2017-02-08 14:14:03 +00:00
def toggleTweet(bot, update):
2017-02-08 14:19:01 +00:00
try:
2017-02-08 14:23:15 +00:00
update.message.reply_text(strings.toggleTweet % ("on" if dbtools.dbHelper().toggleTweet(update.message.chat_id) else "off"))
2017-02-08 14:19:01 +00:00
except:
noauth(update)
2017-02-08 14:14:03 +00:00
2017-02-08 13:58:09 +00:00
def unknown(bot, update):
update.message.reply_text("Sorry, I didn't understand that command.")
def test(bot, update, args):
print(args)
unknown(bot, update)
2017-02-08 11:26:25 +00:00
updater.dispatcher.add_handler(telegram.ext.CommandHandler("auth", auth))
2017-02-08 13:09:00 +00:00
updater.dispatcher.add_handler(telegram.ext.CommandHandler("fish", fish))
2017-02-08 14:19:01 +00:00
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))
2017-02-08 14:14:03 +00:00
updater.dispatcher.add_handler(telegram.ext.CommandHandler("toggletweet", toggleTweet))
updater.dispatcher.add_handler(telegram.ext.CommandHandler("tweet", explicitTweet, pass_args=True))
2017-02-08 14:19:01 +00:00
updater.dispatcher.add_handler(telegram.ext.CommandHandler("unauth", unauth))
updater.dispatcher.add_handler(telegram.ext.CommandHandler("verify", verify, pass_args=True))
2017-02-08 14:14:03 +00:00
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.text, tweet))
2017-02-08 13:58:09 +00:00
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.command, unknown))
updater.dispatcher.add_error_handler(log)
2017-02-07 21:54:22 +00:00
2017-02-08 13:16:04 +00:00
updater.start_polling()
updater.idle()