Skip to content

Commit

Permalink
Merge pull request #20 from ilyydy/voice
Browse files Browse the repository at this point in the history
支持微信语音
  • Loading branch information
ilyydy authored Apr 25, 2023
2 parents bdae71a + e0f1afa commit a8f9607
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# [0.4.0](https://github.com/ilyydy/cf-openai/compare/v0.3.0...v0.4.0) (2023-04-25)


### Features

* 微信公众号支持语音消息 ([6726dac](https://github.com/ilyydy/cf-openai/commit/6726dacc9b65d8f7b43e347678c7f89cbced1f87))



# [0.3.0](https://github.com/ilyydy/cf-openai/compare/v0.2.1...v0.3.0) (2023-04-10)


Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [本地命令行部署](#本地命令行部署)
- [企业微信应用接入配置](#企业微信应用接入配置)
- [微信公众号接入配置](#微信公众号接入配置)
- [输入输出支持](#输入输出支持)
- [可用命令](#可用命令)
- [OpenAI 配置](#openai-配置)
- [全局配置](#全局配置)
Expand Down Expand Up @@ -98,6 +99,11 @@
4. 根据域名和自定义的 ID 得出第二步中服务器配置的服务器地址(URL)并进行配置,格式为 `https://${域名}/openai/wechat/${ID}`。如域名为 `xxx.com`,自定义的 ID 为 `id123`,则服务器地址(URL)为 `https://xxx.com/openai/wechat/id123`
5. 消息加解密方式一般选明文,启用服务器配置,验证接入成功后即可使用

### 输入输出支持

- 输入:文字、语音(需在公众号管理后台开通语音识别)
- 输出:文字

## 可用命令

输入使用时可忽略大小写
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cf-openai",
"version": "0.3.0",
"version": "0.4.0",
"devDependencies": {
"@cloudflare/workers-types": "^4.20230307.0",
"@commitlint/cli": "^17.5.0",
Expand Down
22 changes: 15 additions & 7 deletions src/controller/openai/platform/wechatBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,19 @@ export abstract class WeChatBaseHandler<T extends WeWork | WeChat> extends Base<
* 处理微信发来的消息,返回明文消息
*/
async _handleRecvMsg(recvMsg: wechatType.RecvPlainMsg | weworkType.RecvPlainMsg) {
if (recvMsg.MsgType !== 'text') {
return this.platform.genRespTextXmlMsg('只支持文字消息')
let content = ''
if (recvMsg.MsgType === 'text') {
content = recvMsg.Content.trim()
} else if (recvMsg.MsgType === 'voice') {
if (!recvMsg.Recognition) {
return this.platform.genRespTextXmlMsg('微信公众号未开通语音识别')
}
content = recvMsg.Recognition.trim()
} else {
return this.platform.genRespTextXmlMsg('只支持文字/语音消息')
}
recvMsg.Content = recvMsg.Content.trim()
const recvMsgTokenCount = estimateTokenCount(recvMsg.Content)

const recvMsgTokenCount = estimateTokenCount(content)
if (recvMsgTokenCount >= CONFIG.MAX_CHAT_TOKEN_NUM) {
return this.platform.genRespTextXmlMsg('输入太长,不能多于约 2000 个汉字')
}
Expand All @@ -104,10 +112,10 @@ export abstract class WeChatBaseHandler<T extends WeWork | WeChat> extends Base<
this.resetLogger()

if (GLOBAL_CONFIG.ECHO_MODE) {
return this.platform.genRespTextXmlMsg(recvMsg.Content)
return this.platform.genRespTextXmlMsg(content)
}

const cmdRes = await this.handleCommandMessage(recvMsg.Content)
const cmdRes = await this.handleCommandMessage(content)
if (cmdRes !== null) {
return cmdRes.success ? this.platform.genRespTextXmlMsg(cmdRes.data) : this.platform.genRespTextXmlMsg(cmdRes.msg)
}
Expand Down Expand Up @@ -163,7 +171,7 @@ export abstract class WeChatBaseHandler<T extends WeWork | WeChat> extends Base<
}

this.request.ctx.waitUntil(kvMsgTryTimes.setWithStringify(1))
const respMsg = await this.openAiHandle(recvMsg.Content, recvMsg.MsgId, recvMsgTokenCount)
const respMsg = await this.openAiHandle(content, recvMsg.MsgId, recvMsgTokenCount)
return this.platform.genRespTextXmlMsg(respMsg)
}

Expand Down

0 comments on commit a8f9607

Please sign in to comment.