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.

221 lines
5.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  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. ## TODO: rather use filter list, this is bloated; mensa italia missing
  20. CATEGORIES_LIGHT = {
  21. "Eintopf",
  22. "Menü I",
  23. "Menü II",
  24. "Vegan",
  25. "Vegetarisch",
  26. "Grill III",
  27. "Al dente",
  28. "Pasta und Gratins",
  29. "Kartoffeln und Co.",
  30. ## Nordmensa:
  31. "Stamm 1 vegetarisch",
  32. "Nordmensa-Fit",
  33. "Stamm 2",
  34. "Stamm 3",
  35. ## Turmmensa:
  36. "Turm vegetarisch Kombi",
  37. "Turm 2b",
  38. "Turm 3a",
  39. "Turm Vegan",
  40. "Regionale Linie"
  41. }
  42. CATEGORIES_FULL = {
  43. "Eintopf",
  44. "Menü I",
  45. "Menü II",
  46. "Vegan",
  47. "Vegetarisch",
  48. "Grill III",
  49. "CampusCurry",
  50. "natürlich fit",
  51. "Al dente",
  52. "Pasta und Gratins",
  53. "Fitnesscenter",
  54. "Salatbuffet",
  55. "Studentenfutter",
  56. "Süße Versuchung",
  57. "Süße Spezial Tagesangebot",
  58. "Kartoffeln und Co.",
  59. "Vollwert & Co. Stärke",
  60. "Vollwert & Co. Gemüse",
  61. "Bio-Beilagen",
  62. "Dessertbuffet",
  63. "Last Minute ab 14:30 Uhr",
  64. ## Nordmensa:
  65. "Stamm 1 vegetarisch",
  66. "Nordmensa-Fit",
  67. "Stamm 2",
  68. "Stamm 3",
  69. "Salatbuffet/Pastapoint",
  70. "Last Minute ab 13:30 Uhr",
  71. ## Turmmensa:
  72. "Turm vegetarisch Kombi",
  73. "Turm 2b",
  74. "Turm 3a",
  75. "Turm Vegan",
  76. "Regionale Linie",
  77. "Beilagen",
  78. "Last Minute ab 14:00Uhr"
  79. }
  80. MODES = {
  81. "light" : CATEGORIES_LIGHT,
  82. "full" : CATEGORIES_FULL
  83. }
  84. # Enable logging
  85. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  86. level=logging.INFO)
  87. logger = logging.getLogger(__name__)
  88. with open("stoll.txt", "r") as tmp_file:
  89. STOLL = tmp_file.readlines()
  90. with open("manta.txt", "r") as tmp_file:
  91. MANTA = tmp_file.readlines()
  92. # Define a few command handlers. These usually take the two arguments bot and
  93. # update. Error handlers also receive the raised TelegramError object in error.
  94. def start(bot, update):
  95. """Was ist das hier für eins Bot?"""
  96. update.message.reply_text("""Hallo, ich bin ein Bot.
  97. Ich liefere tolle Informationen über die Mensa. Die Infos kommen von
  98. https://mensa.schneider-hosting.de
  99. Mhmm, lecker. Guten Appetit!""")
  100. def help(bot, update):
  101. """Send a message when the command /help is issued."""
  102. update.message.reply_text("""Commands:
  103. *Mensa*:
  104. /mensa _mensa_ - prints filtered list of meals for _mensa_ (if no _mensa_ given, zentralmensa is used)
  105. /mensa _mensa_ full - prints full list of meals for _mensa_ (if no _mensa_ given, zentralmensa is used)
  106. *Fun*:
  107. /magie - random Axel Stoll qoute
  108. /manta - random Opel Manta joke
  109. /help - this help text""", parse_mode=ParseMode.MARKDOWN)
  110. def magie(bot, update):
  111. """MAGIEEEEEE"""
  112. rand = random.SystemRandom()
  113. update.message.reply_text(STOLL[rand.randrange(0, len(STOLL), 1)])
  114. def manta(bot, update):
  115. """Boah ey, geile Karre!"""
  116. rand = random.SystemRandom()
  117. update.message.reply_text(MANTA[rand.randrange(0, len(MANTA), 1)])
  118. def mensa(bot, update, args):
  119. which = "zentral"
  120. categories = CATEGORIES_LIGHT
  121. for arg in args:
  122. if arg in MENSA_NAME:
  123. which = args[0]
  124. elif arg in MODES:
  125. categories = MODES[arg]
  126. else:
  127. update.message.reply_text("Falscher Aufruf! RTFM und versuchs nochmal.")
  128. return
  129. today = datetime.datetime.now().date().weekday() + 1
  130. today = 2
  131. if datetime.datetime.now().time() > datetime.time(hour=16):
  132. # Es ist zu spät am Tag, zeig das essen für morgen an
  133. today += 1
  134. today = today % 7
  135. url = "https://mensa.schneider-hosting.de/static/%s.%d.json" % (MENSA_URL[which], today)
  136. request = requests.get(url)
  137. request.encoding = 'utf-8'
  138. data = request.json()
  139. message = "Das Essen für %s in der %s \n" % (data["date"], MENSA_NAME[which])
  140. if len(data["meals"]) > 0:
  141. for meal in data["meals"]:
  142. if meal["category"] in categories:
  143. meal_line = "*%s*\n" % meal["category"]
  144. meal_line += meal["title"].strip() + "\n"
  145. # Discord only allows up to 2000 chars per message
  146. if len(message) + len(meal_line) > 2000:
  147. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  148. message = meal_line + "\n"
  149. else:
  150. message += meal_line + "\n"
  151. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  152. else:
  153. update.message.reply_text("Nix zu futtern heute :(")
  154. def echo(bot, update):
  155. """Echo the user message."""
  156. update.message.reply_text(update.message.text)
  157. def error(bot, update, error):
  158. """Log Errors caused by Updates."""
  159. logger.warning('Update "%s" caused error "%s"', update, error)
  160. def main():
  161. """Start the bot."""
  162. # Create the EventHandler and pass it your bot's token.
  163. token = open("token").read()
  164. updater = Updater(token.strip())
  165. # Get the dispatcher to register handlers
  166. dp = updater.dispatcher
  167. # on different commands - answer in Telegram
  168. dp.add_handler(CommandHandler("start", start))
  169. dp.add_handler(CommandHandler("help", help))
  170. dp.add_handler(CommandHandler("magie", magie))
  171. dp.add_handler(CommandHandler("manta", manta))
  172. dp.add_handler(CommandHandler("mensa", mensa, pass_args=True))
  173. # log all errors
  174. dp.add_error_handler(error)
  175. # Start the Bot
  176. updater.start_polling()
  177. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  178. # SIGTERM or SIGABRT. This should be used most of the time, since
  179. # start_polling() is non-blocking and will stop the bot gracefully.
  180. updater.idle()
  181. if __name__ == '__main__':
  182. main()