Skip to content

Commit

Permalink
Merge pull request #5 from zhayujie/feature-wechat-mp
Browse files Browse the repository at this point in the history
feat: add wechat company service account
  • Loading branch information
zhayujie committed Feb 15, 2023
2 parents b13627a + 230ed15 commit 8b9edad
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 51 deletions.
152 changes: 142 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@
**模型:**

- [x] ChatGPT
- ...

**应用:**

- [ ] 终端
- [ ] Web
- [x] 个人微信
- [x] 公众号
- [x] 公众号 (个人/企业)
- [ ] 企业微信
- [ ] Telegram
- [ ] QQ
- [ ] 钉钉
- ...


- [ ] 飞书

# 快速开始

Expand All @@ -39,24 +36,159 @@ cd bot-on-anything/
### 2.配置说明

核心配置文件为 `config.json`
核心配置文件为 `config.json`项目中提供了模板文件 `config-template.json` ,可以从模板复制生成最终生效的 `config.json` 文件:

```bash
cp config-template.json config.json
```

配置文件结构如下:

```bash
{
"model": {
"type" : "openai", # 选用的算法模型
"openai": {
# openAI配置
}
},
"channel": {
"type": "wechat_mp", # 需要接入的应用
"wechat": {
# 个人微信配置
},
"wechat_mp": {
# 公众号配置
}
}
}
```
配置文件在最外层分成 `model``channel` 两部分,model 部分为模型配置,其中的 `type` 指定了选用哪个模型;`channel` 部分包含了应用渠道的配置,`type` 字段指定了接入哪个应用,同时下方对应的配置块也会生效。

在使用时只需要更改 `model``channel` 配置块下的 `type` 字段,即可在任意模型和应用间完成切换,连接不同的通路。下面将依次介绍各个 模型 及 应用 的配置和运行过程。


## 二、选择模型

### 1.ChatGPT

#### 1.1 注册 OpenAI 账号

