Skip to content

Commit

Permalink
Update version to 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Hai Liang Wang committed Jul 22, 2020
1 parent ce60495 commit ea24cbd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 232 deletions.
129 changes: 8 additions & 121 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,146 +2,33 @@

[Chatopera 云服务](https://bot.chatopera.com/) 实现定制化聊天机器人服务。

为企业聊天机器人而生。

* 支持自定义知识库语料/查询知识库
* 支持多轮对话
* 支持屏蔽/取消屏蔽用户
* 查看聊天机器人详情
* 支持技能:心理问答 API, etc.

## 安装

```
pip install chatopera
```

## 使用说明

该SDK的详细使用说明[文档](https://docs.chatopera.com/products/chatbot-platform/index.html).

- 注册账号

```
https://bot.chatopera.com
```

- 集成

```
from chatopera import Chatbot
bot = Chatbot(BOT_APP_ID, BOT_APP_SECRET)
response = bot.faq("py", "你好")
```

Check out [demo](./demo.py) for details.


## APIs

聊天机器人即服务

实例化机器人

## Chatbot
```python
Chatbot(app_id,
app_secret=None,
Chatbot(app_id="机器人设置页面获取",
app_secret="机器人设置页面获取",
provider='https://bot.chatopera.com')
```

## 使用说明

### detail
```python
Chatbot.detail()
```

获得聊天机器人详情


### faq
```python
Chatbot.faq(user_id, text_message)
```

查询机器人知识库


### conversation
```python
Chatbot.conversation(user_id,
text_message,
branch='master',
is_debug=False)
```

查询机器人多轮对话


### mute
```python
Chatbot.mute(user_id)
```

屏蔽一个用户


### unmute
```python
Chatbot.unmute(user_id)
```

取消屏蔽一个用户


### ismute
```python
Chatbot.ismute(user_id)
```

查看一个用户是否被屏蔽


### profile
```python
Chatbot.profile(user_id)
```

查看用户画像


### chats
```python
Chatbot.chats(user_id, limit=20, page=1, sortby='-lasttime')
```

获得聊天历史


### psychSearch
```python
Chatbot.psychSearch(query, threshold=0.2)
```

技能:心理咨询查询接口
文档:[链接](https://docs.chatopera.com/products/psych-assistant/api.html#api-%E6%8E%A5%E5%8F%A3%E5%AE%9A%E4%B9%89)


### psychChat
```python
Chatbot.psychChat(channel, channel_id, user_id, text_message)
```

技能:心理咨询聊天接口
文档:[链接](https://docs.chatopera.com/products/psych-assistant/api.html#api-%E6%8E%A5%E5%8F%A3%E5%AE%9A%E4%B9%89)
快速开始,类接口定义和实例化文档等,参考 [文档中心](https://docs.chatopera.com/products/chatbot-platform/integration.html)

[https://docs.chatopera.com/products/chatbot-platform/integration.html](https://docs.chatopera.com/products/chatbot-platform/integration.html)

## 开源许可协议

Copyright (2018) [北京华夏春松科技有限公司](https://www.chatopera.com/)
Copyright (2018-2020) [北京华夏春松科技有限公司](https://www.chatopera.com/)

[Apache License Version 2.0](./LICENSE)

Copyright 2017-2018, [北京华夏春松科技有限公司](https://www.chatopera.com/). All rights reserved. This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited.
Copyright 2018-2020, [北京华夏春松科技有限公司](https://www.chatopera.com/). All rights reserved. This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited.

[![chatoper banner][co-banner-image]][co-url]

Expand Down
119 changes: 9 additions & 110 deletions app/chatopera/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ def __init__(
})

def command(self, method, path, payload=None):
"""
核心接口,详细介绍,https://docs.chatopera.com/products/chatbot-platform/integration.html
Parameters:
method (string): HTTP Action, delete, put, post, get, etc.
path (string): URL 路径
Returns:
response (dict): JSON格式 dict 数据结构
"""
headers = copy.deepcopy(self.default_headers)

method = method.upper()
Expand All @@ -83,113 +92,3 @@ def command(self, method, path, payload=None):
)

return json.loads(resp.text, encoding="utf-8")

def detail(self):
"""
获得聊天机器人详情
"""
return self.command(M_GET, "/")

def faq(self, user_id, text_message):
"""
查询机器人知识库
"""
return self.command(M_POST, "/faq/query", dict({
"fromUserId": user_id,
"query": text_message
}))

def conversation(
self,
user_id,
text_message,
branch="master",
is_debug=False):
"""
查询机器人多轮对话
"""
return self.command(M_POST, "/conversation/query", dict({
"fromUserId": user_id,
"textMessage": text_message,
"branch": branch,
"isDebug": is_debug
}))

def mute(self, user_id):
"""
屏蔽一个用户
"""
return self.command(M_POST, "/users/%s/mute" % user_id)

def unmute(self, user_id):
"""
取消屏蔽一个用户
"""
return self.command(M_POST, "/users/%s/unmute" % user_id)

def ismute(self, user_id):
"""
查看一个用户是否被屏蔽
"""
resp = self.command(M_POST, "/users/%s/ismute" % user_id)

if resp["rc"] == 0:
return resp["data"]["mute"]
else:
print("[warn] chatbot.ismute: %s" % resp)
return None

def profile(self, user_id):
"""
查看用户画像
"""
resp = self.command(M_GET, "/users/%s/profile" % user_id)
# add auth into headers
headers = copy.deepcopy(self.default_headers)

if resp["rc"] == 0:
return resp["data"]
else:
print("[warn] chatbot.profile: %s" % resp)
return None

def chats(self, user_id, limit=20, page=1, sortby="-lasttime"):
"""
获得聊天历史
"""
return self.command(M_GET, "/users/%s/chats?limit=%d&page=%d&sortby=%s" % (
user_id, limit, page, sortby))

def psychSearch(self, query, threshold=0.2):
"""
技能:心理咨询查询接口
文档:[链接](https://docs.chatopera.com/products/psych-assistant/api.html#api-%E6%8E%A5%E5%8F%A3%E5%AE%9A%E4%B9%89)
"""
resp = self.command(M_POST, "/skills/psych/search")

if "rc" in resp and resp["rc"] == 0:
return resp["data"]
if "rc" in resp and resp["rc"] == 2:
del resp["rc"]
return resp
else:
raise RuntimeError(
"Invalid response, error %s" % (resp["error"] if "error" in resp else None))

def psychChat(self, channel, channel_id, user_id, text_message):
"""
技能:心理咨询聊天接口
文档:[链接](https://docs.chatopera.com/products/psych-assistant/api.html#api-%E6%8E%A5%E5%8F%A3%E5%AE%9A%E4%B9%89)
"""
resp = self.command(M_POST, "/skills/psych/chat", dict({
"channel": channel,
"channelId": channel_id,
"userId": user_id,
"textMessage": text_message
}))

if "rc" in resp and resp["rc"] == 0:
return resp["data"]
else:
raise RuntimeError(
"Invalid response, error %s" % (resp["error"] if "error" in resp else None))
2 changes: 1 addition & 1 deletion app/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

setup(
name='chatopera',
version='1.2.0',
version='2.0.0',
description='定制化聊天机器人服务',
long_description=LONGDOC,
author='Hai Liang Wang',
Expand Down
18 changes: 18 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h1 class="code-line" data-line-start=1 data-line-end=2 ><a id="chatoperachatbot_1"></a>chatopera.chatbot</h1>
<p class="has-line-data" data-line-start="3" data-line-end="4">聊天机器人即服务</p>
<h2 class="code-line" data-line-start=6 data-line-end=7 ><a id="Chatbot_6"></a>Chatbot</h2>
<pre><code class="has-line-data" data-line-start="8" data-line-end="13" class="language-python">Chatbot(self,
app_id,
app_secret=<span class="hljs-keyword">None</span>,
provider=<span class="hljs-string">'https://bot.chatopera.com'</span>)
</code></pre>
<h3 class="code-line" data-line-start=15 data-line-end=16 ><a id="command_15"></a>command</h3>
<pre><code class="has-line-data" data-line-start="17" data-line-end="19" class="language-python">Chatbot.command(method, path, payload=<span class="hljs-keyword">None</span>)
</code></pre>
<p class="has-line-data" data-line-start="20" data-line-end="24">核心接口,详细介绍,<a href="https://docs.chatopera.com/products/chatbot-platform/integration.html">https://docs.chatopera.com/products/chatbot-platform/integration.html</a><br>
Parameters:<br>
method (string): HTTP Action, delete, put, post, get, etc.<br>
path (string): URL 路径</p>
<pre><code>Returns:
response (dict): JSON格式 dict 数据结构
</code></pre>

0 comments on commit ea24cbd

Please sign in to comment.