diff --git a/adacher.py b/adacher.py index 49bbca572395aa2f9c130f501f8005a43ee00809..345a0eacb2f23167a45daa406e4f2e6c79b6ae76 100755 --- a/adacher.py +++ b/adacher.py @@ -2,6 +2,7 @@ import argparse import math +import tempfile from PIL import Image from PIL import ImageFont @@ -53,7 +54,7 @@ def growFontToBox(draw, fontPath, text, width, height, wrap=False): else: newText = text while True: - print(fontsize) + #print(fontsize) fontsize += 1 font = ImageFont.truetype(fontPath, fontsize) @@ -114,6 +115,11 @@ draw.text(( newText, textCol, font=font, + align=args.align ) -img.save('/tmp/adachi.png') +finalHeight = 200 +finalWidth = finalHeight * width // boxBot +img = img.resize((finalWidth, finalHeight), Image.LANCZOS) +img.save("tmpAdacher.png", format="PNG") +print(str(finalWidth) + "," + str(finalHeight)) diff --git a/adacher_matrix.py b/adacher_matrix.py index 22a4563b25ed5a33c3531f20edd7bb483cf333fd..28f67fb64dc239a9c50cc6cdfac25c79fad9f88c 100755 --- a/adacher_matrix.py +++ b/adacher_matrix.py @@ -1,34 +1,69 @@ -# echo.py -# Example: -# randomuser - "!echo example string" -# echo_bot - "example string" +#!/usr/bin/python3 import simplematrixbotlib as botlib import nio import random -from dotenv import load_doatenv +import os +import subprocess +import requests +import json +from dotenv import load_dotenv random.seed() -load_doatenv() +load_dotenv() + +ADACHER_TOKEN = os.getenv("ADACHER_TOKEN") +ADACHER_HS = os.getenv("ADACHER_HS") +ADACHER_USERNAME = os.getenv("ADACHER_USERNAME") +ADACHER_PASSWD = os.getenv("ADACHER_PASSWD") + +creds = botlib.Creds("https://" + ADACHER_HS, ADACHER_USERNAME, ADACHER_PASSWD) -creds = botlib.Creds(os.getenv("ADACHER_HS"), os.getenv("ADACHER_USERNAME"), os.getenv("ADACHER_PASSWD")) bot = botlib.Bot(creds) -PREFIX = '!adacher' +PREFIX = '!' -@bot.listener.on_custom_event(nio.events.room_events.StickerEvent) +@bot.listener.on_message_event async def echo(room, event): match = botlib.MessageMatch(room, event, bot, PREFIX) - print(event.body) - if match.is_not_from_this_bot() and room.room_id=="!ifyNcwrVkKBFvJGDQU:hashi.re"): + if match.is_not_from_this_bot() and match.command("adacher") and ( + room.room_id=="!ifyNcwrVkKBFvJGDQU:hashi.re" + or (room.room_id=="!qesUBTBxBQiVGaKdNJ:iiens.net" and (match.is_from_userid("@krocoh:baguette.party") or match.is_from_userid("@elliu:hashi.re"))) + ): + cmd = event.body.removeprefix("!adacher ").strip() + proc = subprocess.run("./adacher.py " + cmd, shell=True, check=True, encoding='utf-8', stdout=subprocess.PIPE) + width, height = proc.stdout.strip().split(',') + + headers = { "Content-Type": "image/png" } + response = requests.post( + "https://" + ADACHER_HS + "/_matrix/media/r0/upload?access_token=" + ADACHER_TOKEN, + headers=headers, + data=open("tmpAdacher.png", 'rb') + ) + if response.status_code != requests.codes.ok: + print("===== Error, ignoring =====") + print("JSON Response ", response.json()) + print("Status Code", response.status_code) + return + + uri = response.json()['content_uri'] + + print(cmd) content = { - "body": "yes", - "msgtype": "m.text", + "url": uri, + "info": { + "mimetype": "image/png", + "h": height, + "w": width, + "thumbnail_url": uri, + }, + "body": json.JSONEncoder().encode(cmd), "m.relates_to": { "m.in_reply_to": { "event_id": event.event_id } } } - await bot.async_client.room_send(room.room_id, 'm.room.message', content) + await bot.async_client.room_send(room.room_id, 'm.sticker', content) + bot.run()