最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于 Serverless +企業(yè)微信打造 nCoV 疫情監(jiān)控小助手

 更新時(shí)間:2020年02月10日 13:49:24   作者:Serverless  
這篇文章主要介紹了基于 Serverless +企業(yè)微信打造 nCoV 疫情監(jiān)控小助手,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

最近的一些疫情信息很讓人揪心,為了方便大家掌握疫情信息,在空閑之余做了一個關(guān)于 nCoV 的疫情監(jiān)控小助手。主要的功能是通過企業(yè)微信的 WebHook 來推送疫情信息。這里將使用 Serverless 的整體代碼思路和架構(gòu)方式分享給大家。本文作者:tabor

實(shí)現(xiàn)效果

我們想要實(shí)現(xiàn)的大致的效果是這樣的:

首先,我們需要解決的是數(shù)據(jù)來源問題,這里我們可以使用 python 爬蟲來做這件事情,但是由于個人比較懶所以直接用的 2019-nCoV-Crawler  ,這個項(xiàng)目已經(jīng)集成了現(xiàn)有的 API,所以我們直接調(diào)用即可。當(dāng)然有能力的同學(xué)也可以自己部署 Python,我這邊是自己部署的,但是這不是本次的重點(diǎn),就不在贅述。

現(xiàn)在,我們有了數(shù)據(jù),但是數(shù)據(jù)怎么打到服務(wù)器呢?又該如何觸發(fā)?當(dāng)然使用 CVM 也是可以的,但是似乎太笨拙,并且消耗量很大,需要自己搭好所有環(huán)境。所以,這里我們選用 Serverless 方式來部署。

核心邏輯

我們來看看整體業(yè)務(wù)的代碼部分吧,畢竟這里是整個機(jī)器人的核心。我們來看代碼(請求三次接口):

<?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 {
//疫情監(jiān)控告警機(jī)器人
$sc = $sc=" **2019-nCoV 疫情信息同步:** \n
> 全國疫情: 
> 確診人數(shù)<font color=\"info\">".$echo_responseall['results'][0]['confirmedCount']."</font>,疑似感染人數(shù)<font color=\"info\">".$echo_responseall['results'][0]['suspectedCount']."</font>,治愈人數(shù)<font color=\"info\">".$echo_responseall['results'][0]['curedCount']."</font>,死亡人數(shù)<font color=\"info\">".$echo_responseall['results'][0]['deadCount']."</font>\n
> 廣東省: 
> 確診人數(shù)<font color=\"info\">".$echo_responsesz['results'][0]['confirmedCount']."</font>,疑似感染人數(shù)<font color=\"info\">".$echo_responsesz['results'][0]['suspectedCount']."</font>,治愈人數(shù)<font color=\"info\">".$echo_responsesz['results'][0]['curedCount']."</font>,死亡人數(shù)<font color=\"info\">".$echo_responsesz['results'][0]['deadCount']."</font>\n
> 湖北省: 
> 確診人數(shù)<font color=\"info\">".$echo_responsehb['results'][0]['confirmedCount']."</font>,疑似感染人數(shù)<font color=\"info\">".$echo_responsehb['results'][0]['suspectedCount']."</font>,治愈人數(shù)<font color=\"info\">".$echo_responsehb['results'][0]['curedCount']."</font>,死亡人數(shù)<font color=\"info\">".$echo_responsehb['results'][0]['deadCount']."</font>\n
> 深圳市: 
> 確診人數(shù)<font color=\"info\">".$echo_responseszqz."</font>,疑似感染人數(shù)<font color=\"info\">".$echo_responseszys."</font>,治愈人數(shù)<font color=\"info\">".$echo_responseszzy."</font>,死亡人數(shù)<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
>[更多數(shù)據(jù)請查看](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=", //這里的地址填寫為企業(yè)微信的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 "運(yùn)行成功"; 
}
}
?>

是不是很簡單呢?請求數(shù)據(jù),發(fā)送數(shù)據(jù)。

那么我們接下了重點(diǎn)看下如何將我們的業(yè)務(wù)代碼上傳到云端呢?
這里的云端我用的是騰訊云 Serverless 服務(wù) SCF云函數(shù) 。整個部署,使用過程都是免費(fèi)的,對于開發(fā)者來講小項(xiàng)目使用的話免費(fèi)額度是完全夠用的。無需擔(dān)心額外付費(fèi)。

Serverless 部署,選用的是比較流行的 Serverless Framework,使用和部署也是完全免費(fèi)的,那么下面我就來介紹下具體的部署過程吧。

安裝 Serverless 框架

首先,第一步,我們來安裝一個 Serverless Framework 的開發(fā)框架:

$ npm install -g serverless

然后,我們創(chuàng)建一個函數(shù)目錄:

$ mkdir nCov-function
$ cd nCov-function

相關(guān)函數(shù)目錄的內(nèi)容如下:

|- code
 |- index.php // 這里就是上面的業(yè)務(wù)代碼存放位置
|- serverless.yml //serverless 配置文件

配置 Yml 文件

接下來,是我們的重頭戲,配置函數(shù) yml 文件:

