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.

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