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.

224 lines
6.8 KiB

7 years ago
6 years ago
6 years ago
6 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
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 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. from mensa import mensa
  7. from coding_love import send_coding_love_gif
  8. import logging
  9. import random
  10. import requests
  11. import pydog
  12. # Enable logging
  13. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  14. level=logging.INFO)
  15. logger = logging.getLogger(__name__)
  16. with open("stoll.txt", "r") as tmp_file:
  17. STOLL = tmp_file.readlines()
  18. with open("manta.txt", "r") as tmp_file:
  19. MANTA = tmp_file.readlines()
  20. with open("goodlife.txt", "r") as tmp_file:
  21. GOOD_LIFE = tmp_file.readlines()
  22. with open("simon.txt", "r") as tmp_file:
  23. SIMON = tmp_file.readlines()
  24. # Define a few command handlers. These usually take the two arguments bot and
  25. # update. Error handlers also receive the raised TelegramError object in error.
  26. def start(bot, update):
  27. """Was ist das hier für eins Bot?"""
  28. update.message.reply_text("""Hallo, ich bin ein Bot.
  29. Ich liefere tolle Informationen über die Mensa. Die Infos kommen von
  30. https://mensa.schneider-hosting.de
  31. Mhmm, lecker. Guten Appetit!""")
  32. def help(bot, update):
  33. """Send a message when the command /help is issued."""
  34. update.message.reply_text("""Commands:
  35. *Mensa*:
  36. /mensa _mensa_ - prints filtered list of meals for _mensa_ (if no _mensa_ given, zentralmensa is used)
  37. /mensa _mensa_ full - prints full list of meals for _mensa_ (if no _mensa_ given, zentralmensa is used)
  38. *Fun*:
  39. /cat - random cat (using thecatapi.com)
  40. /dog - random dog (using dog.ceo/dog-api)
  41. /magie - random Axel Stoll quote
  42. /manta - random Opel Manta joke
  43. /goodlife - random good life quote
  44. /shrug - prints shrug
  45. /kill - kills you
  46. /revive - revives you
  47. /sudo kill/revive - force kill/revive
  48. /graveyard - shows the dead people
  49. /help - this help text""", parse_mode=ParseMode.MARKDOWN)
  50. def send_cat(bot, update):
  51. """CUTE"""
  52. cat_file = requests.get('http://aws.random.cat/meow').json()['file']
  53. bot.send_photo(chat_id=update.message.chat_id, photo=cat_file)
  54. def send_dog(bot, update):
  55. """CUTE 2.0"""
  56. dog = pydog.PyDog()
  57. bot.send_photo(chat_id=update.message.chat_id, photo=dog.get_random_image())
  58. def kill(bot, update, sudocall=False):
  59. """kill me pls"""
  60. db = Db()
  61. user = update.message.from_user.first_name
  62. if db.is_dead(user, update.message.chat_id):
  63. if sudocall:
  64. update.message.reply_text("%s killed again" % user)
  65. return
  66. update.message.reply_text("%s is already dead" % user)
  67. return
  68. message = update.message.from_user.first_name + " died"
  69. if sudocall:
  70. message += " for real"
  71. db.kill(user, update.message.chat_id)
  72. update.message.reply_text(message)
  73. def revive(bot, update, sudocall=False):
  74. """unkill me pls"""
  75. db = Db()
  76. user = update.message.from_user.first_name
  77. if not db.is_dead(user, update.message.chat_id):
  78. update.message.reply_text("Maybe %s should have a litte 'accident' before" % user)
  79. return
  80. if sudocall:
  81. update.message.reply_text("%s is living again, for real" % user)
  82. db.revive(user, update.message.chat_id)
  83. return
  84. update.message.reply_text("You fool! You cannot revive a dead person!")
  85. SUDO_CMDS = {
  86. "kill": kill,
  87. "revive": revive
  88. }
  89. def graveyard(bot, update):
  90. """List the dead"""
  91. db = Db()
  92. update.message.reply_text("Here are the dead people:\n%s" % db.get_dead_bodies(update.message.chat_id))
  93. def sudo(bot, update, args):
  94. """for real"""
  95. if not args:
  96. update.message.reply_text("Unknown command")
  97. return
  98. for arg in args:
  99. if arg in SUDO_CMDS:
  100. SUDO_CMDS[arg](bot, update, True)
  101. else:
  102. update.message.reply_text("Unknown command")
  103. def send_cat_dog(bot, update):
  104. """Best of both worlds!"""
  105. catdog = "https://upload.wikimedia.org/wikipedia/en/6/64/CatDog.jpeg"
  106. bot.send_photo(chat_id=update.message.chat_id, photo=catdog)
  107. def magie(bot, update):
  108. """MAGIEEEEEE"""
  109. rand = random.SystemRandom()
  110. update.message.reply_text(STOLL[rand.randrange(0, len(STOLL), 1)])
  111. def manta(bot, update):
  112. """Boah ey, geile Karre!"""
  113. rand = random.SystemRandom()
  114. update.message.reply_text(MANTA[rand.randrange(0, len(MANTA), 1)])
  115. def goodlife(bot, update):
  116. """GOOD LIFE MY A..."""
  117. rand = random.SystemRandom()
  118. update.message.reply_text(GOOD_LIFE[rand.randrange(0, len(GOOD_LIFE), 1)])
  119. def shrug(bot, update):
  120. """SHRUG"""
  121. update.message.reply_text("¯\_(ツ)_/¯")
  122. def simon(bot, update, args):
  123. "KAUF DIR N HUND"
  124. name = "@thedailysimon"
  125. if len(args) > 0:
  126. name = args[0]
  127. rand = random.SystemRandom()
  128. animal = SIMON[rand.randrange(0, len(SIMON), 1)].replace('\n', '')
  129. update.message.reply_text(
  130. "%s, bitte kauf dir einen Hund, eine Katze oder von mir aus auch %s, du hast zu viel Zeit"
  131. % (name, animal))
  132. def echo(bot, update):
  133. """Echo the user message."""
  134. update.message.reply_text(update.message.text)
  135. def error(bot, update, error):
  136. """Log Errors caused by Updates."""
  137. logger.warning('Update "%s" caused error "%s"', update, error)
  138. def main():
  139. """Start the bot."""
  140. # Create the EventHandler and pass it your bot's token.
  141. token = open("token").read()
  142. updater = Updater(token.strip())
  143. # Get the dispatcher to register handlers
  144. dp = updater.dispatcher
  145. # on different commands - answer in Telegram
  146. dp.add_handler(CommandHandler("start", start))
  147. dp.add_handler(CommandHandler("help", help))
  148. dp.add_handler(CommandHandler("magie", magie))
  149. dp.add_handler(CommandHandler("manta", manta))
  150. dp.add_handler(CommandHandler("goodlife", goodlife))
  151. dp.add_handler(CommandHandler("mensa", mensa, pass_args=True))
  152. dp.add_handler(CommandHandler("sudo", sudo, pass_args=True))
  153. dp.add_handler(CommandHandler("cat", send_cat))
  154. dp.add_handler(CommandHandler("dog", send_dog))
  155. dp.add_handler(CommandHandler("catdog", send_cat_dog))
  156. dp.add_handler(CommandHandler("shrug", shrug))
  157. dp.add_handler(CommandHandler("kill", kill))
  158. dp.add_handler(CommandHandler("revive", revive))
  159. dp.add_handler(CommandHandler("simon", simon, pass_args=True))
  160. dp.add_handler(CommandHandler("graveyard", graveyard))
  161. dp.add_handler(CommandHandler("codinglove", send_coding_love_gif))
  162. # log all errors
  163. dp.add_error_handler(error)
  164. # Start the Bot
  165. updater.start_polling()
  166. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  167. # SIGTERM or SIGABRT. This should be used most of the time, since
  168. # start_polling() is non-blocking and will stop the bot gracefully.
  169. updater.idle()
  170. if __name__ == '__main__':
  171. main()