A bot that overlays the "you died" text on stuff
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.
 
 

66 lines
2.5 KiB

  1. from telegram.ext import Updater, MessageHandler, Filters, CallbackContext
  2. from telegram import Update,Message
  3. import json
  4. import sys
  5. import io
  6. import logging
  7. from PIL import Image
  8. from overlay import overlay
  9. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  10. level=logging.INFO)
  11. def overlay_photo(update: Update, context: CallbackContext):
  12. try:
  13. caption = update.message.caption
  14. if not caption:
  15. caption = "YOU DIED"
  16. file = context.bot.getFile(update.message.photo[-1].file_id)
  17. bytes = file.download_as_bytearray()
  18. image = Image.open(io.BytesIO(bytes))
  19. overlayed = overlay(image, caption).convert("RGB")
  20. output = io.BytesIO()
  21. overlayed.save(output, "JPEG")
  22. output.seek(0)
  23. context.bot.send_photo(chat_id=update.effective_chat.id, photo=output)
  24. except:
  25. context.bot.send_message(chat_id=update.effective_chat.id, text="Something went wrong. That might not have been an image.")
  26. def overlay_document(update: Update, context: CallbackContext):
  27. try:
  28. caption = update.message.caption
  29. if not caption:
  30. caption = "YOU DIED"
  31. file = context.bot.getFile(update.message.document.file_id)
  32. bytes = file.download_as_bytearray()
  33. image = Image.open(io.BytesIO(bytes))
  34. overlayed = overlay(image, caption)
  35. output = io.BytesIO()
  36. overlayed.save(output, "PNG")
  37. output.seek(0)
  38. context.bot.send_document(chat_id=update.effective_chat.id, document=output)
  39. except:
  40. context.bot.send_message(chat_id=update.effective_chat.id, text="Something went wrong. That might not have been an image.")
  41. def echo(update: Update, context: CallbackContext):
  42. context.bot.send_message(chat_id=update.effective_chat.id, text="Send an image (inline or as a file)")
  43. if __name__ == "__main__":
  44. try:
  45. config = json.load(open("config.json", "r", encoding="utf-8"))
  46. except:
  47. logging.error("Couldn't read the config!")
  48. sys.exit(1)
  49. updater = Updater(token=config["token"], use_context=True)
  50. dispatcher = updater.dispatcher
  51. overlay_photo_handler = MessageHandler(Filters.photo, overlay_photo)
  52. overlay_document_handler = MessageHandler(Filters.document, overlay_document)
  53. echo_handler = MessageHandler(Filters.text, echo)
  54. dispatcher.add_handler(overlay_photo_handler)
  55. dispatcher.add_handler(overlay_document_handler)
  56. dispatcher.add_handler(echo_handler)
  57. updater.start_polling()