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.

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