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.

243 lines
8.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import logging
  4. import random
  5. from telegram import ParseMode, Update
  6. from telegram.ext import Updater, CommandHandler, CallbackContext
  7. from web_requests import send_coding_love_gif, send_wiki_how_article
  8. from reddit import wrong_dog, hamster, reddit, reddit_img, cat, dog, drillcat
  9. from schneiderbot_twitter import toggle_pepito_for_group, check_pepito_and_post_auto, check_pepito_and_post_manually
  10. from db import Db
  11. from mensa import mensa, linux_mensa
  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("res/stoll.txt", "r") as tmp_file:
  17. STOLL = tmp_file.readlines()
  18. with open("res/manta.txt", "r") as tmp_file:
  19. MANTA = tmp_file.readlines()
  20. with open("res/goodlife.txt", "r") as tmp_file:
  21. GOOD_LIFE = tmp_file.readlines()
  22. with open("res/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(update: Update, context: CallbackContext):
  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, Katzenbilder, Hundebilder und andere _tolle_ Sachen.
  30. Mhmm, lecker. Guten Appetit!""", parse_mode=ParseMode.MARKDOWN)
  31. def help(update: Update, context: CallbackContext):
  32. """Send a message when the command /help is issued."""
  33. 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. *Reddit*:
  38. /cat - random cat image (using reddit.com/r/catpictures)
  39. /dog - random dog image (using reddit.com/r/dogpictures)
  40. /hamster - random dog image (using reddit.com/r/hamsters)
  41. /drillcat - random drillcat image (using reddit.com/r/drillcats)
  42. /wrongdog - random wrongdog image (using reddit.com/r/whatswrongwithyourdog)
  43. /reddit subreddit - random submission from reddit.com/r/subreddit
  44. /reddit\_img subreddit - random image submission from reddit.com/r/subreddit
  45. *Quotes*:
  46. /magie - random Axel Stoll quote
  47. /manta - random Opel Manta joke
  48. /goodlife - random good life quote
  49. *Other fun stuff*:
  50. /codinglove - random gif from the coding love (https://thecodinglove.com)
  51. /wikihow - random wikihow article (https://http://de.wikihow.com)
  52. /shrug - prints shrug
  53. /kill - kills you
  54. /revive - revives you
  55. /sudo kill/revive - force kill/revive
  56. /graveyard - shows the dead people
  57. /help - this help text"""
  58. print(reply_text)
  59. update.message.reply_text(reply_text, parse_mode=ParseMode.MARKDOWN)
  60. def kill(update: Update, context: CallbackContext, sudocall=False):
  61. """kill me pls"""
  62. db = Db()
  63. user = update.message.from_user.first_name
  64. if db.is_dead(user, update.message.chat_id):
  65. if sudocall:
  66. update.message.reply_text("%s killed again" % user)
  67. return
  68. update.message.reply_text("%s is already dead" % user)
  69. return
  70. message = update.message.from_user.first_name + " died"
  71. if sudocall:
  72. message += " for real"
  73. db.kill(user, update.message.chat_id)
  74. update.message.reply_text(message)
  75. def revive(update: Update, context: CallbackContext, sudocall=False):
  76. """unkill me pls"""
  77. db = Db()
  78. user = update.message.from_user.first_name
  79. if not db.is_dead(user, update.message.chat_id):
  80. update.message.reply_text("Maybe %s should have a litte 'accident' before" % user)
  81. return
  82. if sudocall:
  83. update.message.reply_text("%s is living again, for real" % user)
  84. db.revive(user, update.message.chat_id)
  85. return
  86. update.message.reply_text("You fool! You cannot revive a dead person!")
  87. SUDO_CMDS = {
  88. "kill": kill,
  89. "revive": revive
  90. }
  91. def graveyard(update: Update, context: CallbackContext):
  92. """List the dead"""
  93. db = Db()
  94. update.message.reply_text("Here are the dead people:\n%s" % db.get_dead_bodies(update.message.chat_id))
  95. def sudo(update: Update, context: CallbackContext, args):
  96. """for real"""
  97. if not args:
  98. update.message.reply_text("Unknown command")
  99. return
  100. for arg in args:
  101. if arg in SUDO_CMDS:
  102. SUDO_CMDS[arg](update, context, True)
  103. else:
  104. update.message.reply_text("Unknown command")
  105. def send_cat_dog(update: Update, context: CallbackContext):
  106. """Best of both worlds!"""
  107. catdog = "https://upload.wikimedia.org/wikipedia/en/6/64/CatDog.jpeg"
  108. context.bot.send_photo(chat_id=update.message.chat_id, photo=catdog)
  109. def magie(update: Update, context: CallbackContext):
  110. """MAGIEEEEEE"""
  111. rand = random.SystemRandom()
  112. update.message.reply_text(STOLL[rand.randrange(0, len(STOLL), 1)])
  113. def manta(update: Update, context: CallbackContext):
  114. """Boah ey, geile Karre!"""
  115. rand = random.SystemRandom()
  116. update.message.reply_text(MANTA[rand.randrange(0, len(MANTA), 1)])
  117. def goodlife(update: Update, context: CallbackContext):
  118. """GOOD LIFE MY A..."""
  119. rand = random.SystemRandom()
  120. update.message.reply_text(GOOD_LIFE[rand.randrange(0, len(GOOD_LIFE), 1)])
  121. def shrug(update: Update, context: CallbackContext):
  122. """SHRUG"""
  123. update.message.reply_text("¯\_(ツ)_/¯")
  124. def simon(update: Update, context: CallbackContext, args):
  125. """KAUF DIR N HUND"""
  126. name = "Simon"
  127. if len(args) > 0:
  128. name = args[0]
  129. rand = random.SystemRandom()
  130. animal = SIMON[rand.randrange(0, len(SIMON), 1)].replace('\n', '')
  131. update.message.reply_text(
  132. "%s, bitte kauf dir einen Hund, eine Katze oder von mir aus auch %s, du hast zu viel Zeit"
  133. % (name, animal))
  134. def echo(update: Update, context: CallbackContext):
  135. """Echo the user message."""
  136. update.message.reply_text(update.message.text)
  137. def error(update: Update, context: CallbackContext):
  138. """Log Errors caused by Updates."""
  139. logger.warning('Update "%s" caused error "%s"', update, context.error)
  140. def init_commands(dp):
  141. dp.add_handler(CommandHandler("start", start))
  142. dp.add_handler(CommandHandler("help", help))
  143. dp.add_handler(CommandHandler("magie", magie))
  144. dp.add_handler(CommandHandler("manta", manta))
  145. dp.add_handler(CommandHandler("goodlife", goodlife))
  146. dp.add_handler(CommandHandler("mensa", mensa, pass_args=True))
  147. dp.add_handler(CommandHandler("lmensa", linux_mensa))
  148. dp.add_handler(CommandHandler("sudo", sudo, pass_args=True))
  149. dp.add_handler(CommandHandler("cat", cat))
  150. dp.add_handler(CommandHandler("dog", 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("wiegeht", send_wiki_how_article))
  159. dp.add_handler(CommandHandler("wikihow", send_wiki_how_article))
  160. dp.add_handler(CommandHandler("wrongdog", wrong_dog))
  161. dp.add_handler(CommandHandler("hamster", hamster))
  162. dp.add_handler(CommandHandler("drillcat", drillcat))
  163. dp.add_handler(CommandHandler("reddit", reddit))
  164. dp.add_handler(CommandHandler("reddit_img", reddit_img))
  165. dp.add_handler(CommandHandler("pepito", check_pepito_and_post_manually))
  166. dp.add_handler(CommandHandler("tpepito", toggle_pepito_for_group))
  167. def main():
  168. """Start the bot."""
  169. # Create the EventHandler and pass it your bot's token.
  170. token = open("config/token").read()
  171. updater = Updater(token.strip())
  172. jq = updater.job_queue
  173. # Get the dispatcher to register handlers
  174. dp = updater.dispatcher
  175. init_commands(dp)
  176. jq.run_repeating(check_pepito_and_post_auto, interval=180, first=0)
  177. # on different commands - answer in Telegram
  178. # log all errors
  179. dp.add_error_handler(error)
  180. # Start the Bot
  181. updater.start_polling()
  182. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  183. # SIGTERM or SIGABRT. This should be used most of the time, since
  184. # start_polling() is non-blocking and will stop the bot gracefully.
  185. updater.idle()
  186. if __name__ == '__main__':
  187. main()