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.

313 lines
9.6 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
6 years ago
6 years ago
7 years ago
7 years ago
7 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
7 years ago
6 years ago
6 years ago
7 years ago
6 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
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
  4. from telegram import ParseMode
  5. from db import Db
  6. import logging
  7. import os
  8. import random
  9. import requests
  10. import datetime
  11. import cat
  12. import pydog
  13. DIETS = {
  14. "veggy" : "fleischlos",
  15. "fleisch" : "mit Fleisch",
  16. "fisch" : "mit Fisch/ Meeresfrüchten"
  17. }
  18. WEEKDAYS = {
  19. "montag" : 1,
  20. "dienstag" : 2,
  21. "mittwoch" : 3,
  22. "donnerstag" : 4,
  23. "freitag" : 5,
  24. "samstag" : 6
  25. }
  26. MENSA_URL = {
  27. "zentral": "zentralmensa",
  28. "nord": "nordmensa",
  29. "turm": "turmmensa",
  30. "italia": "mensaitalia",
  31. "fasthochschule": "bistrohawk"
  32. }
  33. MENSA_NAME = {
  34. "zentral": "Zentralmensa",
  35. "nord": "Nordmensa",
  36. "turm": "Turmmensa",
  37. "italia": "Mensa Italia",
  38. "fasthochschule": "Bistro Fasthochschule"
  39. }
  40. HIDE_CATEGORIES_LIGHT = {
  41. "CampusCurry",
  42. "natürlich fit",
  43. "Fitnesscenter",
  44. "Salatbuffet",
  45. "Studentenfutter",
  46. "Süße Versuchung",
  47. "Süße Spezial Tagesangebot",
  48. "Vollwert & Co. Stärke",
  49. "Vollwert & Co. Gemüse",
  50. "Bio-Beilagen",
  51. "Dessertbuffet",
  52. "Last Minute ab 14:30 Uhr",
  53. ## Nordmensa:
  54. "Salatbuffet/Pastapoint",
  55. "Last Minute ab 13:30 Uhr",
  56. ## Turmmensa:
  57. "Beilagen",
  58. "Last Minute ab 14:00Uhr"
  59. }
  60. HIDE_CATEGORIES_FULL = {
  61. "Last Minute ab 14:30 Uhr",
  62. "Last Minute ab 13:30 Uhr",
  63. "Last Minute ab 14:00Uhr"
  64. }
  65. MODES = {
  66. "light" : HIDE_CATEGORIES_LIGHT,
  67. "full" : HIDE_CATEGORIES_FULL
  68. }
  69. # Enable logging
  70. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  71. level=logging.INFO)
  72. logger = logging.getLogger(__name__)
  73. with open("stoll.txt", "r") as tmp_file:
  74. STOLL = tmp_file.readlines()
  75. with open("manta.txt", "r") as tmp_file:
  76. MANTA = tmp_file.readlines()
  77. with open("goodlife.txt", "r") as tmp_file:
  78. GOOD_LIFE = tmp_file.readlines()
  79. # Define a few command handlers. These usually take the two arguments bot and
  80. # update. Error handlers also receive the raised TelegramError object in error.
  81. def start(bot, update):
  82. """Was ist das hier für eins Bot?"""
  83. update.message.reply_text("""Hallo, ich bin ein Bot.
  84. Ich liefere tolle Informationen über die Mensa. Die Infos kommen von
  85. https://mensa.schneider-hosting.de
  86. Mhmm, lecker. Guten Appetit!""")
  87. def help(bot, update):
  88. """Send a message when the command /help is issued."""
  89. update.message.reply_text("""Commands:
  90. *Mensa*:
  91. /mensa _mensa_ - prints filtered list of meals for _mensa_ (if no _mensa_ given, zentralmensa is used)
  92. /mensa _mensa_ full - prints full list of meals for _mensa_ (if no _mensa_ given, zentralmensa is used)
  93. *Fun*:
  94. /cat - random cat (using thecatapi.com)
  95. /dog - random dog (using dog.ceo/dog-api)
  96. /magie - random Axel Stoll quote
  97. /manta - random Opel Manta joke
  98. /goodlife - random good life quote
  99. /shrug - prints ¯\_()_/¯
  100. /kill - kills you
  101. /revive - revives you
  102. /sudo kill/revive - force kill/revive
  103. /graveyard - shows the dead people
  104. /help - this help text""", parse_mode=ParseMode.MARKDOWN)
  105. def sendCat(bot, update):
  106. """CUTE"""
  107. catfile = cat.getCat();
  108. bot.send_photo(chat_id=update.message.chat_id, photo=open(catfile, 'rb'))
  109. os.remove(catfile)
  110. def sendDog(bot, update):
  111. """CUTE 2.0"""
  112. dog = pydog.PyDog()
  113. bot.send_photo(chat_id=update.message.chat_id, photo=dog.get_random_image())
  114. def kill(bot, update, sudocall = False):
  115. """kill me pls"""
  116. db = Db()
  117. user = update.message.from_user.first_name
  118. if db.is_dead(user, update.message.chat_id):
  119. if sudocall:
  120. update.message.reply_text("%s killed again" % user)
  121. return
  122. update.message.reply_text("%s is already dead" % user)
  123. return
  124. message = update.message.from_user.first_name + " died"
  125. if sudocall:
  126. message += " for real"
  127. db.kill(user, update.message.chat_id)
  128. update.message.reply_text(message)
  129. def revive(bot, update, sudocall = False):
  130. """unkill me pls"""
  131. db = Db()
  132. user = update.message.from_user.first_name
  133. if not db.is_dead(user, update.message.chat_id):
  134. update.message.reply_text("Maybe %s should have a litte 'accident' before" % user)
  135. return
  136. if sudocall:
  137. update.message.reply_text("%s is living again, for real" % user)
  138. db.revive(user, update.message.chat_id)
  139. return
  140. update.message.reply_text("You fool! You cannot revive a dead person!")
  141. SUDOCMDS = {
  142. "kill" : kill,
  143. "revive" : revive
  144. }
  145. def graveyard(bot, update):
  146. """List the dead"""
  147. db = Db()
  148. update.message.reply_text("Here are the dead people:\n%s" % db.get_dead_bodies(update.message.chat_id))
  149. def sudo(bot, update, args):
  150. """for real"""
  151. if not args :
  152. update.message.reply_text("Unknown command")
  153. return
  154. for arg in args:
  155. if arg in SUDOCMDS:
  156. SUDOCMDS[arg](bot, update, True)
  157. else:
  158. update.message.reply_text("Unknown command")
  159. def sendCatDog(bot, update):
  160. """Best of both worlds!"""
  161. catdog = "https://upload.wikimedia.org/wikipedia/en/6/64/CatDog.jpeg"
  162. bot.send_photo(chat_id=update.message.chat_id, photo=catdog)
  163. def magie(bot, update):
  164. """MAGIEEEEEE"""
  165. rand = random.SystemRandom()
  166. update.message.reply_text(STOLL[rand.randrange(0, len(STOLL), 1)])
  167. def manta(bot, update):
  168. """Boah ey, geile Karre!"""
  169. rand = random.SystemRandom()
  170. update.message.reply_text(MANTA[rand.randrange(0, len(MANTA), 1)])
  171. def goodlife(bot, update):
  172. """GOOD LIFE MY A..."""
  173. rand = random.SystemRandom()
  174. update.message.reply_text(GOOD_LIFE[rand.randrange(0, len(GOOD_LIFE), 1)])
  175. def shrug(bot, update):
  176. """SHRUG"""
  177. update.message.reply_text("¯\_(ツ)_/¯")
  178. def simon(bot, update):
  179. "KAUF DIR N HUND"
  180. update.message.reply_text("@Justus_vonRamme bitte kauf dir einen Hund oder eine Katze, du hast zu viel Zeit")
  181. def mensa(bot, update, args):
  182. which = "zentral"
  183. filter_categories = MODES["light"]
  184. diet = ""
  185. today = datetime.datetime.now().date().weekday() + 1
  186. if datetime.datetime.now().time() > datetime.time(hour=16):
  187. # Es ist zu spät am Tag, zeig das essen für morgen an
  188. today += 1
  189. today = today % 7
  190. for arg in args:
  191. arg = arg.lower()
  192. if arg in MENSA_NAME:
  193. which = arg
  194. elif arg in MODES:
  195. filter_categories = MODES[arg]
  196. elif arg in WEEKDAYS:
  197. today = WEEKDAYS[arg]
  198. elif arg in DIETS:
  199. diet = DIETS[arg]
  200. else:
  201. update.message.reply_text("Falscher Aufruf! RTFM und versuchs nochmal.")
  202. return
  203. if today == 0:
  204. update.message.reply_text("Sonntags hat die Mensa zu")
  205. return
  206. url = "https://mensa.schneider-hosting.de/static/%s.%d.json" % (MENSA_URL[which], today)
  207. request = requests.get(url)
  208. request.encoding = 'utf-8'
  209. data = request.json()
  210. message = "Das Essen für %s in der %s \n\n" % (data["date"], MENSA_NAME[which])
  211. if len(data["meals"]) > 1:
  212. for meal in data["meals"]:
  213. if meal["category"] not in filter_categories and diet in meal["diet"]:
  214. meal_line = "*%s*\n" % meal["category"]
  215. meal_line += meal["title"].strip() + "\n"
  216. # Discord only allows up to 2000 chars per message
  217. if len(message) + len(meal_line) > 2000:
  218. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  219. message = meal_line + "\n"
  220. else:
  221. message += meal_line + "\n"
  222. update.message.reply_text(message, parse_mode=ParseMode.MARKDOWN)
  223. else:
  224. update.message.reply_text("Nix zu futtern heute :(")
  225. def echo(bot, update):
  226. """Echo the user message."""
  227. update.message.reply_text(update.message.text)
  228. def error(bot, update, error):
  229. """Log Errors caused by Updates."""
  230. logger.warning('Update "%s" caused error "%s"', update, error)
  231. def main():
  232. """Start the bot."""
  233. # Create the EventHandler and pass it your bot's token.
  234. token = open("token").read()
  235. updater = Updater(token.strip())
  236. # Get the dispatcher to register handlers
  237. dp = updater.dispatcher
  238. # on different commands - answer in Telegram
  239. dp.add_handler(CommandHandler("start", start))
  240. dp.add_handler(CommandHandler("help", help))
  241. dp.add_handler(CommandHandler("magie", magie))
  242. dp.add_handler(CommandHandler("manta", manta))
  243. dp.add_handler(CommandHandler("goodlife", goodlife))
  244. dp.add_handler(CommandHandler("mensa", mensa, pass_args=True))
  245. dp.add_handler(CommandHandler("sudo", sudo, pass_args=True))
  246. dp.add_handler(CommandHandler("cat", sendCat))
  247. dp.add_handler(CommandHandler("dog", sendDog))
  248. dp.add_handler(CommandHandler("catdog", sendCatDog))
  249. dp.add_handler(CommandHandler("shrug", shrug))
  250. dp.add_handler(CommandHandler("kill", kill))
  251. dp.add_handler(CommandHandler("revive", revive))
  252. dp.add_handler(CommandHandler("simon", simon))
  253. dp.add_handler(CommandHandler("graveyard", graveyard))
  254. # log all errors
  255. dp.add_error_handler(error)
  256. # Start the Bot
  257. updater.start_polling()
  258. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  259. # SIGTERM or SIGABRT. This should be used most of the time, since
  260. # start_polling() is non-blocking and will stop the bot gracefully.
  261. updater.idle()
  262. if __name__ == '__main__':
  263. main()