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.

225 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. /codinglove - random gif from the coding love (https://thecodinglove.com)
  42. /magie - random Axel Stoll quote
  43. /manta - random Opel Manta joke
  44. /goodlife - random good life quote
  45. /shrug - prints shrug
  46. /kill - kills you
  47. /revive - revives you
  48. /sudo kill/revive - force kill/revive
  49. /graveyard - shows the dead people
  50. /help - this help text""", parse_mode=ParseMode.MARKDOWN)
  51. def send_cat(bot, update):
  52. """CUTE"""
  53. cat_file = requests.get('http://aws.random.cat/meow').json()['file']
  54. bot.send_photo(chat_id=update.message.chat_id, photo=cat_file)
  55. def send_dog(bot, update):
  56. """CUTE 2.0"""
  57. dog = pydog.PyDog()
  58. bot.send_photo(chat_id=update.message.chat_id, photo=dog.get_random_image())
  59. def kill(bot, update, sudocall=False):
  60. """kill me pls"""
  61. db = Db()
  62. user = update.message.from_user.first_name
  63. if db.is_dead(user, update.message.chat_id):
  64. if sudocall:
  65. update.message.reply_text("%s killed again" % user)
  66. return
  67. update.message.reply_text("%s is already dead" % user)
  68. return
  69. message = update.message.from_user.first_name + " died"
  70. if sudocall:
  71. message += " for real"
  72. db.kill(user, update.message.chat_id)
  73. update.message.reply_text(message)
  74. def revive(bot, update, sudocall=False):
  75. """unkill me pls"""
  76. db = Db()
  77. user = update.message.from_user.first_name
  78. if not db.is_dead(user, update.message.chat_id):
  79. update.message.reply_text("Maybe %s should have a litte 'accident' before" % user)
  80. return
  81. if sudocall:
  82. update.message.reply_text("%s is living again, for real" % user)
  83. db.revive(user, update.message.chat_id)
  84. return
  85. update.message.reply_text("You fool! You cannot revive a dead person!")
  86. SUDO_CMDS = {
  87. "kill": kill,
  88. "revive": revive
  89. }
  90. def graveyard(bot, update):
  91. """List the dead"""
  92. db = Db()
  93. update.message.reply_text("Here are the dead people:\n%s" % db.get_dead_bodies(update.message.chat_id))
  94. def sudo(bot, update, args):
  95. """for real"""
  96. if not args:
  97. update.message.reply_text("Unknown command")
  98. return
  99. for arg in args:
  100. if arg in SUDO_CMDS:
  101. SUDO_CMDS[arg](bot, update, True)
  102. else:
  103. update.message.reply_text("Unknown command")
  104. def send_cat_dog(bot, update):
  105. """Best of both worlds!"""
  106. catdog = "https://upload.wikimedia.org/wikipedia/en/6/64/CatDog.jpeg"
  107. bot.send_photo(chat_id=update.message.chat_id, photo=catdog)
  108. def magie(bot, update):
  109. """MAGIEEEEEE"""
  110. rand = random.SystemRandom()
  111. update.message.reply_text(STOLL[rand.randrange(0, len(STOLL), 1)])
  112. def manta(bot, update):
  113. """Boah ey, geile Karre!"""
  114. rand = random.SystemRandom()
  115. update.message.reply_text(MANTA[rand.randrange(0, len(MANTA), 1)])
  116. def goodlife(bot, update):
  117. """GOOD LIFE MY A..."""
  118. rand = random.SystemRandom()
  119. update.message.reply_text(GOOD_LIFE[rand.randrange(0, len(GOOD_LIFE), 1)])
  120. def shrug(bot, update):
  121. """SHRUG"""
  122. update.message.reply_text("¯\_(ツ)_/¯")
  123. def simon(bot, update, args):
  124. "KAUF DIR N HUND"
  125. name = "@thedailysimon"
  126. if len(args) > 0:
  127. name = args[0]
  128. rand = random.SystemRandom()
  129. animal = SIMON[rand.randrange(0, len(SIMON), 1)].replace('\n', '')
  130. update.message.reply_text(
  131. "%s, bitte kauf dir einen Hund, eine Katze oder von mir aus auch %s, du hast zu viel Zeit"
  132. % (name, animal))
  133. def echo(bot, update):
  134. """Echo the user message."""
  135. update.message.reply_text(update.message.text)
  136. def error(bot, update, error):
  137. """Log Errors caused by Updates."""
  138. logger.warning('Update "%s" caused error "%s"', update, error)
  139. def main():
  140. """Start the bot."""
  141. # Create the EventHandler and pass it your bot's token.
  142. token = open("test_token").read()
  143. updater = Updater(token.strip())
  144. # Get the dispatcher to register handlers
  145. dp = updater.dispatcher
  146. # on different commands - answer in Telegram
  147. dp.add_handler(CommandHandler("start", start))
  148. dp.add_handler(CommandHandler("help", help))
  149. dp.add_handler(CommandHandler("magie", magie))
  150. dp.add_handler(CommandHandler("manta", manta))
  151. dp.add_handler(CommandHandler("goodlife", goodlife))
  152. dp.add_handler(CommandHandler("mensa", mensa, pass_args=True))
  153. dp.add_handler(CommandHandler("sudo", sudo, pass_args=True))
  154. dp.add_handler(CommandHandler("cat", send_cat))
  155. dp.add_handler(CommandHandler("dog", send_dog))
  156. dp.add_handler(CommandHandler("catdog", send_cat_dog))
  157. dp.add_handler(CommandHandler("shrug", shrug))
  158. dp.add_handler(CommandHandler("kill", kill))
  159. dp.add_handler(CommandHandler("revive", revive))
  160. dp.add_handler(CommandHandler("simon", simon, pass_args=True))
  161. dp.add_handler(CommandHandler("graveyard", graveyard))
  162. dp.add_handler(CommandHandler("codinglove", send_coding_love_gif))
  163. # log all errors
  164. dp.add_error_handler(error)
  165. # Start the Bot
  166. updater.start_polling()
  167. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  168. # SIGTERM or SIGABRT. This should be used most of the time, since
  169. # start_polling() is non-blocking and will stop the bot gracefully.
  170. updater.idle()
  171. if __name__ == '__main__':
  172. main()