A bot that overlays the "you died" text on stuff
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

60 рядки
2.3 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. file = context.bot.getFile(update.message.photo[-1].file_id)
  14. bytes = file.download_as_bytearray()
  15. image = Image.open(io.BytesIO(bytes))
  16. overlayed = overlay(image).convert("RGB")
  17. output = io.BytesIO()
  18. overlayed.save(output, "JPEG")
  19. output.seek(0)
  20. context.bot.send_photo(chat_id=update.effective_chat.id, photo=output)
  21. except:
  22. context.bot.send_message(chat_id=update.effective_chat.id, text="Something went wrong. That might not have been an image.")
  23. def overlay_document(update: Update, context: CallbackContext):
  24. try:
  25. file = context.bot.getFile(update.message.document.file_id)
  26. bytes = file.download_as_bytearray()
  27. image = Image.open(io.BytesIO(bytes))
  28. overlayed = overlay(image)
  29. output = io.BytesIO()
  30. overlayed.save(output, "PNG")
  31. output.seek(0)
  32. context.bot.send_document(chat_id=update.effective_chat.id, document=output)
  33. except:
  34. context.bot.send_message(chat_id=update.effective_chat.id, text="Something went wrong. That might not have been an image.")
  35. def echo(update: Update, context: CallbackContext):
  36. context.bot.send_message(chat_id=update.effective_chat.id, text="Send an image (inline or as a file)")
  37. if __name__ == "__main__":
  38. try:
  39. config = json.load(open("config.json", "r", encoding="utf-8"))
  40. except:
  41. logging.error("Couldn't read the config!")
  42. sys.exit(1)
  43. updater = Updater(token=config["token"], use_context=True)
  44. dispatcher = updater.dispatcher
  45. overlay_photo_handler = MessageHandler(Filters.photo, overlay_photo)
  46. overlay_document_handler = MessageHandler(Filters.document, overlay_document)
  47. echo_handler = MessageHandler(Filters.text, echo)
  48. dispatcher.add_handler(overlay_photo_handler)
  49. dispatcher.add_handler(overlay_document_handler)
  50. dispatcher.add_handler(echo_handler)
  51. updater.start_polling()