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.

264 lines
8.0 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
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
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):
  109. """kill me pls"""
  110. update.message.reply_text(update.message.from_user.first_name + " died")
  111. def revive(bot, update):
  112. """unkill me pls"""
  113. update.message.reply_text(update.message.from_user.first_name + " is no longer dead!")
  114. def sendCatDog(bot, update):
  115. """Best of both worlds!"""
  116. catdog = "https://upload.wikimedia.org/wikipedia/en/6/64/CatDog.jpeg"
  117. bot.send_photo(chat_id=update.message.chat_id, photo=catdog)
  118. def magie(bot, update):
  119. """MAGIEEEEEE"""
  120. rand = random.SystemRandom()
  121. update.message.reply_text(STOLL[rand.randrange(0, len(STOLL), 1)])
  122. def manta(bot, update):
  123. """Boah ey, geile Karre!"""
  124. rand = random.SystemRandom()
  125. update.message.reply_text(MANTA[rand.randrange(0, len(MANTA), 1)])
  126. def goodlife(bot, update):
  127. """GOOD LIFE MY A..."""
  128. rand = random.SystemRandom()
  129. update.message.reply_text(GOOD_LIFE[rand.randrange(0, len(GOOD_LIFE), 1)])
  130. def shrug(bot, update):
  131. """SHRUG"""
  132. update.message.reply_text("¯\_(ツ)_/¯")
  133. def simon(bot, update):
  134. "KAUF DIR N HUND"
  135. update.message.reply_text("@Justus_vonRamme bitte kauf dir einen Hund oder eine Katze, du hast zu viel Zeit")
  136. def mensa(bot, update, args):
  137. which = "zentral"
  138. filter_categories = MODES["light"]
  139. diet = ""
  140. today = datetime.datetime.now().date().weekday() + 1
  141. if datetime.datetime.now().time() > datetime.time(hour=16):
  142. # Es ist zu spät am Tag, zeig das essen für morgen an
  143. today += 1
  144. today = today % 7
  145. for arg in args:
  146. if arg in MENSA_NAME:
  147. which = arg
  148. elif arg in MODES:
  149. filter_categories = MODES[arg]
  150. elif arg in WEEKDAYS:
  151. today = WEEKDAYS[arg]
  152. elif arg in DIETS:
  153. diet = DIETS[arg]
  154. else:
  155. update.message.reply_text("Falscher Aufruf! RTFM und versuchs nochmal.")
  156. return
  157. if today == 0:
  158. update.message.reply_text("Sonntags hat die Mensa zu")
  159. return
  160. url = "https://mensa.schneider-hosting.de/static/%s.%d.json" % (MENSA_URL[which], today)
  161. request = requests.get(url)
  162. request.encoding = 'utf-8'
  163. data = request.json()
  164. message = "Das Essen für %s in der %s \n\n" % (data["date"], MENSA_NAME[which])
  165. if len(data["meals"]) > 1:
  166. for meal in data["meals"]:
  167. if meal["category"] not in filter_categories and diet in meal["diet"]:
  168. meal_line = "*%s*\n" % meal["category"]
  169. meal_line += meal["title"].strip() + "\n"
  170. # Discord only allows up to 2000 chars per message
  171. if len(message) + len(meal_line) > 2000:
  172. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  173. message = meal_line + "\n"
  174. else:
  175. message += meal_line + "\n"
  176. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  177. else:
  178. update.message.reply_text("Nix zu futtern heute :(")
  179. def echo(bot, update):
  180. """Echo the user message."""
  181. update.message.reply_text(update.message.text)
  182. def error(bot, update, error):
  183. """Log Errors caused by Updates."""
  184. logger.warning('Update "%s" caused error "%s"', update, error)
  185. def main():
  186. """Start the bot."""
  187. # Create the EventHandler and pass it your bot's token.
  188. token = open("token").read()
  189. updater = Updater(token.strip())
  190. # Get the dispatcher to register handlers
  191. dp = updater.dispatcher
  192. # on different commands - answer in Telegram
  193. dp.add_handler(CommandHandler("start", start))
  194. dp.add_handler(CommandHandler("help", help))
  195. dp.add_handler(CommandHandler("magie", magie))
  196. dp.add_handler(CommandHandler("manta", manta))
  197. dp.add_handler(CommandHandler("goodlife", goodlife))
  198. dp.add_handler(CommandHandler("mensa", mensa, pass_args=True))
  199. dp.add_handler(CommandHandler("cat", sendCat))
  200. dp.add_handler(CommandHandler("dog", sendDog))
  201. dp.add_handler(CommandHandler("catdog", sendCatDog))
  202. dp.add_handler(CommandHandler("shrug", shrug))
  203. dp.add_handler(CommandHandler("kill", kill))
  204. dp.add_handler(CommandHandler("revive", revive))
  205. dp.add_handler(CommandHandler("simon", simon))
  206. # log all errors
  207. dp.add_error_handler(error)
  208. # Start the Bot
  209. updater.start_polling()
  210. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  211. # SIGTERM or SIGABRT. This should be used most of the time, since
  212. # start_polling() is non-blocking and will stop the bot gracefully.
  213. updater.idle()
  214. if __name__ == '__main__':
  215. main()