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.

286 lines
8.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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. with open("goodlife.txt", "r") as tmp_file:
  77. GOOD_LIFE = tmp_file.readlines()
  78. # Define a few command handlers. These usually take the two arguments bot and
  79. # update. Error handlers also receive the raised TelegramError object in error.
  80. def start(bot, update):
  81. """Was ist das hier für eins Bot?"""
  82. update.message.reply_text("""Hallo, ich bin ein Bot.
  83. Ich liefere tolle Informationen über die Mensa. Die Infos kommen von
  84. https://mensa.schneider-hosting.de
  85. Mhmm, lecker. Guten Appetit!""")
  86. def help(bot, update):
  87. """Send a message when the command /help is issued."""
  88. update.message.reply_text("""Commands:
  89. *Mensa*:
  90. /mensa _mensa_ - prints filtered list of meals for _mensa_ (if no _mensa_ given, zentralmensa is used)
  91. /mensa _mensa_ full - prints full list of meals for _mensa_ (if no _mensa_ given, zentralmensa is used)
  92. *Fun*:
  93. /cat - random cat (using thecatapi.com)
  94. /dog - random dog (https://dog.ceo/dog-api)
  95. /magie - random Axel Stoll quote
  96. /manta - random Opel Manta joke
  97. /goodlife - random good life quote
  98. /help - this help text""", parse_mode=ParseMode.MARKDOWN)
  99. def sendCat(bot, update):
  100. """CUTE"""
  101. catfile = cat.getCat();
  102. bot.send_photo(chat_id=update.message.chat_id, photo=open(catfile, 'rb'))
  103. os.remove(catfile)
  104. def sendDog(bot, update):
  105. """CUTE 2.0"""
  106. dog = pydog.PyDog()
  107. bot.send_photo(chat_id=update.message.chat_id, photo=dog.get_random_image())
  108. def kill(bot, update, sudocall = False):
  109. """kill me pls"""
  110. message = update.message.from_user.first_name + " died"
  111. if sudocall:
  112. message += " for real"
  113. update.message.reply_text(message)
  114. def revive(bot, update, sudocall = False):
  115. """unkill me pls"""
  116. message = update.message.from_user.first_name + " is living again"
  117. if sudocall:
  118. message += " for real"
  119. update.message.reply_text(message)
  120. SUDOCMDS = {
  121. "kill" : kill,
  122. "revive" : revive
  123. }
  124. def sudo(bot, update, args):
  125. """for real"""
  126. if not args :
  127. update.message.reply_text("Unknown command")
  128. return
  129. for arg in args:
  130. if arg in SUDOCMDS:
  131. SUDOCMDS[arg](bot, update, True)
  132. else:
  133. update.message.reply_text("Unknown command")
  134. def sendCatDog(bot, update):
  135. """Best of both worlds!"""
  136. catdog = "https://upload.wikimedia.org/wikipedia/en/6/64/CatDog.jpeg"
  137. bot.send_photo(chat_id=update.message.chat_id, photo=catdog)
  138. def magie(bot, update):
  139. """MAGIEEEEEE"""
  140. rand = random.SystemRandom()
  141. update.message.reply_text(STOLL[rand.randrange(0, len(STOLL), 1)])
  142. def manta(bot, update):
  143. """Boah ey, geile Karre!"""
  144. rand = random.SystemRandom()
  145. update.message.reply_text(MANTA[rand.randrange(0, len(MANTA), 1)])
  146. def goodlife(bot, update):
  147. """GOOD LIFE MY A..."""
  148. rand = random.SystemRandom()
  149. update.message.reply_text(GOOD_LIFE[rand.randrange(0, len(GOOD_LIFE), 1)])
  150. def shrug(bot, update):
  151. """SHRUG"""
  152. update.message.reply_text("¯\_(ツ)_/¯")
  153. def simon(bot, update):
  154. "KAUF DIR N HUND"
  155. update.message.reply_text("@Justus_vonRamme bitte kauf dir einen Hund oder eine Katze, du hast zu viel Zeit")
  156. def mensa(bot, update, args):
  157. which = "zentral"
  158. filter_categories = MODES["light"]
  159. diet = ""
  160. today = datetime.datetime.now().date().weekday() + 1
  161. if datetime.datetime.now().time() > datetime.time(hour=16):
  162. # Es ist zu spät am Tag, zeig das essen für morgen an
  163. today += 1
  164. today = today % 7
  165. for arg in args:
  166. if arg in MENSA_NAME:
  167. which = arg
  168. elif arg in MODES:
  169. filter_categories = MODES[arg]
  170. elif arg in WEEKDAYS:
  171. today = WEEKDAYS[arg]
  172. elif arg in DIETS:
  173. diet = DIETS[arg]
  174. else:
  175. update.message.reply_text("Falscher Aufruf! RTFM und versuchs nochmal.")
  176. return
  177. if today == 0:
  178. update.message.reply_text("Sonntags hat die Mensa zu")
  179. return
  180. url = "https://mensa.schneider-hosting.de/static/%s.%d.json" % (MENSA_URL[which], today)
  181. request = requests.get(url)
  182. request.encoding = 'utf-8'
  183. data = request.json()
  184. message = "Das Essen für %s in der %s \n\n" % (data["date"], MENSA_NAME[which])
  185. if len(data["meals"]) > 1:
  186. for meal in data["meals"]:
  187. if meal["category"] not in filter_categories and diet in meal["diet"]:
  188. meal_line = "*%s*\n" % meal["category"]
  189. meal_line += meal["title"].strip() + "\n"
  190. # Discord only allows up to 2000 chars per message
  191. if len(message) + len(meal_line) > 2000:
  192. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  193. message = meal_line + "\n"
  194. else:
  195. message += meal_line + "\n"
  196. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  197. else:
  198. update.message.reply_text("Nix zu futtern heute :(")
  199. def echo(bot, update):
  200. """Echo the user message."""
  201. update.message.reply_text(update.message.text)
  202. def error(bot, update, error):
  203. """Log Errors caused by Updates."""
  204. logger.warning('Update "%s" caused error "%s"', update, error)
  205. def main():
  206. """Start the bot."""
  207. # Create the EventHandler and pass it your bot's token.
  208. token = open("token").read()
  209. updater = Updater(token.strip())
  210. # Get the dispatcher to register handlers
  211. dp = updater.dispatcher
  212. # on different commands - answer in Telegram
  213. dp.add_handler(CommandHandler("start", start))
  214. dp.add_handler(CommandHandler("help", help))
  215. dp.add_handler(CommandHandler("magie", magie))
  216. dp.add_handler(CommandHandler("manta", manta))
  217. dp.add_handler(CommandHandler("goodlife", goodlife))
  218. dp.add_handler(CommandHandler("mensa", mensa, pass_args=True))
  219. dp.add_handler(CommandHandler("sudo", sudo, pass_args=True))
  220. dp.add_handler(CommandHandler("cat", sendCat))
  221. dp.add_handler(CommandHandler("dog", sendDog))
  222. dp.add_handler(CommandHandler("catdog", sendCatDog))
  223. dp.add_handler(CommandHandler("shrug", shrug))
  224. dp.add_handler(CommandHandler("kill", kill))
  225. dp.add_handler(CommandHandler("revive", revive))
  226. dp.add_handler(CommandHandler("simon", simon))
  227. # log all errors
  228. dp.add_error_handler(error)
  229. # Start the Bot
  230. updater.start_polling()
  231. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  232. # SIGTERM or SIGABRT. This should be used most of the time, since
  233. # start_polling() is non-blocking and will stop the bot gracefully.
  234. updater.idle()
  235. if __name__ == '__main__':
  236. main()