From 656190e70f1f148e78794ea45526deda2e3c2948 Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Wed, 11 Jan 2017 20:43:37 +0800 Subject: [PATCH] Add xiaoice command --- apiclient.py | 7 +++++++ commands/ai.py | 46 +++++++++++++++++++++++++++++++++++++++++ commands/core.py | 29 -------------------------- commands/simpletools.py | 8 +++---- config.py | 2 +- 5 files changed, 58 insertions(+), 34 deletions(-) create mode 100644 commands/ai.py diff --git a/apiclient.py b/apiclient.py index 18f7e2a1..0e236abc 100644 --- a/apiclient.py +++ b/apiclient.py @@ -89,5 +89,12 @@ class ApiClient: except requests.exceptions.ConnectionError: return None + def wx_consult(self, account, content): + if self.wx_api_url: + try: + return requests.get(self.wx_api_url + '/consult', params={'account': account, 'content': content}) + except requests.exceptions.ConnectionError: + return None + client = ApiClient() diff --git a/commands/ai.py b/commands/ai.py new file mode 100644 index 00000000..0fad2c58 --- /dev/null +++ b/commands/ai.py @@ -0,0 +1,46 @@ +import os +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() + + +@cr.register('tuling123', 'chat', '聊天') +def tuling123(args_text, ctx_msg, internal=False): + url = 'http://www.tuling123.com/openapi/api' + data = { + 'key': os.environ.get('TURING123_API_KEY'), + 'info': args_text, + 'userid': get_source(ctx_msg) + } + resp = requests.post(url, data=data) + if resp.status_code == 200: + json = resp.json() + if internal: + return json + if int(json.get('code', 0)) == 100000: + reply = json.get('text', '') + else: + # Is not text type + reply = '腊鸡图灵机器人返回了一堆奇怪的东西,就不发出来了' + else: + if internal: + return None + 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 + return None diff --git a/commands/core.py b/commands/core.py index d56b7bac..36d3040f 100644 --- a/commands/core.py +++ b/commands/core.py @@ -1,8 +1,4 @@ -import os -import requests - from command import CommandRegistry -from little_shit import get_source from apiclient import client as api __registry__ = cr = CommandRegistry() @@ -16,31 +12,6 @@ def echo(args_text, ctx_msg, internal=False): return api.send_message(args_text, ctx_msg) -@cr.register('tuling123', 'chat', '聊天') -def tuling123(args_text, ctx_msg, internal=False): - url = 'http://www.tuling123.com/openapi/api' - data = { - 'key': os.environ.get('TURING123_API_KEY'), - 'info': args_text, - 'userid': get_source(ctx_msg) - } - resp = requests.post(url, data=data) - if resp.status_code == 200: - json = resp.json() - if internal: - return json - if int(json.get('code', 0)) == 100000: - reply = json.get('text', '') - else: - # Is not text type - reply = '腊鸡图灵机器人返回了一堆奇怪的东西,就不发出来了' - else: - if internal: - return None - reply = '腊鸡图灵机器人出问题了,先不管他,过会儿再玩他' - echo(reply, ctx_msg) - - @cr.register('help', '帮助', '用法', '使用帮助', '使用指南', '使用说明', '使用方法', '怎么用') def help(_, ctx_msg): echo( diff --git a/commands/simpletools.py b/commands/simpletools.py index b2cc5579..3e7d804d 100644 --- a/commands/simpletools.py +++ b/commands/simpletools.py @@ -4,7 +4,7 @@ import requests from lxml import etree from command import CommandRegistry -from commands import core +from commands import core, ai __registry__ = cr = CommandRegistry() @@ -73,14 +73,14 @@ def weather(args_text, ctx_msg): core.echo('请在命令后加上要查的城市哦~(命令和城市用空格或逗号隔开)', ctx_msg) return - data = core.tuling123(city + '天气', ctx_msg, internal=True) + data = ai.tuling123(city + '天气', ctx_msg, internal=True) core.echo(data.get('text', ''), ctx_msg) @cr.register('joke') @cr.register('笑话', '说笑话', '说个笑话') def weather(_, ctx_msg): - data = core.tuling123('说个笑话', ctx_msg, internal=True) + data = ai.tuling123('说个笑话', ctx_msg, internal=True) core.echo(data.get('text', ''), ctx_msg) @@ -91,7 +91,7 @@ def weather(args_text, ctx_msg): if not query: core.echo('请在命令后加上要查的关键词哦~(命令和关键词用空格或逗号隔开)', ctx_msg) return - data = core.tuling123('百科 ' + query, ctx_msg, internal=True) + data = ai.tuling123('百科 ' + query, ctx_msg, internal=True) core.echo(data.get('text', ''), ctx_msg) diff --git a/config.py b/config.py index c8aa4c6f..f85bd7ea 100644 --- a/config.py +++ b/config.py @@ -1,6 +1,6 @@ config = { 'fallback_command': 'natural_language.process', - 'fallback_command_after_nl_processors': 'core.tuling123', + 'fallback_command_after_nl_processors': 'ai.tuling123', 'command_start_flags': ('/', '/', '来,', '来,'), # Add '' (empty string) here to allow commands without start flags 'command_name_separators': ('->', '::', '/'), # Regex 'command_args_start_flags': (',', ':', ',', ', ', ':', ': '), # Regex