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.

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