{"id":16347090,"url":"https://github.com/ospoon/ewgn","last_synced_at":"2025-09-11T08:41:14.164Z","repository":{"id":105556610,"uuid":"441490088","full_name":"OSpoon/ewgn","owner":"OSpoon","description":"ewgn《企业微信群机器人实战》","archived":false,"fork":false,"pushed_at":"2021-12-24T14:48:33.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-19T05:06:23.859Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OSpoon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-24T14:46:29.000Z","updated_at":"2022-04-12T01:00:50.000Z","dependencies_parsed_at":"2024-03-25T05:15:23.768Z","dependency_job_id":null,"html_url":"https://github.com/OSpoon/ewgn","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/OSpoon/ewgn","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OSpoon%2Fewgn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OSpoon%2Fewgn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OSpoon%2Fewgn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OSpoon%2Fewgn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OSpoon","download_url":"https://codeload.github.com/OSpoon/ewgn/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OSpoon%2Fewgn/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265554986,"owners_count":23787311,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T00:39:37.409Z","updated_at":"2025-07-17T00:37:04.699Z","avatar_url":"https://github.com/OSpoon.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ewgn《企业微信群机器人实战》\n\n\u003e Dear，大家好，我是“前端小鑫同学”，😇长期从事前端开发，安卓开发，热衷技术，在编程路上越走越远～\n\n\n---\n\n### 背景介绍\n我们公司一直在用企业微信来做员工的沟通工具，不少群里面都有添加一下群聊机器人来做一下任务的提醒，那么做为群聊机器人的一个应用场景自然就是执行定义任务，如定时发送会议提醒，周报填写提醒等等，那往往我们就需要有一台不关机的个人PC或者云服务器来支持群聊机器人的长时间运行（Windows系统中的广告可以在任务计划程序中找到并关闭），个人自用的PC电脑其实不太适合长时间的运行（Windows谁用谁知道，没多久就会变卡），云服务器也需要花费不少的费用，所以我们就需要有一个**Serverless**的平台来解决这个问题（免费的额度也够用了，绝对超值）。\n### 案例介绍\n我们通过采用腾讯云平台中**Serverless**产品下的**云函数**来做服务支撑完成一个企业微信群机器人定时每天早8点发送当天天气情况的一个案例，实现此案例你需要做如下准备工作：\n\n1. 注册腾讯云平台：[https://cloud.tencent.com/](https://cloud.tencent.com/)\n1. 注册企业微信平台：[https://work.weixin.qq.com/](https://work.weixin.qq.com/)\n1. 使用VSCodeIDE并安装插件《Tencent Serverless Toolkit for VS Code》\n### 企微群机器人如何发送消息：\n实现企微群机器人消息发送主要是通过向分配给机器人的Webhook地址发送请求来完成的，如果你是群主当你拉机器人进群后查看机器人信息就可以得到机器人对应的Webhook地址了，特别特别要注意：一定要保护好机器人的webhook地址，避免泄漏！不要分享到github、博客等可被公开查阅的地方，否则坏人就可以用你的机器人来发垃圾消息了。。\n\n- 企微群机器人配置说明：[https://work.weixin.qq.com/api/doc/90000/90136/91770](https://work.weixin.qq.com/api/doc/90000/90136/91770)\n\n![image.png](https://cdn.nlark.com/yuque/0/2021/png/2373519/1640353287394-17076616-7a71-468f-bbc8-965bccea4af2.png#clientId=u732b3123-d254-4\u0026from=paste\u0026id=u1ce3988a\u0026margin=%5Bobject%20Object%5D\u0026name=image.png\u0026originHeight=470\u0026originWidth=383\u0026originalType=binary\u0026ratio=1\u0026size=58948\u0026status=done\u0026style=none\u0026taskId=u4b3ca5e9-8f33-4941-bd95-b7937ebceda)\n#### 项目环境准备：\n\n1. 初始化项目：`npm init -y`\n1. 初始化Ts配置：`tsc --init`\n1. 安装axios来实现请求数据：`yarn add axios`\n#### 模块划分：\n\n1. model.ts：消息模型（对应API文档），创建消息模型函数；\n```typescript\n/**\n * 消息类型\n */\nexport enum MsgType {\n    /** markdown类型 */\n    markdown = \"markdown\",\n}\n\n/**\n * markdow消息模型\n */\nexport type MarkdownContext = {\n    /** markdown内容，最长不超过4096个字节，必须是utf8编码 */\n    content: string,\n}\n\n/**\n * 消息模型基类\n */\nexport type Message\u003cT\u003e = {\n    msgtype: MsgType \u0026 T,\n    [type: string]: T,\n}\n\n/**\n * 创建待发送消息\n * @param type \n * @param content \n * @returns \n */\nfunction createMessage\u003cT\u003e(type: MsgType, content: T) {\n    return {\n        msgtype: type,\n        [type]: content,\n    } as Message\u003cT\u003e;\n}\n\n/**\n * 创建markdown类型的消息\n * @param content \n * @returns \n */\nexport function createFileMessage(content: FileContext) {\n    return createMessage\u003cFileContext\u003e(MsgType.file, content);\n}\n```\n\n2. notice.ts：发送各类型通知函数；\n```typescript\nimport axios from 'axios';\nimport { createFileMessage, createImageMessage, createMarkdownMessage, createNewsMessage, createTextMessage, FileContext, ImageContext, MarkdownContext, Message, NewsContext, TextContext } from './model';\n\nfunction request\u003cT\u003e(webhook: string, message: Message\u003cT\u003e) {\n    if (!webhook) throw new Error(\"请设置正确机器人的webhook地址\");\n    axios.post(webhook, message).then(res =\u003e {\n        const { status, data } = res;\n        if (status === 200 \u0026\u0026 data) {\n            console.log(data.errcode === 0 ? \"发送成功\" : data.errmsg);\n        }\n    }).catch(err =\u003e {\n        console.log(err);\n    })\n}\n\n/** 发起Markdown类型消息通知 */\nexport const requestMDNotice = (webhook: string, mdContext: MarkdownContext) =\u003e request\u003cMarkdownContext\u003e(webhook, createMarkdownMessage(mdContext));\n```\n\n3. index.ts：入口执行天气信息获取和调用发送Markdown类型消息。\n```typescript\nimport { requestMDNotice } from \"./notice\";\nimport axios from 'axios';\nimport { AMAP_WEATHER_API, ENTERPRISE_WECHAT_ROBOT_WEB_HOOK } from \"./config\";\naxios.get(AMAP_WEATHER_API).then(res =\u003e {\n    const { status, data } = res;\n    if (status === 200 \u0026\u0026 data) {\n        if (data.status === '1' \u0026\u0026 data.infocode === '10000') {\n            let message = ''\n            data.lives.forEach((live: any) =\u003e {\n                message += `\n##### 今天${live.province}，${live.city}天气情况\n* 天气： ${live.weather}\n* 气温： ${live.temperature} 摄氏度\n* 风向： ${live.winddirection}\n* 风力： ${live.windpower} 级\n* 湿度： ${live.humidity}\n* 数据发布的时间： ${live.reporttime}\\n\n                `\n            });\n            // 向企业微信群发送MD格式的通知\n            requestMDNotice(ENTERPRISE_WECHAT_ROBOT_WEB_HOOK, {\n                content: message\n            })\n        }\n    }\n})\n```\n\n4. 高德天气查询API和机器人Webhook地址配置：\n```typescript\n// 企业微信群机器人webhook地址\nexport const  ENTERPRISE_WECHAT_ROBOT_WEB_HOOK = \"《请填写你自己机器人的webhook地址》\";\n// 高德天气查询API,城市地址在高德开发文档查询\nexport const  AMAP_WEATHER_API = \"《请填写你自己申请天气查询地址》\"\n```\n#### 运行调试：\n\n- 通过ts-node直接执行我们的入口文件：`ts-node .\\src\\index.ts`，输出发送成功后就可以在企微群看到最新的消息了。\n\n![image.png](https://cdn.nlark.com/yuque/0/2021/png/2373519/1640354130557-c3ccb73b-ef7c-4be1-81c6-2a890a40b7e6.png#clientId=u732b3123-d254-4\u0026from=paste\u0026height=288\u0026id=u1171dff1\u0026margin=%5Bobject%20Object%5D\u0026name=image.png\u0026originHeight=1154\u0026originWidth=1125\u0026originalType=binary\u0026ratio=1\u0026size=246749\u0026status=done\u0026style=none\u0026taskId=u325dc52f-deae-42cd-9c2b-546998fa248\u0026width=281)\n### 云函数执行定时发送天气情况任务：\n#### 安装并认识腾讯云函数开发插件《Tencent Serverless Toolkit for VS Code》：\n\n- 拉取云端的云函数列表，并触发云函数。​\n- 在本地快速创建云函数项目。\n- 使用模拟的 COS、CMQ、CKafka、API 网关等触发器事件来触发函数运行。\n- 上传函数代码到云端，更新函数配置。\n- 在云端运行、调试函数代码。\n#### 初始化并编写云函数：\n\n1. 通过VSCode打开一个空的文件夹；\n1. 认准腾讯云Logo![image.png](https://cdn.nlark.com/yuque/0/2021/png/2373519/1640354355560-1b5ac413-5ee5-4a32-8bf7-aeafc93c6fd6.png#clientId=u732b3123-d254-4\u0026from=paste\u0026height=34\u0026id=u241bd680\u0026margin=%5Bobject%20Object%5D\u0026name=image.png\u0026originHeight=34\u0026originWidth=44\u0026originalType=binary\u0026ratio=1\u0026size=772\u0026status=done\u0026style=none\u0026taskId=u60db04c7-d07d-42fa-bc3f-e630f2c5e07\u0026width=44)打开插件，第一次使用需要绑定用户凭证，绑定地域；\n1. 在本地函数窗口创建函数=\u003e选择Nodejs版本=\u003e填写函数名，得到如下项目基础模板：\n\n![image.png](https://cdn.nlark.com/yuque/0/2021/png/2373519/1640354664506-6301acdb-6881-4f02-b9b8-de7f2f68ea88.png#clientId=u732b3123-d254-4\u0026from=paste\u0026height=166\u0026id=u7f470576\u0026margin=%5Bobject%20Object%5D\u0026name=image.png\u0026originHeight=166\u0026originWidth=216\u0026originalType=binary\u0026ratio=1\u0026size=5948\u0026status=done\u0026style=none\u0026taskId=u32005fca-dcc8-4eca-a786-e0736ac30ef\u0026width=216)\n\n4. 将我们ts编写的机器人发送通知的代码编译为js版本，直接运行`tsc`后将得到的内容覆盖云函数的`src`目录（index.js文件需要copy内容到main_handler函数中）；\n4. 因为我们有实用`axios`模块来发送请求，所以云函数项目中也需要安装：进入云函数项目的`**src**`目录执行：`npm init -y \u0026\u0026 yarn add axios`；\n#### 上传并在云端部署调试\n\n- 云函数的开发到此就完成了，通过本地函数窗口执行上传云函数（node_module如果内容过多建议先在控制台进行安装），并在控制台执行部署和测试；\n\n![image.png](https://cdn.nlark.com/yuque/0/2021/png/2373519/1640355408690-277d851b-906b-4380-aba4-7ec246f4bfee.png#clientId=u732b3123-d254-4\u0026from=paste\u0026height=432\u0026id=u3b8bb59b\u0026margin=%5Bobject%20Object%5D\u0026name=image.png\u0026originHeight=575\u0026originWidth=1018\u0026originalType=binary\u0026ratio=1\u0026size=46285\u0026status=done\u0026style=none\u0026taskId=uff4c30b3-87e3-436a-86d6-c3c1d8004c0\u0026width=764)\n#### 配置触发管理（定时任务）\n\n- 在触发管理菜单中创建新的触发器来执行定时任务Cron表达式通过[https://crontab.guru/every-day](https://crontab.guru/every-day)查询调整，提交后即可完成，期待明早的消息提醒吧。\n\n![image.png](https://cdn.nlark.com/yuque/0/2021/png/2373519/1640355926812-7e3f760e-ee33-4bfd-83ca-fdf30ec69367.png#clientId=u732b3123-d254-4\u0026from=paste\u0026height=437\u0026id=uWfEP\u0026margin=%5Bobject%20Object%5D\u0026name=image.png\u0026originHeight=437\u0026originWidth=685\u0026originalType=binary\u0026ratio=1\u0026size=30319\u0026status=done\u0026style=none\u0026taskId=u5e6b1864-50fc-4845-988d-61f77a6ca4e\u0026width=685)![image.png](https://cdn.nlark.com/yuque/0/2021/png/2373519/1640355965746-b3ce5f13-c160-476a-b7f5-51201efd7f40.png#clientId=u732b3123-d254-4\u0026from=paste\u0026height=525\u0026id=u19f51a57\u0026margin=%5Bobject%20Object%5D\u0026name=image.png\u0026originHeight=525\u0026originWidth=817\u0026originalType=binary\u0026ratio=1\u0026size=31816\u0026status=done\u0026style=none\u0026taskId=ude35c80e-682e-46cc-a38f-3d3e7644080\u0026width=817)\n### 结语：\n本篇涉及到的内容点有：高德开发平台天气查询API使用，腾讯云Serverless云函数使用，企微群机器人配置及API，共同完成了今天的实战案例，你还有什么有趣的应用场景呢？说说看？😂\n\n---\n\n**欢迎关注我的公众号“前端小鑫同学”，原创技术文章第一时间推送。**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fospoon%2Fewgn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fospoon%2Fewgn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fospoon%2Fewgn/lists"}