« useless.py » n'existait pas sur « cda512e85d6ade2ebb161b3ab05c70b240a32008 »
Sélectionner une révision Git
-
Alexandre Morignot a rédigéAlexandre Morignot a rédigé
adacher_matrix.py 2,25 Kio
#!/usr/bin/python3
import simplematrixbotlib as botlib
import nio
import random
import os
import subprocess
import requests
import json
from dotenv import load_dotenv
random.seed()
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)
bot = botlib.Bot(creds)
PREFIX = '!'
@bot.listener.on_message_event
async def echo(room, event):
match = botlib.MessageMatch(room, event, bot, PREFIX)
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 = {
"url": uri,
"info": {
"mimetype": "image/png",
"h": height,
"w": width,
"size": 0,
"thumbnail_url": uri,
},
"body": cmd,
#"m.relates_to": {
# "m.in_reply_to": {
# "event_id": event.event_id
# }
#}
}
await bot.async_client.room_send(room.room_id, 'm.sticker', content)
bot.run()