# serverless.yml
myFunction:
 component: "@serverless/tencent-scf" //引用tencent-scf component
 inputs:
 name: nCoVFunction //函數(shù)名稱
 enableRoleAuth: true
 codeUri: ./code //代碼本地存放位置
 handler: index.main_handler
 runtime: Php5
 region: ap-shanghai //函數(shù)運(yùn)行地域
 description: My Serverless nCoV Function.
 memorySize: 128 //運(yùn)行內(nèi)存
 timeout: 20 //超時(shí)時(shí)間
 exclude:
 - .gitignore
 - .git/**
 - node_modules/**
 - .serverless
 - .env
 include:
 - ./nCoVFunction.zip
 environment:
 variables:
 TEST: vale
 vpcConfig:
 subnetId: ''
 vpcId: ''
 events:
 - timer: // 定時(shí)觸發(fā)器
  name: timer
  parameters:
  cronExpression: '0 0 10,21 * * * *' //明天早上10點(diǎn),晚上21點(diǎn)
  enable: true

萬事具備,我們就可以直接部署 SLS 了。

部署到云端

通過 sls 命令(serverless 的縮寫)進(jìn)行部署,并可以添加 –debug 參數(shù)查看部署過程中的信息:

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

運(yùn)行結(jié)果如下:

這樣,我們就完成了一個 nCoV 的在線觸發(fā)函數(shù)機(jī)器人~是不是很簡單呢?快來開始動手吧~

傳送門:

GitHub: github.com/serverless

官網(wǎng):serverless.com

好了,就給大家介紹到這來,希望大家喜歡!

相關(guān)文章

  • git生成SSH key實(shí)現(xiàn)過程

    git生成SSH key實(shí)現(xiàn)過程

    該文章主要講述了在Windows和Linux環(huán)境下生成和管理SSH密鑰的步驟,包括檢查現(xiàn)有SSH密鑰、生成新的SSH密鑰、保存和保護(hù)密鑰等步驟,并提供了一些相關(guān)命令,適合初學(xué)者參考學(xué)習(xí)
    2026-04-04
  • 單點(diǎn)登錄之cas集成sonar的配置方法

    單點(diǎn)登錄之cas集成sonar的配置方法

    這篇文章主要介紹了單點(diǎn)登錄之cas集成sonar的相關(guān)知識,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • IntelliJ IDEA 性能優(yōu)化的教程詳解

    IntelliJ IDEA 性能優(yōu)化的教程詳解

    這篇文章主要介紹了IntelliJ IDEA 性能優(yōu)化的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • git分支(branch)操作相關(guān)命令及分支命令的使用

    git分支(branch)操作相關(guān)命令及分支命令的使用

    這篇文章主要介紹了git分支(branch)操作相關(guān)命令及分支命令的使用的相關(guān)資料,需要的朋友可以參考下
    2017-10-10
  • Visual?Studio自定義項(xiàng)目模版

    Visual?Studio自定義項(xiàng)目模版

    這篇文章介紹了Visual?Studio自定義項(xiàng)目模版的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 有效的提高編程技能的12個方法

    有效的提高編程技能的12個方法

    以下是提高編程技能的12個方法,主要是不停學(xué)習(xí),努力成全才,跟聰明人或有想法的一起
    2012-05-05
  • cypress測試工具特點(diǎn)及使用介紹

    cypress測試工具特點(diǎn)及使用介紹

    這篇文章主要為大家介紹了cypress測試工具以及為什么要使用cypress的原因,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • git如何拉取項(xiàng)目分支代碼

    git如何拉取項(xiàng)目分支代碼

    本文詳細(xì)介紹了如何使用Git拉取項(xiàng)目分支代碼,并提供了解決常見問題(如fatal: unable to update url base from redirection)的步驟,問題解決的關(guān)鍵在于確保你有項(xiàng)目的訪問權(quán)限
    2026-01-01
  • 如何創(chuàng)建VS Code 擴(kuò)展插件

    如何創(chuàng)建VS Code 擴(kuò)展插件

    VS Code提供了強(qiáng)大的擴(kuò)展功能,本文主要介紹了如何創(chuàng)建VS Code 擴(kuò)展插件,主要包括插件的創(chuàng)建、開發(fā)和發(fā)布過程,具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-01-01
  • Git報(bào)錯:fatal:refusing?to?merge?unrelated?histories問題解決過程

    Git報(bào)錯:fatal:refusing?to?merge?unrelated?histories問題解決過程

    本文作者分享了因誤刪.git文件夾而引發(fā)的Git報(bào)錯解決過程,作者嘗試重新初始化倉庫并拉取代碼,但遭到了Git的拒絕,作者認(rèn)為正確的做法是重新克隆代碼庫,以保留完整的提交歷史記錄,本文提醒我們要重視Git的歷史記錄機(jī)制及其重要性
    2026-04-04

最新評論

绿春县| 霍城县| 宁海县| 渝中区| 北票市| 安龙县| 达州市| 华容县| 英德市| 松江区| 加查县| 开江县| 穆棱市| 双柏县| 静安区| 万源市| 偏关县| 奉节县| 文安县| 田林县| 乌恰县| 灵川县| 宁阳县| 蚌埠市| 乌苏市| 南皮县| 绿春县| 繁峙县| 江源县| 驻马店市| 凉山| 太康县| 东莞市| 丹巴县| 永善县| 鄂温| 栾川县| 崇明县| 临海市| 沂源县| 石首市|