当前位置: 移动技术网 > IT编程>开发语言>其他编程 > 基于 Serverless +企业微信打造 nCoV 疫情监控小助手

基于 Serverless +企业微信打造 nCoV 疫情监控小助手

2020年05月10日  | 移动技术网IT编程  | 我要评论

最近的一些疫情信息很让人揪心,为了方便大家掌握疫情信息,在空闲之余做了一个关于 ncov 的疫情监控小助手。主要的功能是通过企业微信的 webhook 来推送疫情信息。这里将使用 serverless 的整体代码思路和架构方式分享给大家。本文作者:tabor

实现效果

我们想要实现的大致的效果是这样的:

首先,我们需要解决的是数据来源问题,这里我们可以使用 python 爬虫来做这件事情,但是由于个人比较懒所以直接用的 2019-ncov-crawler  ,这个项目已经集成了现有的 api,所以我们直接调用即可。当然有能力的同学也可以自己部署 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=" **2019-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/zt2020/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

官网:

好了,就给大家介绍到这来,希望大家喜欢!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网