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.

249 lines
7.4 KiB

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