前往 [OpenAI注册页面](https://beta.openai.com/signup) 创建账号,参考这篇 [教程](https://www.cnblogs.com/damugua/p/16969508.html) 可以通过虚拟手机号来接收验证码。创建完账号则前往 [API管理页面](https://beta.openai.com/account/api-keys) 创建一个 API Key 并保存下来,后面需要在项目中配置这个key。

> 项目中使用的对话模型是 davinci,计费方式是约每 750 字 (包含请求和回复) 消耗 $0.02,图片生成是每张消耗 $0.016,账号创建有免费的 $18 额度,使用完可以更换邮箱重新注册。
#### 1.2 配置项说明

```bash
{
"model": {
"type" : "openai",

"openai": {
"api_key": "YOUR API KEY",
"conversation_max_tokens": 1000,
"character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。"
}
}
```
+ `api_key`:填入上面注册账号时创建的 `OpenAI API KEY`
+ `conversation_max_tokens`:表示能够记忆的上下文最大字数(一问一答为一组对话,如果累积的对话字数超出限制,就会优先移除最早的一组对话)
+ `character_desc` 配置中保存着你对机器人说的一段话,他会记住这段话并作为他的设定,你可以为他定制任何人格
## 三、运行应用
### 1.个人微信
与项目 [chatgpt-on-wechat](https://github.com/zhayujie/chatgpt-on-wechat) 的使用方式相同,目前接入个人微信可能导致账号被限制,暂时不建议使用。
配置项说明:
```bash
"channel": {
"type": "wechat",

"single_chat_prefix": ["bot", "@bot"],
"single_chat_reply_prefix": "[bot] ",
"group_chat_prefix": ["@bot"],
"group_name_white_list": ["ChatGPT测试群"],
"image_create_prefix": ["", "", "找一张"],

"wechat": {
}
}
```
个人微信的配置项放在和 `type` 同级的层次,表示这些为公共配置,会复用于其他应用。配置加载时会优先使用模块内的配置,如果未找到便使用公共配置。
在项目根目录下执行 `python3 app.py` 即可启动程序,用手机扫码后完成登录,使用详情参考 [chatgpt-on-wechat](https://github.com/zhayujie/chatgpt-on-wechat)。
### 2.个人订阅号
#### 2.1 依赖安装
安装 [werobot](https://github.com/offu/WeRoBot) 依赖:
```bash
pip3 install werobot
```
#### 2.2 配置
```bash
"channel": {
"type": "wechat_mp",

"wechat_mp": {
"token": "YOUR TOKEN", # token值
"port": "8088" # 程序启动监听的端口
}
}
```
#### 2.1 运行程序
在项目目录下运行 `python3 app.py`,终端显示如下则表示已成功运行:
## 三、选择应用
```
[INFO][2023-02-16 01:39:53][app.py:12] - [INIT] load config: ...
[INFO][2023-02-16 01:39:53][wechat_mp_channel.py:25] - [WX_Public] Wechat Public account service start!
Bottle v0.12.23 server starting up (using AutoServer())...
Listening on http://127.0.0.1:8088/
Hit Ctrl-C to quit.
```
### 1.微信
#### 2.2 设置公众号回调地址
在 [微信公众平台](https://mp.weixin.qq.com/) 中进入个人订阅号,启用服务器配置:
### 2.公众号
![wx_mp_config.png](docs/images/wx_mp_config.png)
- 服务器地址 (URL):在浏览器访问该URL需要能访问到服务器上运行的python程序 (默认为8088端口)
- 令牌 (Token):需和配置中的token一致
#### 2.3 使用
用户关注订阅号后,发送消息即可。
## 四、运行
> 注:用户发送消息后,微信后台会向配置的URL地址推送,但如果5s内未回复就会断开连接,同时重试3次,但往往请求openai接口不止5s。本项目中通过异步和缓存将5s超时限制优化至15s,但超出该时间仍无法正常回复。 同时每次5s连接断开时web框架会报错,待后续优化。
### 3.企业服务号
在企业服务号中,通过先异步访问openai接口,再通过客服接口主动推送用户的方式,解决了个人订阅号的15s超时问题。
企业服务号配置只需修改type为`wechat_mp_service`,配置块仍复用 `wechat_mp`,在基础上增加了 `app_id``app_secret` 两个配置项。
```bash
"channel": {
"type": "wechat_mp_service",

"wechat_mp": {
"token": "YOUR TOKEN", # token值
"port": "8088", # 程序启动监听的端口
"app_id": "YOUR APP ID", # appID
"app_secret": "YOUR APP SECRET" # app secret
}
}
```
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
logger.info("[INIT] load config: {}".format(config.conf()))

# create channel
channel = channel_factory.create_channel(config.conf().get("channel"))
channel = channel_factory.create_channel(config.conf().get("channel").get("type"))

# startup channel
channel.startup()
Expand Down
2 changes: 1 addition & 1 deletion bridge/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ def __init__(self):
pass

def fetch_reply_content(self, query, context):
return model_factory.create_bot(config.conf().get("model")).reply(query, context)
return model_factory.create_bot(config.conf().get("model").get("type")).reply(query, context)
8 changes: 6 additions & 2 deletions channel/channel_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ def create_channel(channel_type):
return WechatChannel()

elif channel_type == const.WECHAT_MP:
from channel.wechat.wechat_mp_channel import WechatPublicAccount
return WechatPublicAccount()
from channel.wechat.wechat_mp_channel import WechatSubsribeAccount
return WechatSubsribeAccount()

elif channel_type == const.WECHAT_MP_SERVICE:
from channel.wechat.wechat_mp_service_channel import WechatServiceAccount
return WechatServiceAccount()

else:
raise RuntimeError
23 changes: 12 additions & 11 deletions channel/wechat/wechat_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from channel.channel import Channel
from concurrent.futures import ThreadPoolExecutor
from common.log import logger
from config import conf
from common import const
from config import channel_conf_val, channel_conf
import requests
import io

Expand Down Expand Up @@ -45,15 +46,15 @@ def handle(self, msg):
to_user_id = msg['ToUserName'] # 接收人id
other_user_id = msg['User']['UserName'] # 对手方id
content = msg['Text']
match_prefix = self.check_prefix(content, conf().get('single_chat_prefix'))
match_prefix = self.check_prefix(content, channel_conf_val(const.WECHAT, 'single_chat_prefix'))
if from_user_id == other_user_id and match_prefix is not None:
# 好友向自己发送消息
if match_prefix != '':
str_list = content.split(match_prefix, 1)
if len(str_list) == 2:
content = str_list[1].strip()

img_match_prefix = self.check_prefix(content, conf().get('image_create_prefix'))
img_match_prefix = self.check_prefix(content, channel_conf_val(const.WECHAT, 'image_create_prefix'))
if img_match_prefix:
content = content.split(img_match_prefix, 1)[1].strip()
thread_pool.submit(self._do_send_img, content, from_user_id)
Expand All @@ -65,7 +66,7 @@ def handle(self, msg):
str_list = content.split(match_prefix, 1)
if len(str_list) == 2:
content = str_list[1].strip()
img_match_prefix = self.check_prefix(content, conf().get('image_create_prefix'))
img_match_prefix = self.check_prefix(content, channel_conf_val(const.WECHAT, 'image_create_prefix'))
if img_match_prefix:
content = content.split(img_match_prefix, 1)[1].strip()
thread_pool.submit(self._do_send_img, content, to_user_id)
Expand All @@ -88,11 +89,11 @@ def handle_group(self, msg):
elif len(content_list) == 2:
content = content_list[1]

config = conf()
match_prefix = (msg['IsAt'] and not config.get("group_at_off", False)) or self.check_prefix(origin_content, config.get('group_chat_prefix')) \
or self.check_contain(origin_content, config.get('group_chat_keyword'))
if ('ALL_GROUP' in config.get('group_name_white_list') or group_name in config.get('group_name_white_list') or self.check_contain(group_name, config.get('group_name_keyword_white_list'))) and match_prefix:
img_match_prefix = self.check_prefix(content, conf().get('image_create_prefix'))
match_prefix = (msg['IsAt'] and not channel_conf_val(const.WECHAT, "group_at_off", False)) or self.check_prefix(origin_content, channel_conf_val(const.WECHAT, 'group_chat_prefix')) \
or self.check_contain(origin_content, channel_conf_val(const.WECHAT, 'group_chat_keyword'))
group_white_list = channel_conf_val(const.WECHAT, 'group_name_white_list')
if ('ALL_GROUP' in group_white_list or group_name in group_white_list or self.check_contain(group_name, channel_conf_val(const.WECHAT, 'group_name_keyword_white_list'))) and match_prefix:
img_match_prefix = self.check_prefix(content, channel_conf_val(const.WECHAT, 'image_create_prefix'))
if img_match_prefix:
content = content.split(img_match_prefix, 1)[1].strip()
thread_pool.submit(self._do_send_img, content, group_id)
Expand All @@ -111,7 +112,7 @@ def _do_send(self, query, reply_user_id):
context['from_user_id'] = reply_user_id
reply_text = super().build_reply_content(query, context)
if reply_text:
self.send(conf().get("single_chat_reply_prefix") + reply_text, reply_user_id)
self.send(channel_conf_val(const.WECHAT, "single_chat_reply_prefix") + reply_text, reply_user_id)
except Exception as e:
logger.exception(e)

Expand Down Expand Up @@ -146,7 +147,7 @@ def _do_send_group(self, query, msg):
reply_text = super().build_reply_content(query, context)
if reply_text:
reply_text = '@' + msg['ActualNickName'] + ' ' + reply_text.strip()
self.send(conf().get("group_chat_reply_prefix", "") + reply_text, msg['User']['UserName'])
self.send(channel_conf_val(const.WECHAT, "group_chat_reply_prefix", "") + reply_text, msg['User']['UserName'])


def check_prefix(self, content, prefix_list):
Expand Down
11 changes: 6 additions & 5 deletions channel/wechat/wechat_mp_channel.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import werobot
import time
import config
from config import channel_conf
from common import const
from common.log import logger
from channel.channel import Channel
from concurrent.futures import ThreadPoolExecutor

robot = werobot.WeRoBot(token=config.fetch(const.WECHAT_MP).get('token'))
robot = werobot.WeRoBot(token=channel_conf(const.WECHAT_MP).get('token'))
thread_pool = ThreadPoolExecutor(max_workers=8)
cache = {}

Expand All @@ -15,14 +15,15 @@ def hello_world(msg):
logger.info('[WX_Public] receive public msg: {}, userId: {}'.format(msg.content, msg.source))
key = msg.content + '|' + msg.source
if cache.get(key):
# request time
cache.get(key)['req_times'] += 1
return WechatPublicAccount().handle(msg)
return WechatSubsribeAccount().handle(msg)


class WechatPublicAccount(Channel):
class WechatSubsribeAccount(Channel):
def startup(self):
logger.info('[WX_Public] Wechat Public account service start!')
robot.config['PORT'] = config.fetch(const.WECHAT_MP).get('port')
robot.config['PORT'] = channel_conf(const.WECHAT_MP).get('port')
robot.run()

def handle(self, msg, count=0):
Expand Down
36 changes: 36 additions & 0 deletions channel/wechat/wechat_mp_service_channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import werobot
from config import channel_conf
from common import const
from common.log import logger
from channel.channel import Channel
from concurrent.futures import ThreadPoolExecutor

robot = werobot.WeRoBot(token=channel_conf(const.WECHAT_MP).get('token'))
thread_pool = ThreadPoolExecutor(max_workers=8)

@robot.text
def hello_world(msg):
logger.info('[WX_Public] receive public msg: {}, userId: {}'.format(msg.content, msg.source))
return WechatServiceAccount().handle(msg)


class WechatServiceAccount(Channel):
def startup(self):
logger.info('[WX_Public] Wechat Public account service start!')
robot.config['PORT'] = channel_conf(const.WECHAT_MP).get('port')
robot.config["APP_ID"] = "YOUR APP ID"
robot.config["APP_SECRET"] = "YOUR APP SECRET"
robot.run()

def handle(self, msg, count=0):
context = {}
context['from_user_id'] = msg.source
thread_pool.submit(self._do_send, msg.content, context)
return "正在思考中..."


def _do_send(self, query, context):
reply_text = super().build_reply_content(query, context)
logger.info('[WX_Public] reply content: {}'.format(reply_text))
client = robot.client
client.send_text_message(context['from_user_id'], reply_text)
1 change: 1 addition & 0 deletions common/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# channel
WECHAT = "wechat"
WECHAT_MP = "wechat_mp"
WECHAT_MP_SERVICE = "wechat_mp_service"

# model
OPEN_AI = "openai"
Loading

0 comments on commit 8b9edad

Please sign in to comment.