Skip to content

Commit

Permalink
docs: update README.md for wechatcom_app
Browse files Browse the repository at this point in the history
  • Loading branch information
lanvent committed Apr 24, 2023
1 parent be5a62f commit 6e14fce
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
52 changes: 38 additions & 14 deletions channel/wechatcom/README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,57 @@
# 企业微信应用号channel

> 详细文档暂无
企业微信官方提供了客服、应用等API,本channel使用的是企业微信的应用API的能力。因为未来可能还会开发客服能力,所以本channel的类型名叫作`wechatcom_app`

## 自建应用
`wechatcom_app` channel支持插件系统和图片声音交互等能力,除了无法加入群聊,作为个人使用的私人助理已绰绰有余。

- 在企业微信工作台自建应用
## 开始之前

建立应用后点击通过API接收消息,设置服务器地址,服务器地址是`http://url:port/wxcomapp`的形式,也可以不用域名,比如 `http://ip:port/wxcomapp`
- 在企业中确认自己拥有在企业内自建应用的权限。
- 如果没有权限或者是个人用户,也可创建未认证的企业。操作方式:登录手机企业微信,选择`创建/加入企业`来创建企业,类型请选择企业,企业名称可随意填写。
未认证的企业有100人的服务人数上限,其他功能与认证企业没有差异。

- 修改配置
本channel需安装的依赖与公众号一致,需要安装`wechatpy``web.py`,它们包含在`requirements-optional.txt`中。

在主目录下的`config.json`中填写以下配置项
## 使用方法

1.查看企业ID

