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.

214 lines
5.6 KiB

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('Help!')
  103. def magie(bot, update):
  104. """MAGIEEEEEE"""
  105. rand = random.SystemRandom()
  106. update.message.reply_text(STOLL[rand.randrange(0, len(STOLL), 1)])
  107. def manta(bot, update):
  108. """Boah ey, geile Karre!"""
  109. rand = random.SystemRandom()
  110. update.message.reply_text(MANTA[rand.randrange(0, len(MANTA), 1)])
  111. def mensa(bot, update, args):
  112. which = "zentral"
  113. categories = CATEGORIES_LIGHT
  114. for arg in args:
  115. if arg in MENSA_NAME:
  116. which = args[0]
  117. elif arg in MODES:
  118. categories = MODES[arg]
  119. if which not in MENSA_NAME:
  120. update.message.reply_text("Diese Mensa gibt es nicht! RTFM und versuchs nochmal.")
  121. return
  122. today = datetime.datetime.now().date().weekday() + 1
  123. today = 2
  124. if datetime.datetime.now().time() > datetime.time(hour=16):
  125. # Es ist zu spät am Tag, zeig das essen für morgen an
  126. today += 1
  127. today = today % 7
  128. url = "https://mensa.schneider-hosting.de/static/%s.%d.json" % (MENSA_URL[which], today)
  129. request = requests.get(url)
  130. request.encoding = 'utf-8'
  131. data = request.json()
  132. message = "Das Essen für %s in der %s \n" % (data["date"], MENSA_NAME[which])
  133. if len(data["meals"]) > 0:
  134. for meal in data["meals"]:
  135. if meal["category"] in categories:
  136. meal_line = "*%s*\n" % meal["category"]
  137. meal_line += meal["title"].strip() + "\n"
  138. # Discord only allows up to 2000 chars per message
  139. if len(message) + len(meal_line) > 2000:
  140. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  141. message = meal_line + "\n"
  142. else:
  143. message += meal_line + "\n"
  144. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  145. else:
  146. update.message.reply_text("Nix zu futtern heute :(")
  147. def echo(bot, update):
  148. """Echo the user message."""
  149. update.message.reply_text(update.message.text)
  150. def error(bot, update, error):
  151. """Log Errors caused by Updates."""
  152. logger.warning('Update "%s" caused error "%s"', update, error)
  153. def main():
  154. """Start the bot."""
  155. # Create the EventHandler and pass it your bot's token.
  156. token = open("token").read()
  157. updater = Updater(token.strip())
  158. # Get the dispatcher to register handlers
  159. dp = updater.dispatcher
  160. # on different commands - answer in Telegram
  161. dp.add_handler(CommandHandler("start", start))
  162. dp.add_handler(CommandHandler("help", help))
  163. dp.add_handler(CommandHandler("magie", magie))
  164. dp.add_handler(CommandHandler("manta", manta))
  165. dp.add_handler(CommandHandler("mensa", mensa, pass_args=True))
  166. # log all errors
  167. dp.add_error_handler(error)
  168. # Start the Bot
  169. updater.start_polling()
  170. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  171. # SIGTERM or SIGABRT. This should be used most of the time, since
  172. # start_polling() is non-blocking and will stop the bot gracefully.
  173. updater.idle()
  174. if __name__ == '__main__':
  175. main()