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

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

@ -4,7 +4,6 @@ import requests
from command import CommandRegistry
from commands import core
from little_shit import get_source
from apiclient import client as api
__registry__ = cr = CommandRegistry()
@ -33,15 +32,15 @@ def tuling123(args_text, ctx_msg, internal=False):
reply = '腊鸡图灵机器人出问题了,先不管他,过会儿再玩他'
core.echo(reply, ctx_msg)
@cr.register('xiaoice', '小冰')
def xiaoice(args_text, ctx_msg, internal=False):
resp = api.wx_consult(account='xiaoice-ms', content=args_text)
if resp:
json = resp.json()
if json and json.get('reply'):
reply = json['reply']
core.echo(reply, ctx_msg, internal)
return reply
core.echo('小冰没有回复,请稍后再试', ctx_msg, internal)
return None
# TODO: 加入微信消息源之后修改
# @cr.register('xiaoice', '小冰')
# def xiaoice(args_text, ctx_msg, internal=False):
# resp = api.wx_consult(account='xiaoice-ms', content=args_text)
# if resp:
# json = resp.json()
# if json and json.get('reply'):
# reply = json['reply']
# core.echo(reply, ctx_msg, internal)
# return reply
# core.echo('小冰没有回复,请稍后再试', ctx_msg, internal)
# return None

View File

@ -1,5 +1,5 @@
from command import CommandRegistry
from apiclient import client as api
from msg_src_adapter import get_adapter_by_ctx
__registry__ = cr = CommandRegistry()
@ -9,7 +9,10 @@ def echo(args_text, ctx_msg, internal=False):
if internal:
return None
else:
return api.send_message(args_text, ctx_msg)
return get_adapter_by_ctx(ctx_msg).send_message(
target=ctx_msg,
content=args_text
)
@cr.register('help', '帮助', '用法', '使用帮助', '使用指南', '使用说明', '使用方法', '怎么用')

View File

@ -1,15 +1,12 @@
import os
import importlib
from command import CommandRegistry
from commands import core
from nl_processor import parse_potential_commands
from little_shit import get_nl_processors_dir, get_fallback_command_after_nl_processors
from little_shit import load_plugins, get_fallback_command_after_nl_processors
from command import hub as cmdhub
def _init():
_load_processors()
load_plugins('nl_processors')
__registry__ = cr = CommandRegistry(init_func=_init)
@ -41,13 +38,3 @@ def process(args_text, ctx_msg):
core.echo('暂时无法理解你的意思。\n'
'由于自然语言识别还非常不完善,建议使用命令来精确控制我。\n'
'如需帮助请发送「使用帮助」。', ctx_msg)
def _load_processors():
processor_mod_files = filter(
lambda filename: filename.endswith('.py') and not filename.startswith('_'),
os.listdir(get_nl_processors_dir())
)
command_mods = [os.path.splitext(file)[0] for file in processor_mod_files]
for mod_name in command_mods:
importlib.import_module('nl_processors.' + mod_name)

View File

@ -1,6 +1,6 @@
import os
import re
from functools import reduce, wraps
from functools import wraps
import pytz
import requests

View File

@ -26,22 +26,21 @@ def test(_, ctx_msg):
@cr.register('block')
@cr.restrict(full_command_only=True, superuser_only=True)
@split_arguments(maxsplit=2)
@split_arguments(maxsplit=1)
def block(_, ctx_msg, argv=None):
def _send_error_msg():
core.echo('参数不正确。\n\n正确使用方法:\nsudo.block wx|qq <account-to-block>', ctx_msg)
core.echo('参数不正确。\n\n正确使用方法:\nsudo.block <account-to-block>', ctx_msg)
if len(argv) != 2:
if len(argv) != 1:
_send_error_msg()
return
via, account = argv
account = argv[0]
# Get a target using a fake context message
target = get_target({
'via': via,
'type': 'friend_message',
'sender_uid': account,
'sender_account': account
'via': 'default',
'msg_type': 'private',
'sender_id': account
})
if not target:
@ -65,31 +64,28 @@ def block_list(_, ctx_msg, internal=False):
if internal:
return blocked_targets
if blocked_targets:
# `t[1:]` to reply user account, without target prefix 'p'.
# This is a shit code, and should be changed later sometime.
core.echo('已屏蔽的用户:\n' + ', '.join([t[1:] for t in blocked_targets]), ctx_msg)
core.echo('已屏蔽的用户:\n' + ', '.join(blocked_targets), ctx_msg)
else:
core.echo('还没有屏蔽过用户', ctx_msg)
@cr.register('unblock')
@cr.restrict(full_command_only=True, superuser_only=True)
@split_arguments(maxsplit=2)
@split_arguments(maxsplit=1)
def unblock(_, ctx_msg, argv=None):
def _send_error_msg():
core.echo('参数不正确。\n\n正确使用方法:\nsudo.unblock wx|qq <account-to-unblock>', ctx_msg)
core.echo('参数不正确。\n\n正确使用方法:\nsudo.unblock <account-to-unblock>', ctx_msg)
if len(argv) != 2:
if len(argv) != 1:
_send_error_msg()
return
via, account = argv
account = argv[0]
# Get a target using a fake context message
target = get_target({
'via': via,
'type': 'friend_message',
'sender_uid': account,
'sender_account': account
'via': 'default',
'msg_type': 'private',
'sender_id': account
})
if not target: