|
|
#!/usr/bin/env python # -*- coding: utf-8 -*-
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters from telegram import ParseMode import logging import random import requests import datetime
MENSA_URL = { "zentral": "zentralmensa", "nord": "nordmensa", "turm": "turmmensa" } MENSA_NAME = { "zentral": "Zentralmensa", "nord": "Nordmensa", "turm": "Turmmensa" }
# Enable logging logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
with open("stoll.txt", "r") as tmp_file: STOLL = tmp_file.readlines()
with open("manta.txt", "r") as tmp_file: MANTA = 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(bot, update): """Was ist das hier für eins Bot?""" update.message.reply_text("""Hallo, ich bin ein Bot.
Ich liefere tolle Informationen über die Mensa. Die Infos kommen von https://mensa.schneider-hosting.de Mhmm, lecker. Guten Appetit!""")
def help(bot, update): """Send a message when the command /help is issued.""" update.message.reply_text('Help!')
def magie(bot, update): """MAGIEEEEEE""" rand = random.SystemRandom() update.message.reply_text(STOLL[rand.randrange(0, len(STOLL), 1)])
def manta(bot, update): """Boah ey, geile Karre!""" rand = random.SystemRandom() update.message.reply_text(MANTA[rand.randrange(0, len(MANTA), 1)])
def mensa(bot, update, args): which = "zentral" if len(args): which = args[0]
if which not in MENSA_NAME: update.message.reply_text("Diese Mensa gibt es nicht! RTFM und versuchs nochmal.") return
today = datetime.datetime.now().date().weekday() + 1 today = 2 if datetime.datetime.now().time() > datetime.time(hour=16): # Es ist zu spät am Tag, zeig das essen für morgen an today += 1
today = today % 7
url = "https://mensa.schneider-hosting.de/static/%s.%d.json" % (MENSA_URL[which], today) request = requests.get(url) request.encoding = 'utf-8' data = request.json()
message = "Das Essen für %s in der %s \n" % (data["date"], MENSA_NAME[which]) if len(data["meals"]) > 0: for meal in data["meals"]: meal_line = "*%s*\n" % meal["category"] meal_line += meal["title"].strip() + "\n"
# Discord only allows up to 2000 chars per message if len(message) + len(meal_line) > 2000: update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN) message = meal_line + "\n" else: message += meal_line + "\n" update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN) else: update.message.reply_text("Nix zu futtern heute :(")
def echo(bot, update): """Echo the user message.""" update.message.reply_text(update.message.text)
def error(bot, update, error): """Log Errors caused by Updates.""" logger.warning('Update "%s" caused error "%s"', update, error)
def main(): """Start the bot.""" # Create the EventHandler and pass it your bot's token. token = open("token").read() updater = Updater(token.strip())
# Get the dispatcher to register handlers dp = updater.dispatcher
# on different commands - answer in Telegram 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("mensa", mensa, pass_args=True))
# 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()
|