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.

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