matrix-bot/matrix_bot/plugins/socket.py

38 lines
1.4 KiB
Python

import json
from aiohttp import web
from ..message import MarkdownMessage
from ._plugin import _Plugin
class Plugin(_Plugin):
help = 'Echo the given input back into the room'
async def __handle_request(self, request: web.Request) -> None:
response = {'status': 'Bad Request', 'status_code': 400}
if request.has_body:
body = await request.json()
if 'type' in body.keys():
self.logger.debug('Request recieved: %s %s', request, body)
if body['type'] == 'message.text' and 'room_id' in body.keys(
) and 'message' in body.keys():
room = await self.__bot.find_room_by_id(body['room_id'])
message = MarkdownMessage(body['message'])
await message.send(self.client, room)
response = {'status': 'OK', 'status_code': 200}
return web.Response(body=json.dumps(response),
status=response['status_code'],
content_type='application/json')
async def on_run(self) -> None:
app = web.Application()
app.router.add_post('/', self.__handle_request)
runner = web.AppRunner(app)
self.logger.debug('Setup Web server')
await runner.setup()
self.logger.debug('Web server starts listening')
site = web.UnixSite(runner, '/tmp/test.sock')
await site.start()