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.

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