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.

244 lines
7.6 KiB

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