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.

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