完成适配器化改造,所有上报数据统一、接口调用等都改成通过不同消息源的适配器来完成,插件和消息源的耦合

This commit is contained in:
Richard Chien
2017-02-15 15:52:18 +08:00
parent ecd446f057
commit 3508db348d
17 changed files with 383 additions and 221 deletions

View File

@ -1,6 +1,5 @@
import re
import sys
import importlib
import interactive
from filter import as_filter
@ -14,20 +13,6 @@ _command_start_flags = get_command_start_flags()
_command_args_start_flags = get_command_args_start_flags()
def _load_commands():
command_mod_files = filter(
lambda filename: filename.endswith('.py') and not filename.startswith('_'),
os.listdir(get_commands_dir())
)
command_mods = [os.path.splitext(file)[0] for file in command_mod_files]
for mod_name in command_mods:
cmd_mod = importlib.import_module('commands.' + mod_name)
try:
cmdhub.add_registry(mod_name, cmd_mod.__registry__)
except AttributeError:
print('Failed to load command module "' + mod_name + '.py".', file=sys.stderr)
@as_filter(priority=0)
def _dispatch_command(ctx_msg):
try:
@ -77,4 +62,12 @@ def _dispatch_command(ctx_msg):
core.echo('这个命令不支持' + se.msg_type + '哦~', ctx_msg)
_load_commands()
def _add_registry_mod_cb(mod):
mod_name = mod.__name__.split('.')[1]
try:
cmdhub.add_registry(mod_name, mod.__registry__)
except AttributeError:
print('Failed to load command module "' + mod_name + '.py".', file=sys.stderr)
load_plugins('commands', module_callback=_add_registry_mod_cb)

View File

@ -8,7 +8,7 @@ from filter import as_filter
@as_filter(priority=100)
def _filter(ctx_msg):
msg_format = ctx_msg.get('format')
if msg_format != 'text' and ctx_msg.get('type') != 'friend_message':
if msg_format != 'text' and ctx_msg.get('msg_type') != 'private':
return False
if msg_format not in ('text', 'media'):
return False

View File

@ -7,7 +7,10 @@ from filter import as_filter
@as_filter(priority=1000)
def _log_message(ctx_msg):
print(ctx_msg.get('sender', '')
+ (('@' + ctx_msg.get('group')) if ctx_msg.get('type') == 'group_message' else '')
+ (('@' + ctx_msg.get('discuss')) if ctx_msg.get('type') == 'discuss_message' else '')
+ ': ' + ctx_msg.get('content'))
log = ctx_msg.get('sender') or ctx_msg.get('sender_id') or '未知用户'
if ctx_msg.get('msg_type') == 'group':
log += '@' + ctx_msg.get('group') or ctx_msg.get('group_id') or '未知群组'
if ctx_msg.get('msg_type') == 'discuss':
log += '@' + ctx_msg.get('discuss') or ctx_msg.get('discuss_id') or '未知讨论组'
log += ': ' + ctx_msg.get('content', '')
print(log)

View File

@ -3,12 +3,16 @@ This filter intercepts messages not intended to the bot and removes the beginnin
"""
from filter import as_filter
from apiclient import client as api
from msg_src_adapter import get_adapter_by_ctx
@as_filter(priority=50)
def _split_at_xiaokai(ctx_msg):
if ctx_msg.get('type') == 'group_message' or ctx_msg.get('type') == 'discuss_message':
if ctx_msg.get('is_at_me'):
# Directly return because it has been confirmed by previous processes
return True
if ctx_msg.get('msg_type') == 'group' or ctx_msg.get('msg_type') == 'discuss':
text = ctx_msg.get('text', '')
if text.startswith('@'):
my_group_nick = ctx_msg.get('receiver')
@ -16,10 +20,8 @@ def _split_at_xiaokai(ctx_msg):
return False
at_me = '@' + my_group_nick
if not text.startswith(at_me):
user_info = api.get_user_info(ctx_msg).json()
if not user_info:
return False
my_nick = user_info.get('name')
user_info = get_adapter_by_ctx(ctx_msg).get_login_info(ctx_msg)
my_nick = user_info.get('nickname')
if not my_nick:
return False
at_me = '@' + my_nick

View File

@ -1,25 +0,0 @@
"""
This filter unitize context messages from different platform.
"""
from filter import as_filter
@as_filter(priority=10000)
def _unitize(ctx_msg):
if 'group_uid' in ctx_msg:
ctx_msg['group_uid'] = str(ctx_msg['group_uid'])
if 'sender_uid' in ctx_msg:
ctx_msg['sender_uid'] = str(ctx_msg['sender_uid'])
if 'sender_id' in ctx_msg:
ctx_msg['sender_id'] = str(ctx_msg['sender_id'])
if 'discuss_id' in ctx_msg:
ctx_msg['discuss_id'] = str(ctx_msg['discuss_id'])
if 'group_id' in ctx_msg:
ctx_msg['group_id'] = str(ctx_msg['group_id'])
if 'id' in ctx_msg:
ctx_msg['id'] = str(ctx_msg['id'])
if ctx_msg.get('via') == 'qq' and not ctx_msg.get('format'):
# All QQ messages that can be received are text
ctx_msg['format'] = 'text'