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.

239 lines
6.7 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. 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 = args[0]
  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. url = "https://mensa.schneider-hosting.de/static/%s.%d.json" % (MENSA_URL[which], today)
  141. request = requests.get(url)
  142. request.encoding = 'utf-8'
  143. data = request.json()
  144. message = "Das Essen für %s in der %s \n\n" % (data["date"], MENSA_NAME[which])
  145. if len(data["meals"]) > 1:
  146. for meal in data["meals"]:
  147. if meal["category"] not in filter_categories and diet in meal["diet"]:
  148. meal_line = "*%s*\n" % meal["category"]
  149. meal_line += meal["title"].strip() + "\n"
  150. # Discord only allows up to 2000 chars per message
  151. if len(message) + len(meal_line) > 2000:
  152. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  153. message = meal_line + "\n"
  154. else:
  155. message += meal_line + "\n"
  156. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  157. else:
  158. update.message.reply_text("Nix zu futtern heute :(")
  159. def echo(bot, update):
  160. """Echo the user message."""
  161. update.message.reply_text(update.message.text)
  162. def error(bot, update, error):
  163. """Log Errors caused by Updates."""
  164. logger.warning('Update "%s" caused error "%s"', update, error)
  165. def main():
  166. """Start the bot."""
  167. # Create the EventHandler and pass it your bot's token.
  168. token = open("token").read()
  169. updater = Updater(token.strip())
  170. # Get the dispatcher to register handlers
  171. dp = updater.dispatcher
  172. # on different commands - answer in Telegram
  173. dp.add_handler(CommandHandler("start", start))
  174. dp.add_handler(CommandHandler("help", help))
  175. dp.add_handler(CommandHandler("magie", magie))
  176. dp.add_handler(CommandHandler("manta", manta))
  177. dp.add_handler(CommandHandler("mensa", mensa, pass_args=True))
  178. dp.add_handler(CommandHandler("cat", sendCat))
  179. dp.add_handler(CommandHandler("dog", sendDog))
  180. dp.add_handler(CommandHandler("catdog", sendCatDog))
  181. dp.add_handler(CommandHandler("shrug", shrug))
  182. dp.add_handler(CommandHandler("kill", kill))
  183. # log all errors
  184. dp.add_error_handler(error)
  185. # Start the Bot
  186. updater.start_polling()
  187. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  188. # SIGTERM or SIGABRT. This should be used most of the time, since
  189. # start_polling() is non-blocking and will stop the bot gracefully.
  190. updater.idle()
  191. if __name__ == '__main__':
  192. main()