瀏覽代碼

Write the rest of the fucking bot

master
Fen Dweller 5 年之前
父節點
當前提交
d59d900034
共有 2 個檔案被更改,包括 61 行新增1 行删除
  1. +1
    -1
      overlay.py
  2. +60
    -0
      you-died.py

+ 1
- 1
overlay.py 查看文件

@@ -3,7 +3,7 @@ from PIL import Image, ImageDraw, ImageFont
def sub(p1, p2):
return (p1[0] - p2[0], p1[1] - p2[1])

def overlay(image_in, image_out):
def overlay(image_in):
image = image_in.convert("RGBA")

size = image.size


+ 60
- 0
you-died.py 查看文件

@@ -0,0 +1,60 @@
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()

Loading…
取消
儲存