|
- from telegram.ext import Updater, MessageHandler, Filters, CallbackContext
- from telegram import Update,Message
- import json
- import sys
- import io
- import logging
- from PIL import Image
-
- from overlay import overlay
-
- logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
- level=logging.INFO)
-
- def overlay_photo(update: Update, context: CallbackContext):
- try:
- file = context.bot.getFile(update.message.photo[-1].file_id)
- bytes = file.download_as_bytearray()
- image = Image.open(io.BytesIO(bytes))
- overlayed = overlay(image).convert("RGB")
- output = io.BytesIO()
- overlayed.save(output, "JPEG")
- output.seek(0)
- context.bot.send_photo(chat_id=update.effective_chat.id, photo=output)
- except:
- context.bot.send_message(chat_id=update.effective_chat.id, text="Something went wrong. That might not have been an image.")
-
- def overlay_document(update: Update, context: CallbackContext):
- try:
- file = context.bot.getFile(update.message.document.file_id)
- bytes = file.download_as_bytearray()
- image = Image.open(io.BytesIO(bytes))
- overlayed = overlay(image)
- output = io.BytesIO()
- overlayed.save(output, "PNG")
- output.seek(0)
- context.bot.send_document(chat_id=update.effective_chat.id, document=output)
- except:
- context.bot.send_message(chat_id=update.effective_chat.id, text="Something went wrong. That might not have been an image.")
-
- def echo(update: Update, context: CallbackContext):
- context.bot.send_message(chat_id=update.effective_chat.id, text="Send an image (inline or as a file)")
-
- if __name__ == "__main__":
- try:
- config = json.load(open("config.json", "r", encoding="utf-8"))
- except:
- logging.error("Couldn't read the config!")
- sys.exit(1)
-
- updater = Updater(token=config["token"], use_context=True)
- dispatcher = updater.dispatcher
- overlay_photo_handler = MessageHandler(Filters.photo, overlay_photo)
- overlay_document_handler = MessageHandler(Filters.document, overlay_document)
- echo_handler = MessageHandler(Filters.text, echo)
-
- dispatcher.add_handler(overlay_photo_handler)
- dispatcher.add_handler(overlay_document_handler)
- dispatcher.add_handler(echo_handler)
-
- updater.start_polling()
|