twitools/bot.py
Klaus-Uwe Mitterer 74239a16b8 Fix tweeting
2017-02-10 18:54:30 +01:00

111 lines
3.8 KiB
Python
Executable file

#!/usr/bin/env python3
import ast, dbtools, logging, setuptools, strings, telegram.ext, twitools, tweepy
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
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 noauth(update):
update.message.reply_text(strings.noauth)
def start(bot, update):
update.message.reply_text(strings.start % (setuptools.botname(), setuptools.botname()))
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
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)
def unauth(bot, update):
dbtools.dbHelper().deleteUser(update.message.chat_id)
update.message.reply_text(strings.unauth % setuptools.url())
def fish(bot, update):
dbtools.dbHelper().addFish(update.message.chat_id)
update.message.reply_text("Yummy! Thanks! :3")
def explicitTweet(bot, update, args):
two = twitools.twoHelper(update.message.chat_id)
two.tweet(' '.join(args))
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))
def toggleTweet(bot, update):
try:
update.message.reply_text(strings.toggleTweet % ("on" if dbtools.dbHelper().toggleTweet(update.message.chat_id) else "off"))
except:
noauth(update)
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)
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_handler(telegram.ext.MessageHandler(telegram.ext.Filters.text, tweet))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.command, unknown))
updater.dispatcher.add_error_handler(log)
updater.start_polling()
updater.idle()