From 6649fde6b979ea3663ec21576b54b4ae2b57789f Mon Sep 17 00:00:00 2001 From: angerstoner Date: Sat, 31 Mar 2018 00:11:48 +0200 Subject: [PATCH] added mensa command --- schneiderbot.py | 71 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/schneiderbot.py b/schneiderbot.py index 2155c64..4f2f623 100644 --- a/schneiderbot.py +++ b/schneiderbot.py @@ -1,25 +1,23 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -"""Simple Bot to reply to Telegram messages. - -This program is dedicated to the public domain under the CC0 license. - -This Bot uses the Updater class to handle the bot. - -First, a few handler functions are defined. Then, those functions are passed to -the Dispatcher and registered at their respective places. -Then, the bot is started and runs until we press Ctrl-C on the command line. - -Usage: -Basic Echobot example, repeats messages. -Press Ctrl-C on the command line or send a signal to the process to stop the -bot. -""" - 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', @@ -56,6 +54,46 @@ def manta(bot, update): 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) @@ -80,6 +118,7 @@ def main(): 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