优化sio命令解析
This commit is contained in:
parent
93c745d681
commit
5813255b8e
15
command.py
Normal file
15
command.py
Normal file
@ -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
|
17
server.py
17
server.py
@ -7,6 +7,7 @@ from sanic.log import logger, Colors
|
|||||||
from sanic.response import text
|
from sanic.response import text
|
||||||
from socketio import AsyncClient
|
from socketio import AsyncClient
|
||||||
|
|
||||||
|
from command import command_convert
|
||||||
from gitea_model import WebHookIssueComment, WebHookIssue, GiteaEvent
|
from gitea_model import WebHookIssueComment, WebHookIssue, GiteaEvent
|
||||||
from model import Ctx, SioConfig, Message
|
from model import Ctx, SioConfig, Message
|
||||||
from unit import sio_log_format
|
from unit import sio_log_format
|
||||||
@ -148,6 +149,22 @@ def start_sio_listener():
|
|||||||
def catch_all(event, data):
|
def catch_all(event, data):
|
||||||
logger.debug(sio_log_format('catch_all:', f'{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__":
|
if __name__ == "__main__":
|
||||||
app.run(host='0.0.0.0', port=80, dev=True)
|
app.run(host='0.0.0.0', port=80, dev=True)
|
||||||
|
Loading…
Reference in New Issue
Block a user