Skip to content
Extraits de code Groupes Projets
Valider 0444afa2 rédigé par Elliu's avatar Elliu
Parcourir les fichiers

Working finally

parent aad11456
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import argparse import argparse
import math import math
import tempfile
from PIL import Image from PIL import Image
from PIL import ImageFont from PIL import ImageFont
...@@ -53,7 +54,7 @@ def growFontToBox(draw, fontPath, text, width, height, wrap=False): ...@@ -53,7 +54,7 @@ def growFontToBox(draw, fontPath, text, width, height, wrap=False):
else: else:
newText = text newText = text
while True: while True:
print(fontsize) #print(fontsize)
fontsize += 1 fontsize += 1
font = ImageFont.truetype(fontPath, fontsize) font = ImageFont.truetype(fontPath, fontsize)
...@@ -114,6 +115,11 @@ draw.text(( ...@@ -114,6 +115,11 @@ draw.text((
newText, newText,
textCol, textCol,
font=font, 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))
# echo.py #!/usr/bin/python3
# Example:
# randomuser - "!echo example string"
# echo_bot - "example string"
import simplematrixbotlib as botlib import simplematrixbotlib as botlib
import nio import nio
import random import random
from dotenv import load_doatenv import os
import subprocess
import requests
import json
from dotenv import load_dotenv
random.seed() 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) 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): async def echo(room, event):
match = botlib.MessageMatch(room, event, bot, PREFIX) match = botlib.MessageMatch(room, event, bot, PREFIX)
print(event.body) if match.is_not_from_this_bot() and match.command("adacher") and (
if match.is_not_from_this_bot() and room.room_id=="!ifyNcwrVkKBFvJGDQU:hashi.re"): 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 = { content = {
"body": "yes", "url": uri,
"msgtype": "m.text", "info": {
"mimetype": "image/png",
"h": height,
"w": width,
"thumbnail_url": uri,
},
"body": json.JSONEncoder().encode(cmd),
"m.relates_to": { "m.relates_to": {
"m.in_reply_to": { "m.in_reply_to": {
"event_id": event.event_id "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() bot.run()
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter