Merge pull request #4 from SuperHuangXu/patch-1

增加 TypeScript 版本函数
This commit is contained in:
Easy 2021-06-04 10:32:38 +08:00 committed by GitHub
commit 08fc56faeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 50 additions and 0 deletions

View File

@ -126,6 +126,56 @@ ret = send_to_wecom("推送测试\r\n测试换行", "企业ID③", "应用ID①"
print( ret ); print( ret );
``` ```
TypeScript 版:
```typescript
import request from 'superagent'
async function sendToWecom(body: {
text: string
wecomCId: string
wecomSecret: string
wecomAgentId: string
wecomTouid?: string
}): Promise<{ errcode: number; errmsg: string; invaliduser: string }> {
body.wecomTouid = body.wecomTouid ?? '@all'
const getTokenUrl = `https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${body.wecomCId}&corpsecret=${body.wecomSecret}`
const getTokenRes = await request.get(getTokenUrl)
const accessToken = getTokenRes.body.access_token
if (accessToken?.length <= 0) {
throw new Error('获取 accessToken 失败')
}
const sendMsgUrl = `https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${accessToken}`
const sendMsgRes = await request.post(sendMsgUrl).send({
touser: body.wecomTouid,
agentid: body.wecomAgentId,
msgtype: 'text',
text: {
content: body.text,
},
duplicate_check_interval: 600,
})
return sendMsgRes.body
}
```
使用实例:
```typescript
sendToWecom({
text: '推送测试\r\n测试换行',
wecomAgentId: '应用ID①',
wecomSecret: '应用secret②',
wecomCId: '企业ID③',
})
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})
```
其他版本的函数可参照上边的逻辑自行编写欢迎PR。 其他版本的函数可参照上边的逻辑自行编写欢迎PR。