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