From 72aa98458313f42c989f453a8c0dc3a9c59d9681 Mon Sep 17 00:00:00 2001 From: xsu Date: Tue, 1 Jun 2021 14:37:28 +0800 Subject: [PATCH 1/3] feat: add annotation --- README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/README.md b/README.md index a284074..ef4948d 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,50 @@ $ret = send_to_wecom("推送测试\r\n测试换行", "企业ID③", "应用ID① print_r( $ret ); ``` +PYTHON版: + +```python + import json,requests +def send_to_wecom(text,wecom_cid,wecom_secret,wecom_aid,wecom_touid='@all'): + get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={wecom_cid}&corpsecret={wecom_secret}" + response = requests.get(get_token_url).content + access_token = json.loads(response).get('access_token') + if access_token and len(access_token) > 0: + send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}' + data = { + "touser":wecom_touid, + "agentid":wecom_aid, + "msgtype":"text", + "text":{ + "content":text + }, + "duplicate_check_interval":600 + } + response = requests.post(send_msg_url,data=json.dumps(data)).content + return response + else: + return False + +``` + +使用实例: + +```python +ret = send_to_wecom("推送测试\r\n测试换行", "企业ID③", "应用ID①", "应用secret②"); +print( ret ); +``` + 其他版本的函数可参照上边的逻辑自行编写,欢迎PR。 +## 注解 +出现`接口请求正常,企业微信接受消息正常,个人微信无法收到消息`: +1. 进入「我的企业」 → 「[微信插件](https://work.weixin.qq.com/wework_admin/frame#profile/wxPlugin)」,拉到最下方,勾选 “允许成员在微信插件中接收和回复聊天消息” +[1622527548856 - IMGBED (图床)](https://www.imgbed.com/image/1622527548856.HPIRU) +2. 在企业微信客户端 「我」 → 「设置」 → 「新消息通知」中关闭 “仅在企业微信中接受消息” 限制条件 +[IMG 4753 - IMGBED (图床)](https://www.imgbed.com/image/img-4753.HPKPX) + + + + + From 92fbe774732daea43dd28f91bb098a8e94a5f13c Mon Sep 17 00:00:00 2001 From: xsu Date: Tue, 1 Jun 2021 14:53:00 +0800 Subject: [PATCH 2/3] fix: image link --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ef4948d..fce94ab 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ print_r( $ret ); PYTHON版: ```python - import json,requests +import json,requests def send_to_wecom(text,wecom_cid,wecom_secret,wecom_aid,wecom_touid='@all'): get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={wecom_cid}&corpsecret={wecom_secret}" response = requests.get(get_token_url).content @@ -131,12 +131,12 @@ print( ret ); ## 注解 出现`接口请求正常,企业微信接受消息正常,个人微信无法收到消息`: + 1. 进入「我的企业」 → 「[微信插件](https://work.weixin.qq.com/wework_admin/frame#profile/wxPlugin)」,拉到最下方,勾选 “允许成员在微信插件中接收和回复聊天消息” -[1622527548856 - IMGBED (图床)](https://www.imgbed.com/image/1622527548856.HPIRU) +![](https://img.ams1.imgbed.xyz/2021/06/01/HPIRU.jpg) + 2. 在企业微信客户端 「我」 → 「设置」 → 「新消息通知」中关闭 “仅在企业微信中接受消息” 限制条件 -[IMG 4753 - IMGBED (图床)](https://www.imgbed.com/image/img-4753.HPKPX) - - +![](https://img.ams1.imgbed.xyz/2021/06/01/HPKPX.jpg) From 36bdb71c38219f694b721c03e7f27264a9f61b20 Mon Sep 17 00:00:00 2001 From: bUBBLE Date: Thu, 3 Jun 2021 15:55:36 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20TypeScript=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/README.md b/README.md index fce94ab..cc7b424 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,56 @@ ret = send_to_wecom("推送测试\r\n测试换行", "企业ID③", "应用ID①" 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。