#!/usr/bin/env python # -*- coding: utf-8 -*- import logging import random from telegram import ParseMode, Update from telegram.ext import Updater, CommandHandler, CallbackContext from web_requests import send_coding_love_gif, send_wiki_how_article from reddit import wrong_dog, hamster, reddit, reddit_img, cat, dog, drillcat from schneiderbot_twitter import toggle_pepito_for_group, check_pepito_and_post_auto, check_pepito_and_post_manually from db import Db from mensa import mensa, linux_mensa # Enable logging logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) with open("res/stoll.txt", "r") as tmp_file: STOLL = tmp_file.readlines() with open("res/manta.txt", "r") as tmp_file: MANTA = tmp_file.readlines() with open("res/goodlife.txt", "r") as tmp_file: GOOD_LIFE = tmp_file.readlines() with open("res/simon.txt", "r") as tmp_file: SIMON = tmp_file.readlines() # Define a few command handlers. These usually take the two arguments bot and # update. Error handlers also receive the raised TelegramError object in error. def start(update: Update, context: CallbackContext): """Was ist das hier für eins Bot?""" update.message.reply_text("""Hallo, ich bin ein Bot. Ich liefere tolle Informationen über die Mensa, Katzenbilder, Hundebilder und andere _tolle_ Sachen. Mhmm, lecker. Guten Appetit!""", parse_mode=ParseMode.MARKDOWN) def help(update: Update, context: CallbackContext): """Send a message when the command /help is issued.""" reply_text = """Commands: *Mensa*: /mensa _mensa_ - prints filtered list of meals for _mensa_ (if no _mensa_ given, zentralmensa is used) /mensa _mensa_ full - prints full list of meals for _mensa_ (if no _mensa_ given, zentralmensa is used) *Reddit*: /cat - random cat image (using reddit.com/r/catpictures) /dog - random dog image (using reddit.com/r/dogpictures) /hamster - random dog image (using reddit.com/r/hamsters) /drillcat - random drillcat image (using reddit.com/r/drillcats) /wrongdog - random wrongdog image (using reddit.com/r/whatswrongwithyourdog) /reddit subreddit - random submission from reddit.com/r/subreddit /reddit\_img subreddit - random image submission from reddit.com/r/subreddit *Quotes*: /magie - random Axel Stoll quote /manta - random Opel Manta joke /goodlife - random good life quote *Other fun stuff*: /codinglove - random gif from the coding love (https://thecodinglove.com) /wikihow - random wikihow article (https://http://de.wikihow.com) /shrug - prints shrug /kill - kills you /revive - revives you /sudo kill/revive - force kill/revive /graveyard - shows the dead people /help - this help text""" print(reply_text) update.message.reply_text(reply_text, parse_mode=ParseMode.MARKDOWN) def kill(update: Update, context: CallbackContext, sudocall=False): """kill me pls""" db = Db() user = update.message.from_user.first_name if db.is_dead(user, update.message.chat_id): if sudocall: update.message.reply_text("%s killed again" % user) return update.message.reply_text("%s is already dead" % user) return message = update.message.from_user.first_name + " died" if sudocall: message += " for real" db.kill(user, update.message.chat_id) update.message.reply_text(message) def revive(update: Update, context: CallbackContext, sudocall=False): """unkill me pls""" db = Db() user = update.message.from_user.first_name if not db.is_dead(user, update.message.chat_id): update.message.reply_text("Maybe %s should have a litte 'accident' before" % user) return if sudocall: update.message.reply_text("%s is living again, for real" % user) db.revive(user, update.message.chat_id) return update.message.reply_text("You fool! You cannot revive a dead person!") SUDO_CMDS = { "kill": kill, "revive": revive } def graveyard(update: Update, context: CallbackContext): """List the dead""" db = Db() update.message.reply_text("Here are the dead people:\n%s" % db.get_dead_bodies(update.message.chat_id)) def sudo(update: Update, context: CallbackContext, args): """for real""" if not args: update.message.reply_text("Unknown command") return for arg in args: if arg in SUDO_CMDS: SUDO_CMDS[arg](update, context, True) else: update.message.reply_text("Unknown command") def send_cat_dog(update: Update, context: CallbackContext): """Best of both worlds!""" catdog = "https://upload.wikimedia.org/wikipedia/en/6/64/CatDog.jpeg" context.bot.send_photo(chat_id=update.message.chat_id, photo=catdog) def magie(update: Update, context: CallbackContext): """MAGIEEEEEE""" rand = random.SystemRandom() update.message.reply_text(STOLL[rand.randrange(0, len(STOLL), 1)]) def manta(update: Update, context: CallbackContext): """Boah ey, geile Karre!""" rand = random.SystemRandom() update.message.reply_text(MANTA[rand.randrange(0, len(MANTA), 1)]) def goodlife(update: Update, context: CallbackContext): """GOOD LIFE MY A...""" rand = random.SystemRandom() update.message.reply_text(GOOD_LIFE[rand.randrange(0, len(GOOD_LIFE), 1)]) def shrug(update: Update, context: CallbackContext): """SHRUG""" update.message.reply_text("¯\_(ツ)_/¯") def simon(update: Update, context: CallbackContext, args): """KAUF DIR N HUND""" name = "Simon" if len(args) > 0: name = args[0] rand = random.SystemRandom() animal = SIMON[rand.randrange(0, len(SIMON), 1)].replace('\n', '') update.message.reply_text( "%s, bitte kauf dir einen Hund, eine Katze oder von mir aus auch %s, du hast zu viel Zeit" % (name, animal)) def echo(update: Update, context: CallbackContext): """Echo the user message.""" update.message.reply_text(update.message.text) def error(update: Update, context: CallbackContext): """Log Errors caused by Updates.""" logger.warning('Update "%s" caused error "%s"', update, context.error) def init_commands(dp): dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) dp.add_handler(CommandHandler("magie", magie)) dp.add_handler(CommandHandler("manta", manta)) dp.add_handler(CommandHandler("goodlife", goodlife)) dp.add_handler(CommandHandler("mensa", mensa, pass_args=True)) dp.add_handler(CommandHandler("lmensa", linux_mensa)) dp.add_handler(CommandHandler("sudo", sudo, pass_args=True)) dp.add_handler(CommandHandler("cat", cat)) dp.add_handler(CommandHandler("dog", dog)) dp.add_handler(CommandHandler("catdog", send_cat_dog)) dp.add_handler(CommandHandler("shrug", shrug)) dp.add_handler(CommandHandler("kill", kill)) dp.add_handler(CommandHandler("revive", revive)) dp.add_handler(CommandHandler("simon", simon, pass_args=True)) dp.add_handler(CommandHandler("graveyard", graveyard)) dp.add_handler(CommandHandler("codinglove", send_coding_love_gif)) dp.add_handler(CommandHandler("wiegeht", send_wiki_how_article)) dp.add_handler(CommandHandler("wikihow", send_wiki_how_article)) dp.add_handler(CommandHandler("wrongdog", wrong_dog)) dp.add_handler(CommandHandler("hamster", hamster)) dp.add_handler(CommandHandler("drillcat", drillcat)) dp.add_handler(CommandHandler("reddit", reddit)) dp.add_handler(CommandHandler("reddit_img", reddit_img)) dp.add_handler(CommandHandler("pepito", check_pepito_and_post_manually)) dp.add_handler(CommandHandler("tpepito", toggle_pepito_for_group)) def main(): """Start the bot.""" # Create the EventHandler and pass it your bot's token. token = open("config/token").read() updater = Updater(token.strip()) jq = updater.job_queue # Get the dispatcher to register handlers dp = updater.dispatcher init_commands(dp) jq.run_repeating(check_pepito_and_post_auto, interval=180, first=0) # on different commands - answer in Telegram # log all errors dp.add_error_handler(error) # Start the Bot updater.start_polling() # Run the bot until you press Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle() if __name__ == '__main__': main()