A bot that overlays the "you died" text on stuff
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

184 Zeilen
5.8 KiB

  1. from telegram.ext import (
  2. Updater,
  3. MessageHandler,
  4. Filters,
  5. CallbackContext,
  6. ConversationHandler,
  7. CommandHandler,
  8. )
  9. from telegram import Update, Message, ReplyKeyboardMarkup, ReplyKeyboardRemove, File
  10. import json
  11. import sys
  12. import io
  13. import logging
  14. from PIL import Image
  15. from overlay import overlay
  16. logging.basicConfig(
  17. format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
  18. )
  19. def overlay_photo(update: Update, context: CallbackContext):
  20. try:
  21. caption = update.message.caption
  22. if not caption:
  23. caption = "YOU DIED"
  24. file = context.bot.getFile(update.message.photo[-1].file_id)
  25. bytes = file.download_as_bytearray()
  26. image = Image.open(io.BytesIO(bytes))
  27. overlayed = overlay(image, caption).convert("RGB")
  28. output = io.BytesIO()
  29. overlayed.save(output, "JPEG")
  30. output.seek(0)
  31. context.bot.send_photo(chat_id=update.effective_chat.id, photo=output)
  32. except:
  33. context.bot.send_message(
  34. chat_id=update.effective_chat.id,
  35. text="Something went wrong. That might not have been an image.",
  36. )
  37. def overlay_document(update: Update, context: CallbackContext):
  38. try:
  39. caption = update.message.caption
  40. if not caption:
  41. caption = "YOU DIED"
  42. file = context.bot.getFile(update.message.document.file_id)
  43. bytes = file.download_as_bytearray()
  44. image = Image.open(io.BytesIO(bytes))
  45. overlayed = overlay(image, caption)
  46. output = io.BytesIO()
  47. overlayed.save(output, "PNG")
  48. output.seek(0)
  49. context.bot.send_document(chat_id=update.effective_chat.id, document=output)
  50. except:
  51. context.bot.send_message(
  52. chat_id=update.effective_chat.id,
  53. text="Something went wrong. That might not have been an image.",
  54. )
  55. def echo(update: Update, context: CallbackContext):
  56. context.bot.send_message(
  57. chat_id=update.effective_chat.id,
  58. text="Send an image (inline or as a file). You can customize the text by adding a caption. Alternatively, use the /caption command to choose the image, text, and color.",
  59. )
  60. IMAGE, TEXT, COLOR = range(3)
  61. def start_overlay(update: Update, context: CallbackContext) -> int:
  62. """Begins the overlay process."""
  63. update.message.reply_text("First, send the image." "\n\n" "Send /cancel to stop.")
  64. return IMAGE
  65. def receive_image(update: Update, context: CallbackContext) -> int:
  66. """Receives the image to add text to."""
  67. user = update.message.from_user
  68. image = update.message.photo[-1].get_file()
  69. context.user_data["image"] = image
  70. logging.info("Received photo from %s", user.first_name)
  71. update.message.reply_text("Next, send the caption.")
  72. return TEXT
  73. def receive_text(update: Update, context: CallbackContext) -> int:
  74. """Receives the caption to put on the image."""
  75. user = update.message.from_user
  76. text = update.message.text
  77. context.user_data["text"] = text
  78. options = [["Red", "Gold"]]
  79. logging.info("Received text from %s", user.first_name)
  80. update.message.reply_text(
  81. "Finally, what color do you want?",
  82. reply_markup=ReplyKeyboardMarkup(
  83. options, one_time_keyboard=True, input_field_placeholder="Color"
  84. ),
  85. )
  86. return COLOR
  87. def receive_color(update: Update, context: CallbackContext) -> int:
  88. """Receives the color for the caption."""
  89. user = update.message.from_user
  90. color = update.message.text
  91. logging.info("Received color from %s", user.first_name)
  92. image: File = context.user_data["image"]
  93. text: str = context.user_data["text"]
  94. try:
  95. bytes = image.download_as_bytearray()
  96. image = Image.open(io.BytesIO(bytes))
  97. color_rgb = (200, 25, 25)
  98. if color == "Gold":
  99. color_rgb = (255, 255, 108)
  100. overlayed = overlay(image, text, color_rgb).convert("RGB")
  101. output = io.BytesIO()
  102. overlayed.save(output, "JPEG")
  103. output.seek(0)
  104. context.bot.send_photo(chat_id=update.effective_chat.id, photo=output)
  105. except:
  106. context.bot.send_message(
  107. chat_id=update.effective_chat.id,
  108. text="Something went wrong. That might not have been an image.",
  109. )
  110. update.message.reply_text("Done!", reply_markup=ReplyKeyboardRemove())
  111. return ConversationHandler.END
  112. def cancel_overlay(update: Update, context: CallbackContext) -> int:
  113. """Cancels the overlay process."""
  114. user = update.message.from_user
  115. logging.info("Canceled overlay for %s", user.first_name)
  116. update.message.reply_text("Cancelled.", reply_markup=ReplyKeyboardRemove())
  117. return ConversationHandler.END
  118. if __name__ == "__main__":
  119. try:
  120. config = json.load(open("config.json", "r", encoding="utf-8"))
  121. except:
  122. logging.error("Couldn't read the config!")
  123. sys.exit(1)
  124. updater = Updater(token=config["token"], use_context=True)
  125. dispatcher = updater.dispatcher
  126. overlay_photo_handler = MessageHandler(Filters.photo, overlay_photo)
  127. overlay_document_handler = MessageHandler(Filters.document, overlay_document)
  128. echo_handler = MessageHandler(Filters.text, echo)
  129. conv_handler = ConversationHandler(
  130. entry_points=[CommandHandler("caption", start_overlay)],
  131. states={
  132. IMAGE: [MessageHandler(Filters.photo, receive_image)],
  133. TEXT: [MessageHandler(Filters.text, receive_text)],
  134. COLOR: [MessageHandler(Filters.regex("^(Red|Gold)$"), receive_color)],
  135. },
  136. fallbacks=[CommandHandler("cancel", cancel_overlay)],
  137. )
  138. dispatcher.add_handler(conv_handler)
  139. dispatcher.add_handler(overlay_photo_handler)
  140. dispatcher.add_handler(overlay_document_handler)
  141. dispatcher.add_handler(echo_handler)
  142. updater.start_polling()