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.

240 lines
6.4 KiB

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