Telegram version of schneiderbot. I'll totally do something w/ this, this is never going to be effectively a mirror.
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.

244 lines
7.2 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. "italia": "mensaitalia"
  30. "fasthochschule": "bistrohawk"
  31. }
  32. MENSA_NAME = {
  33. "zentral": "Zentralmensa",
  34. "nord": "Nordmensa",
  35. "turm": "Turmmensa"
  36. "italia": "Mensa Italia"
  37. "hawk": "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 mensa(bot, update, args):
  124. which = "zentral"
  125. filter_categories = MODES["light"]
  126. diet = ""
  127. today = datetime.datetime.now().date().weekday() + 1
  128. if datetime.datetime.now().time() > datetime.time(hour=16):
  129. # Es ist zu spät am Tag, zeig das essen für morgen an
  130. today += 1
  131. today = today % 7
  132. for arg in args:
  133. if arg in MENSA_NAME:
  134. which = arg
  135. elif arg in MODES:
  136. filter_categories = MODES[arg]
  137. elif arg in WEEKDAYS:
  138. today = WEEKDAYS[arg]
  139. elif arg in DIETS:
  140. diet = DIETS[arg]
  141. else:
  142. update.message.reply_text("Falscher Aufruf! RTFM und versuchs nochmal.")
  143. return
  144. if today == 0:
  145. update.message.reply_text("Sonntags hat die Mensa zu")
  146. return
  147. url = "https://mensa.schneider-hosting.de/static/%s.%d.json" % (MENSA_URL[which], today)
  148. request = requests.get(url)
  149. request.encoding = 'utf-8'
  150. data = request.json()
  151. message = "Das Essen für %s in der %s \n\n" % (data["date"], MENSA_NAME[which])
  152. if len(data["meals"]) > 1:
  153. for meal in data["meals"]:
  154. if meal["category"] not in filter_categories and diet in meal["diet"]:
  155. meal_line = "*%s*\n" % meal["category"]
  156. meal_line += meal["title"].strip() + "\n"
  157. # Discord only allows up to 2000 chars per message
  158. if len(message) + len(meal_line) > 2000:
  159. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  160. message = meal_line + "\n"
  161. else:
  162. message += meal_line + "\n"
  163. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  164. else:
  165. update.message.reply_text("Nix zu futtern heute :(")
  166. def echo(bot, update):
  167. """Echo the user message."""
  168. update.message.reply_text(update.message.text)
  169. def error(bot, update, error):
  170. """Log Errors caused by Updates."""
  171. logger.warning('Update "%s" caused error "%s"', update, error)
  172. def main():
  173. """Start the bot."""
  174. # Create the EventHandler and pass it your bot's token.
  175. token = open("token").read()
  176. updater = Updater(token.strip())
  177. # Get the dispatcher to register handlers
  178. dp = updater.dispatcher
  179. # on different commands - answer in Telegram
  180. dp.add_handler(CommandHandler("start", start))
  181. dp.add_handler(CommandHandler("help", help))
  182. dp.add_handler(CommandHandler("magie", magie))
  183. dp.add_handler(CommandHandler("manta", manta))
  184. dp.add_handler(CommandHandler("mensa", mensa, pass_args=True))
  185. dp.add_handler(CommandHandler("cat", sendCat))
  186. dp.add_handler(CommandHandler("dog", sendDog))
  187. dp.add_handler(CommandHandler("catdog", sendCatDog))
  188. dp.add_handler(CommandHandler("shrug", shrug))
  189. dp.add_handler(CommandHandler("kill", kill))
  190. # log all errors
  191. dp.add_error_handler(error)
  192. # Start the Bot
  193. updater.start_polling()
  194. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  195. # SIGTERM or SIGABRT. This should be used most of the time, since
  196. # start_polling() is non-blocking and will stop the bot gracefully.
  197. updater.idle()
  198. if __name__ == '__main__':
  199. main()