- 扫码登陆[企业微信后台](https://work.weixin.qq.com)
- 选择`我的企业`,点击`企业信息`,记住该`企业ID`

2.创建自建应用

- 选择应用管理, 在自建区选创建应用来创建企业自建应用
- 上传应用logo,填写应用名称等项
- 创建应用后进入应用详情页面,记住`AgentId``Secert`

3.配置应用

- 在详情页如果点击`企业可信IP`的配置(没看到可以不管),填入你服务器的公网IP
- 点击`接收消息`下的启用API接收消息
- `URL`填写格式为`http://url:port/wxcomapp`,是程序监听的端口,默认是9898
如果是未认证的企业,url可直接使用服务器的IP。如果是认证企业,需要使用备案的域名,可使用二级域名。
- `Token`可随意填写,停留在这个页面
- 在程序根目录`config.json`中增加配置(**去掉注释**),`wechatcomapp_aes_key`是当前页面的`wechatcomapp_aes_key`

```python
# wechatcom的通用配置
"channel_type": "wechatcom_app",
"wechatcom_corp_id": "", # 企业微信公司的corpID
# wechatcomapp的配置
"wechatcomapp_token": "", # 企业微信app的token
"wechatcomapp_port": 9898, # 企业微信app的服务端口,不需要端口转发
"wechatcomapp_port": 9898, # 企业微信app的服务端口, 不需要端口转发
"wechatcomapp_secret": "", # 企业微信app的secret
"wechatcomapp_agent_id": "", # 企业微信app的agent_id
"wechatcomapp_aes_key": "", # 企业微信app的aes_key
```

- 运行程序
- 运行程序,在页面中点击保存,保存成功说明验证成功

4.连接个人微信

```python app.py```
选择`我的企业`,点击`微信插件`,下面有个邀请关注的二维码。微信扫码后,即可在微信中看到对应企业,在这里你便可以和机器人沟通。

在设置服务器页面点击保存
## 测试体验

- 添加可信IP
AIGC开放社区中已经部署了多个可免费使用的Bot,扫描下方的二维码会自动邀请你来体验。

在自建应用管理页下方,将服务器的IP添加到可信IP
<img width="360" src="./docs/images/aigcopen.png">
50 changes: 13 additions & 37 deletions channel/wechatcom/wechatcomapp_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ def __init__(self):
self.aes_key = conf().get("wechatcomapp_aes_key")
print(self.corp_id, self.secret, self.agent_id, self.token, self.aes_key)
logger.info(
"[wechatcom] init: corp_id: {}, secret: {}, agent_id: {}, token: {}, aes_key: {}".format(
self.corp_id, self.secret, self.agent_id, self.token, self.aes_key
)
"[wechatcom] init: corp_id: {}, secret: {}, agent_id: {}, token: {}, aes_key: {}".format(self.corp_id, self.secret, self.agent_id, self.token, self.aes_key)
)
self.crypto = WeChatCrypto(self.token, self.aes_key, self.corp_id)
self.client = WeChatClient(self.corp_id, self.secret) # todo: 这里可能有线程安全问题
Expand All @@ -46,7 +44,7 @@ def startup(self):
# start message listener
urls = ("/wxcomapp", "channel.wechatcom.wechatcomapp_channel.Query")
app = web.application(urls, globals(), autoreload=False)
port = conf().get("wechatcomapp_port", 8080)
port = conf().get("wechatcomapp_port", 9898)
web.httpserver.runsimple(app.wsgifunc(), ("0.0.0.0", port))

def send(self, reply: Reply, context: Context):
Expand All @@ -70,26 +68,18 @@ def send(self, reply: Reply, context: Context):
os.remove(amr_file)
except Exception:
pass
self.client.message.send_voice(
self.agent_id, receiver, response["media_id"]
)
logger.info(
"[wechatcom] sendVoice={}, receiver={}".format(reply.content, receiver)
)
self.client.message.send_voice(self.agent_id, receiver, response["media_id"])
logger.info("[wechatcom] sendVoice={}, receiver={}".format(reply.content, receiver))
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
img_url = reply.content
pic_res = requests.get(img_url, stream=True)
image_storage = io.BytesIO()
for block in pic_res.iter_content(1024):
image_storage.write(block)
if (sz := fsize(image_storage)) >= 10 * 1024 * 1024:
logger.info(
"[wechatcom] image too large, ready to compress, sz={}".format(sz)
)
logger.info("[wechatcom] image too large, ready to compress, sz={}".format(sz))
image_storage = compress_imgfile(image_storage, 10 * 1024 * 1024 - 1)
logger.info(
"[wechatcom] image compressed, sz={}".format(fsize(image_storage))
)
logger.info("[wechatcom] image compressed, sz={}".format(fsize(image_storage)))
image_storage.seek(0)
try:
response = self.client.media.upload("image", image_storage)
Expand All @@ -98,32 +88,22 @@ def send(self, reply: Reply, context: Context):
logger.error("[wechatcom] upload image failed: {}".format(e))
return

self.client.message.send_image(
self.agent_id, receiver, response["media_id"]
)
logger.info(
"[wechatcom] sendImage url={}, receiver={}".format(img_url, receiver)
)
self.client.message.send_image(self.agent_id, receiver, response["media_id"])
logger.info("[wechatcom] sendImage url={}, receiver={}".format(img_url, receiver))
elif reply.type == ReplyType.IMAGE: # 从文件读取图片
image_storage = reply.content
if (sz := fsize(image_storage)) >= 10 * 1024 * 1024:
logger.info(
"[wechatcom] image too large, ready to compress, sz={}".format(sz)
)
logger.info("[wechatcom] image too large, ready to compress, sz={}".format(sz))
image_storage = compress_imgfile(image_storage, 10 * 1024 * 1024 - 1)
logger.info(
"[wechatcom] image compressed, sz={}".format(fsize(image_storage))
)
logger.info("[wechatcom] image compressed, sz={}".format(fsize(image_storage)))
image_storage.seek(0)
try:
response = self.client.media.upload("image", image_storage)
logger.debug("[wechatcom] upload image response: {}".format(response))
except WeChatClientException as e:
logger.error("[wechatcom] upload image failed: {}".format(e))
return
self.client.message.send_image(
self.agent_id, receiver, response["media_id"]
)
self.client.message.send_image(self.agent_id, receiver, response["media_id"])
logger.info("[wechatcom] sendImage, receiver={}".format(receiver))


Expand All @@ -137,9 +117,7 @@ def GET(self):
timestamp = params.timestamp
nonce = params.nonce
echostr = params.echostr
echostr = channel.crypto.check_signature(
signature, timestamp, nonce, echostr
)
echostr = channel.crypto.check_signature(signature, timestamp, nonce, echostr)
except InvalidSignatureException:
raise web.Forbidden()
return echostr
Expand All @@ -152,9 +130,7 @@ def POST(self):
signature = params.msg_signature
timestamp = params.timestamp
nonce = params.nonce
message = channel.crypto.decrypt_message(
web.data(), signature, timestamp, nonce
)
message = channel.crypto.decrypt_message(web.data(), signature, timestamp, nonce)
except (InvalidSignatureException, InvalidCorpIdException):
raise web.Forbidden()
msg = parse_message(message)
Expand Down
Binary file added docs/images/aigcopen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6e14fce

Please sign in to comment.