From 5813255b8eccafd50612256828bac7a4995ae4a7 Mon Sep 17 00:00:00 2001 From: adk23333 <2633103794@qq.com> Date: Tue, 19 Dec 2023 00:48:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96sio=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command.py | 15 +++++++++++++++ server.py | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 command.py diff --git a/command.py b/command.py new file mode 100644 index 0000000..c50b11a --- /dev/null +++ b/command.py @@ -0,0 +1,15 @@ +from typing import Dict, Any, Optional + +from model import Message + + +def command_convert(data: Dict[str, Any]) -> Optional[Message]: + match (data.get('message').get('content')): + case '/bot': # 测试存活 + return Message(content='icalingua bot test', room_id=data['roomId']) + + case ['/gitea validate', '-v']: + return Message() + + case _: + return None diff --git a/server.py b/server.py index 2937556..5b409da 100644 --- a/server.py +++ b/server.py @@ -7,6 +7,7 @@ from sanic.log import logger, Colors from sanic.response import text from socketio import AsyncClient +from command import command_convert from gitea_model import WebHookIssueComment, WebHookIssue, GiteaEvent from model import Ctx, SioConfig, Message from unit import sio_log_format @@ -148,6 +149,22 @@ def start_sio_listener(): def catch_all(event, data): logger.debug(sio_log_format('catch_all:', f'{event}|{data}')) + @app.ctx.sio.on('addMessage') + async def add_message(data: Dict[str, Any]): + logger.debug(sio_log_format('add_message:', data)) + + sender_id = data['message']['senderId'] + is_self = (sender_id == app.ctx.sio_config.SELF_ID) + + sender_name = data['message']['username'] + content = data['message']['content'] + room_id = data['roomId'] + + if not is_self: + msg = command_convert(data) + if msg is not None: + await app.ctx.sio.emit('sendMessage', msg.to_json()) + if __name__ == "__main__": app.run(host='0.0.0.0', port=80, dev=True)