基于 Serverless 与企业微信 WebHook 构建 nCoV 疫情监控机器人
近期疫情动态牵动人心,为便于公众及时获取相关信息,笔者利用业余时间开发了一款 nCoV 疫情监控小助手。该工具的核心机制是通过企业微信的 WebHook 接口向用户推送疫情数据。下文将分享基于 Serverless 架构的整体代码思路与部署方案。作者:tabor
实现效果
预期达成的最终效果如下所示:

首要解决的是数据来源问题。虽然可以使用 Python 爬虫自行采集,但考虑到便捷性,直接采用已集成现有 API 的开源项目 2026-nCoV-Crawler 更为高效。具备能力的开发者也可选择自行部署 Python 环境,但这并非本次教程的重点,故不再赘述。
获取数据后,需考虑如何将其传输至服务器并触发执行。尽管使用 CVM(云服务器)可行,但其配置繁琐且资源消耗较大,需手动搭建完整运行环境。因此,本方案选用 Serverless 模式进行部署,以简化运维流程。
核心逻辑
接下来审视整体业务的代码部分,这是机器人的核心所在。以下展示请求三次接口的代码示例:
<?php
function main_handler($event, $context) {
// 广东省情况
$curlsz = curl_init();
curl_setopt_array($curlsz, array(
CURLOPT_URL => "https://lab.isaaclin.cn/nCoV/api/area?latest=0&province=%E5%B9%BF%E4%B8%9C%E7%9C%81",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 3000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: lab.isaaclin.cn",
"Postman-Token: 680e5ea7-5c2e-4fb6-9295-7e336f2252c6,abd73e01-2a60-42b5-9bbe-92aa83805a7e",
"User-Agent: PostmanRuntime/7.15.0",
"accept-encoding: gzip, deflate",
"cache-control: no-cache"
),
));
$responsesz = curl_exec($curlsz);
$echo_responsesz = json_decode($responsesz, true);
$err = curl_error($curlsz);
curl_close($curlsz);
// 湖北省情况
$curlhb = curl_init();
curl_setopt_array($curlhb, array(
CURLOPT_URL => "https://lab.isaaclin.cn/nCoV/api/area?latest=0&province=%E6%B9%96%E5%8C%97%E7%9C%81",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 3000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: lab.isaaclin.cn",
"Postman-Token: 680e5ea7-5c2e-4fb6-9295-7e336f2252c6,abd73e01-2a60-42b5-9bbe-92aa83805a7e",
"User-Agent: PostmanRuntime/7.15.0",
"accept-encoding: gzip, deflate",
"cache-control: no-cache"
),
));
$responsehb = curl_exec($curlhb);
$echo_responsehb = json_decode($responsehb, true);
$err = curl_error($curlhb);
curl_close($curlhb);
// 全国总体情况
$curlall = curl_init();
curl_setopt_array($curlall, array(
CURLOPT_URL => "https://lab.isaaclin.cn/nCoV/api/overall",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 3000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: lab.isaaclin.cn",
"Postman-Token: 680e5ea7-5c2e-4fb6-9295-7e336f2252c6,abd73e01-2a60-42b5-9bbe-92aa83805a7e",
"User-Agent: PostmanRuntime/7.15.0",
"accept-encoding: gzip, deflate",
"cache-control: no-cache"
),
));
$responseall = curl_exec($curlall);
$echo_responseall = json_decode($responseall, true);
$err = curl_error($curlall);
curl_close($curlall);
//判断是否为深圳地域(这里逻辑写的比较简单,但是够用了)
if ($echo_responsesz['results'][0]['cities'][0]['cityName'] == '深圳') {
$echo_responseszqz = $echo_responsesz['results'][0]['cities'][0]['confirmedCount'];
$echo_responseszys = $echo_responsesz['results'][0]['cities'][0]['suspectedCount'];
$echo_responseszzy = $echo_responsesz['results'][0]['cities'][0]['curedCount'];
$echo_responseszsw = $echo_responsesz['results'][0]['cities'][0]['deadCount'];
} else {
$echo_responseszqz = $echo_responsesz['results'][0]['cities'][1]['confirmedCount'];
$echo_responseszys = $echo_responsesz['results'][0]['cities'][1]['suspectedCount'];
$echo_responseszzy = $echo_responsesz['results'][0]['cities'][1]['curedCount'];
$echo_responseszsw = $echo_responsesz['results'][0]['cities'][1]['deadCount'];
}
if ($err) {
echo "cURL Error #:" . $err;
} else {
//疫情监控告警机器人
$sc = $sc=" **2026-nCoV 疫情信息同步:** \n
> 全国疫情:
> 确诊人数<font color=\"info\">".$echo_responseall['results'][0]['confirmedCount']."</font>,疑似感染人数<font color=\"info\">".$echo_responseall['results'][0]['suspectedCount']."</font>,治愈人数<font color=\"info\">".$echo_responseall['results'][0]['curedCount']."</font>,死亡人数<font color=\"info\">".$echo_responseall['results'][0]['deadCount']."</font>\n
> 广东省:
> 确诊人数<font color=\"info\">".$echo_responsesz['results'][0]['confirmedCount']."</font>,疑似感染人数<font color=\"info\">".$echo_responsesz['results'][0]['suspectedCount']."</font>,治愈人数<font color=\"info\">".$echo_responsesz['results'][0]['curedCount']."</font>,死亡人数<font color=\"info\">".$echo_responsesz['results'][0]['deadCount']."</font>\n
> 湖北省:
> 确诊人数<font color=\"info\">".$echo_responsehb['results'][0]['confirmedCount']."</font>,疑似感染人数<font color=\"info\">".$echo_responsehb['results'][0]['suspectedCount']."</font>,治愈人数<font color=\"info\">".$echo_responsehb['results'][0]['curedCount']."</font>,死亡人数<font color=\"info\">".$echo_responsehb['results'][0]['deadCount']."</font>\n
> 深圳市:
> 确诊人数<font color=\"info\">".$echo_responseszqz."</font>,疑似感染人数<font color=\"info\">".$echo_responseszys."</font>,治愈人数<font color=\"info\">".$echo_responseszzy."</font>,死亡人数<font color=\"info\">".$echo_responseszsw."</font>\n
> <font color=\"info\">".$echo_responseall['results'][0]['note1']."</font>
> <font color=\"info\">".$echo_responseall['results'][0]['note2']."</font>
> <font color=\"info\">".$echo_responseall['results'][0]['note3']."</font>
> <font color=\"info\">".$echo_responseall['results'][0]['remark1']."</font>
> <font color=\"info\">".$echo_responseall['results'][0]['remark2']."</font>
> <font color=\"info\"> 信息出处:".$echo_responseall['results'][0]['generalRemark']."</font> \n
>[更多数据请查看](https://news.qq.com/zt2026/page/feiyan.htm) \n
";
$post = array('msgtype' => 'markdown', 'markdown' => array('content' => $sc));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=", //这里的地址填写为企业微信的HOOK路径,https://work.weixin.qq.com/api/doc/90000/90136/91770
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($post,JSON_UNESCAPED_UNICODE),
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Postman-Token: ab32082b-ce64-4832-b51f-8f2f1b3e98ef"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
return "运行成功";
}
}
?>
逻辑是否十分简洁?主要步骤即为请求数据源并将结果发送出去。
随后重点探讨如何将业务代码上传至云端。 此处选用的云端平台是腾讯云 Serverless 服务 SCF云函数 。整个部署及使用过程均免费,对于小型项目而言,免费额度完全足够,开发者无需担心额外费用。
在 Serverless 部署方面,选用目前较为流行的 Serverless Framework。其使用及部署同样完全免费,下面将详细介绍具体的部署步骤。
安装 Serverless 框架
第一步,安装 Serverless Framework 开发框架:
$ npm install -g serverless
接着,创建一个函数目录:
$ mkdir nCov-function
$ cd nCov-function
相关函数目录的结构内容如下:
|- code
|- index.php // 这里就是上面的业务代码存放位置
|- serverless.yml //serverless 配置文件
配置 Yml 文件
接下来进入关键环节,即配置函数 yml 文件:
# serverless.yml
myFunction:
component: "@serverless/tencent-scf" //引用tencent-scf component
inputs:
name: nCoVFunction //函数名称
enableRoleAuth: true
codeUri: ./code //代码本地存放位置
handler: index.main_handler
runtime: Php5
region: ap-shanghai //函数运行地域
description: My Serverless nCoV Function.
memorySize: 128 //运行内存
timeout: 20 //超时时间
exclude:
- .gitignore
- .git/**
- node_modules/**
- .serverless
- .env
include:
- ./nCoVFunction.zip
environment:
variables:
TEST: vale
vpcConfig:
subnetId: ''
vpcId: ''
events:
- timer: // 定时触发器
name: timer
parameters:
cronExpression: '0 0 10,21 * * * *' //明天早上10点,晚上21点
enable: true
准备工作完成后,即可直接部署 SLS。
部署到云端
通过 sls 命令(serverless 的缩写)执行部署,并可添加 –debug 参数以查看部署过程中的详细信息:
taborchen$ sls --debug
DEBUG ─ Resolving the template's static variables.
DEBUG ─ Collecting components from the template.
DEBUG ─ Downloading any NPM components found in the template.
DEBUG ─ Analyzing the template's components dependencies.
DEBUG ─ Creating the template's components graph.
DEBUG ─ Syncing template state.
DEBUG ─ Executing the template's components graph.
DEBUG ─ Compressing function nCoVFunction file to /Users/taborchen/Desktop/工作/yiqing/.ser
verless/nCoVFunction.zip.
DEBUG ─ Compressed function nCoVFunction file successful
DEBUG ─ Uploading service package to cos[sls-cloudfunction-ap-shanghai-code]. sls-cloudfunc
tion-default-nCoVFunction-1580960644.zip
DEBUG ─ Uploaded package successful /Users/taborchen/Desktop/工作/yiqing/.serverless/nCoVFu
nction.zip
DEBUG ─ Creating function nCoVFunction
DEBUG ─ Created function nCoVFunction successful
DEBUG ─ Setting tags for function nCoVFunction
DEBUG ─ Creating trigger for function nCoVFunction
DEBUG ─ Created timer trigger timer for function nCoVFunction success.
DEBUG ─ Deployed function nCoVFunction successful
运行结果如下所示:

至此,一个基于 nCoV 的在线触发函数机器人便已完成~操作是否简单?快来动手尝试吧~
传送门:
GitHub: github.com/serverless
官网:serverless.com
以上就是全部内容,希望大家喜欢!