Telegram version of schneiderbot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.0 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
  4. from telegram import ParseMode
  5. import logging
  6. import random
  7. import requests
  8. import datetime
  9. MENSA_URL = {
  10. "zentral": "zentralmensa",
  11. "nord": "nordmensa",
  12. "turm": "turmmensa"
  13. }
  14. MENSA_NAME = {
  15. "zentral": "Zentralmensa",
  16. "nord": "Nordmensa",
  17. "turm": "Turmmensa"
  18. }
  19. # Enable logging
  20. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  21. level=logging.INFO)
  22. logger = logging.getLogger(__name__)
  23. with open("stoll.txt", "r") as tmp_file:
  24. STOLL = tmp_file.readlines()
  25. with open("manta.txt", "r") as tmp_file:
  26. MANTA = tmp_file.readlines()
  27. # Define a few command handlers. These usually take the two arguments bot and
  28. # update. Error handlers also receive the raised TelegramError object in error.
  29. def start(bot, update):
  30. """Was ist das hier für eins Bot?"""
  31. update.message.reply_text("""Hallo, ich bin ein Bot.
  32. Ich liefere tolle Informationen über die Mensa. Die Infos kommen von
  33. https://mensa.schneider-hosting.de
  34. Mhmm, lecker. Guten Appetit!""")
  35. def help(bot, update):
  36. """Send a message when the command /help is issued."""
  37. update.message.reply_text('Help!')
  38. def magie(bot, update):
  39. """MAGIEEEEEE"""
  40. rand = random.SystemRandom()
  41. update.message.reply_text(STOLL[rand.randrange(0, len(STOLL), 1)])
  42. def manta(bot, update):
  43. """Boah ey, geile Karre!"""
  44. rand = random.SystemRandom()
  45. update.message.reply_text(MANTA[rand.randrange(0, len(MANTA), 1)])
  46. def mensa(bot, update, args):
  47. which = "zentral"
  48. if len(args):
  49. which = args[0]
  50. if which not in MENSA_NAME:
  51. update.message.reply_text("Diese Mensa gibt es nicht! RTFM und versuchs nochmal.")
  52. return
  53. today = datetime.datetime.now().date().weekday() + 1
  54. today = 2
  55. if datetime.datetime.now().time() > datetime.time(hour=16):
  56. # Es ist zu spät am Tag, zeig das essen für morgen an
  57. today += 1
  58. today = today % 7
  59. url = "https://mensa.schneider-hosting.de/static/%s.%d.json" % (MENSA_URL[which], today)
  60. request = requests.get(url)
  61. request.encoding = 'utf-8'
  62. data = request.json()
  63. message = "Das Essen für %s in der %s \n" % (data["date"], MENSA_NAME[which])
  64. if len(data["meals"]) > 0:
  65. for meal in data["meals"]:
  66. meal_line = "*%s*\n" % meal["category"]
  67. meal_line += meal["title"].strip() + "\n"
  68. # Discord only allows up to 2000 chars per message
  69. if len(message) + len(meal_line) > 2000:
  70. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  71. message = meal_line + "\n"
  72. else:
  73. message += meal_line + "\n"
  74. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  75. else:
  76. update.message.reply_text("Nix zu futtern heute :(")
  77. def echo(bot, update):
  78. """Echo the user message."""
  79. update.message.reply_text(update.message.text)
  80. def error(bot, update, error):
  81. """Log Errors caused by Updates."""
  82. logger.warning('Update "%s" caused error "%s"', update, error)
  83. def main():
  84. """Start the bot."""
  85. # Create the EventHandler and pass it your bot's token.
  86. token = open("token").read()
  87. updater = Updater(token.strip())
  88. # Get the dispatcher to register handlers
  89. dp = updater.dispatcher
  90. # on different commands - answer in Telegram
  91. dp.add_handler(CommandHandler("start", start))
  92. dp.add_handler(CommandHandler("help", help))
  93. dp.add_handler(CommandHandler("magie", magie))
  94. dp.add_handler(CommandHandler("manta", manta))
  95. dp.add_handler(CommandHandler("mensa", mensa, pass_args=True))
  96. # log all errors
  97. dp.add_error_handler(error)
  98. # Start the Bot
  99. updater.start_polling()
  100. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  101. # SIGTERM or SIGABRT. This should be used most of the time, since
  102. # start_polling() is non-blocking and will stop the bot gracefully.
  103. updater.idle()
  104. if __name__ == '__main__':
  105. main()