Telegram version of schneiderbot. I'll totally do something w/ this, this is never going to be effectively a mirror.
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.

220 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. if datetime.datetime.now().time() > datetime.time(hour=16):
  131. # Es ist zu spät am Tag, zeig das essen für morgen an
  132. today += 1
  133. today = today % 7
  134. url = "https://mensa.schneider-hosting.de/static/%s.%d.json" % (MENSA_URL[which], today)
  135. request = requests.get(url)
  136. request.encoding = 'utf-8'
  137. data = request.json()
  138. message = "Das Essen für %s in der %s \n" % (data["date"], MENSA_NAME[which])
  139. if len(data["meals"]) > 0:
  140. for meal in data["meals"]:
  141. if meal["category"] in categories:
  142. meal_line = "*%s*\n" % meal["category"]
  143. meal_line += meal["title"].strip() + "\n"
  144. # Discord only allows up to 2000 chars per message
  145. if len(message) + len(meal_line) > 2000:
  146. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  147. message = meal_line + "\n"
  148. else:
  149. message += meal_line + "\n"
  150. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  151. else:
  152. update.message.reply_text("Nix zu futtern heute :(")
  153. def echo(bot, update):
  154. """Echo the user message."""
  155. update.message.reply_text(update.message.text)
  156. def error(bot, update, error):
  157. """Log Errors caused by Updates."""
  158. logger.warning('Update "%s" caused error "%s"', update, error)
  159. def main():
  160. """Start the bot."""
  161. # Create the EventHandler and pass it your bot's token.
  162. token = open("token").read()
  163. updater = Updater(token.strip())
  164. # Get the dispatcher to register handlers
  165. dp = updater.dispatcher
  166. # on different commands - answer in Telegram
  167. dp.add_handler(CommandHandler("start", start))
  168. dp.add_handler(CommandHandler("help", help))
  169. dp.add_handler(CommandHandler("magie", magie))
  170. dp.add_handler(CommandHandler("manta", manta))
  171. dp.add_handler(CommandHandler("mensa", mensa, pass_args=True))
  172. # log all errors
  173. dp.add_error_handler(error)
  174. # Start the Bot
  175. updater.start_polling()
  176. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  177. # SIGTERM or SIGABRT. This should be used most of the time, since
  178. # start_polling() is non-blocking and will stop the bot gracefully.
  179. updater.idle()
  180. if __name__ == '__main__':
  181. main()