兴国资源网 Design By www.nnzcdc.com

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

实现效果

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

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

首先,我们需要解决的是数据来源问题,这里我们可以使用 python 爬虫来做这件事情,但是由于个人比较懒所以直接用的 2019-nCoV-Crawler  ,这个项目已经集成了现有的 API,所以我们直接调用即可。当然有能力的同学也可以自己部署 Python,我这边是自己部署的,但是这不是本次的重点,就不在赘述。

现在,我们有了数据,但是数据怎么打到服务器呢?又该如何触发?当然使用 CVM 也是可以的,但是似乎太笨拙,并且消耗量很大,需要自己搭好所有环境。所以,这里我们选用 Serverless 方式来部署。

核心逻辑

我们来看看整体业务的代码部分吧,毕竟这里是整个机器人的核心。我们来看代码(请求三次接口):

<"https://lab.isaaclin.cn/nCoV/api/area",
 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",
 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", //这里的地址填写为企业微信的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 "运行成功"; 
}
}
"htmlcode">
$ 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

运行结果如下:

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

这样,我们就完成了一个 nCoV 的在线触发函数机器人~是不是很简单呢?快来开始动手吧~

传送门:

GitHub: github.com/serverless

官网:serverless.com

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

兴国资源网 Design By www.nnzcdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
兴国资源网 Design By www.nnzcdc.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。