echobot
This commit is contained in:
parent
710d52ee3a
commit
1d96f80ee6
3 changed files with 42 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
|||
from string import ascii_lowercase as ascii_lowercase_
|
||||
|
||||
commands = {
|
||||
'echo',
|
||||
'asino',
|
||||
}
|
||||
|
||||
|
|
38
lanonna/echobot.py
Normal file
38
lanonna/echobot.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
'''Example echobot, used to demonstrate la nonna capabilities but also debug
|
||||
Example of payloads received from mq:
|
||||
{"command": "echo", "content": "test",
|
||||
"source_message_id": "$i3YsHdF9oWRBZQ4hJ8QC_JzJRHYX1fW3wVMP1A0wQzM", "sender_nick": "bparodi",
|
||||
"room_id": "!PQHkyOTruVtZnMRCRe:goulash.lezzo.org"}
|
||||
'''
|
||||
import aio_pika
|
||||
import logging
|
||||
import json
|
||||
|
||||
async def run(mq_url):
|
||||
connection = await aio_pika.connect_robust(mq_url)
|
||||
|
||||
async with connection:
|
||||
channel = await connection.channel()
|
||||
|
||||
queue = await channel.declare_queue('echo')
|
||||
|
||||
try:
|
||||
async with queue.iterator() as queue_iter:
|
||||
async for message in queue_iter:
|
||||
async with message.process():
|
||||
body = message.body.decode()
|
||||
# br = protocol.json_to_bot_response(body)
|
||||
print(body)
|
||||
br = json.loads(body)
|
||||
response = {
|
||||
'content': br['content'],
|
||||
'source_message_id': br['source_message_id'],
|
||||
'room_id': br['room_id'],
|
||||
'as_reply': True,
|
||||
'as_markdown': False,
|
||||
}
|
||||
btes = json.dumps(response).encode()
|
||||
mqmsg = aio_pika.Message(body=btes)
|
||||
await channel.default_exchange.publish(mqmsg, 'lanonna')
|
||||
except Exception as e:
|
||||
logging.exception(f'Exception in mq loop: {e}')
|
|
@ -60,11 +60,14 @@ async def mqmain(rabbit_client: mq.MQClient, matrix_client: matrix.MatrixClient)
|
|||
|
||||
|
||||
async def main(conf: config.Configuration):
|
||||
import echobot
|
||||
|
||||
mq_client = await mq.initialize(conf)
|
||||
matrix_client = await matrix.initialize(conf)
|
||||
|
||||
async with await psycopg.AsyncConnection.connect(conf.db_url, autocommit=True) as db_connection:
|
||||
await asyncio.gather(
|
||||
echobot.run(conf.mq_url),
|
||||
mqmain(mq_client, matrix_client),
|
||||
matrixmain(matrix_client, mq_client, db_connection)
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue