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.

214 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
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 kill(bot, update):
  89. """kill me pls"""
  90. update.message.reply_text(update.message.from_user.first_name + " died")
  91. def magie(bot, update):
  92. """MAGIEEEEEE"""
  93. rand = random.SystemRandom()
  94. update.message.reply_text(STOLL[rand.randrange(0, len(STOLL), 1)])
  95. def manta(bot, update):
  96. """Boah ey, geile Karre!"""
  97. rand = random.SystemRandom()
  98. update.message.reply_text(MANTA[rand.randrange(0, len(MANTA), 1)])
  99. def shrug(bot, update):
  100. """SHRUG"""
  101. update.message.reply_text("¯\_(ツ)_/¯")
  102. def mensa(bot, update, args):
  103. which = "zentral"
  104. filter_categories = MODES["light"]
  105. for arg in args:
  106. if arg in MENSA_NAME:
  107. which = args[0]
  108. elif arg in MODES:
  109. filter_categories = MODES[arg]
  110. else:
  111. update.message.reply_text("Falscher Aufruf! RTFM und versuchs nochmal.")
  112. return
  113. today = datetime.datetime.now().date().weekday() + 1
  114. if datetime.datetime.now().time() > datetime.time(hour=16):
  115. # Es ist zu spät am Tag, zeig das essen für morgen an
  116. today += 1
  117. today = today % 7
  118. url = "https://mensa.schneider-hosting.de/static/%s.%d.json" % (MENSA_URL[which], today)
  119. request = requests.get(url)
  120. request.encoding = 'utf-8'
  121. data = request.json()
  122. message = "Das Essen für %s in der %s \n\n" % (data["date"], MENSA_NAME[which])
  123. if len(data["meals"]) > 1:
  124. for meal in data["meals"]:
  125. if meal["category"] not in filter_categories:
  126. meal_line = "*%s*\n" % meal["category"]
  127. meal_line += meal["title"].strip() + "\n"
  128. # Discord only allows up to 2000 chars per message
  129. if len(message) + len(meal_line) > 2000:
  130. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  131. message = meal_line + "\n"
  132. else:
  133. message += meal_line + "\n"
  134. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  135. else:
  136. update.message.reply_text("Nix zu futtern heute :(")
  137. def echo(bot, update):
  138. """Echo the user message."""
  139. update.message.reply_text(update.message.text)
  140. def error(bot, update, error):
  141. """Log Errors caused by Updates."""
  142. logger.warning('Update "%s" caused error "%s"', update, error)
  143. def main():
  144. """Start the bot."""
  145. # Create the EventHandler and pass it your bot's token.
  146. token = open("token").read()
  147. updater = Updater(token.strip())
  148. # Get the dispatcher to register handlers
  149. dp = updater.dispatcher
  150. # on different commands - answer in Telegram
  151. dp.add_handler(CommandHandler("start", start))
  152. dp.add_handler(CommandHandler("help", help))
  153. dp.add_handler(CommandHandler("magie", magie))
  154. dp.add_handler(CommandHandler("manta", manta))
  155. dp.add_handler(CommandHandler("mensa", mensa, pass_args=True))
  156. dp.add_handler(CommandHandler("cat", sendCat))
  157. dp.add_handler(CommandHandler("dog", sendDog))
  158. dp.add_handler(CommandHandler("shrug", shrug))
  159. dp.add_handler(CommandHandler("kill", kill))
  160. # log all errors
  161. dp.add_error_handler(error)
  162. # Start the Bot
  163. updater.start_polling()
  164. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  165. # SIGTERM or SIGABRT. This should be used most of the time, since
  166. # start_polling() is non-blocking and will stop the bot gracefully.
  167. updater.idle()
  168. if __name__ == '__main__':
  169. main()