…生活與工作…個人的生活雜記….

Telegram bot FUNCTION 地震

2016-10-07-15-42-54

程式碼:

//FUNCTION 地震
//觸發參數 E_TYPE crontab "A" 手動查詢 "M1"(地震) "M2" (最近一次地震)
//觸發參數 E_id 對話ID
function func_earthquerk(E_status,E_id,E_type){
earthquake_str = '';
earthquake_str_path = '';
earthquake_datastmp = '';
if (E_status == '1') {
//大地震
E_url = 'http://opendata.cwb.gov.tw/govdownload?dataid=E-A0015-001R&authorizationkey=rdec-key-123-45678-011121314';
//E_url = 'https://www.est.idv.tw/1.xml';
}else{
//小地震
E_url = 'http://opendata.cwb.gov.tw/govdownload?dataid=E-A0016-001R&authorizationkey=rdec-key-123-45678-011121314';
//E_url = 'https://www.est.idv.tw/2.xml';
}
request({
url:E_url,
method: "GET"
}, function(e_error, e_response, e_body) {
if(e_error == null){
parser.parseString(e_body,function (e_err, e_result) {
//時間Timestamp
earthquake_timestmp = parseInt(new Date(dateFormat(e_result.cwbopendata.sent,'isoUtcDateTime')).getTime().toString().slice(0,10));
if (E_status == '1'){
earthquake_timestmp_1 = earthquake_timestmp;
}
now_time = parseInt(new Date().getTime().toString().slice(0,10)); //UTC+8
//now_time = earthquake_timestmp;.

switch(E_type){
case 'A':
now_time = now_time-1800;
pre_earthquake_str = '';
break;

case 'M1':
earthquake_timestmp = earthquake_timestmp+2500;
pre_earthquake_str = '地震了!!!塊陶啊!!\n';
if (now_time >= earthquake_timestmp) {
pre_earthquake_str = '目前無地震報告,請5分鐘後再試一次';
}
api.sendMessage({chat_id: E_id, text: pre_earthquake_str});
earthquake_flag = E_type;
earthquake_flag_1 = E_type;
break;

case 'M2':
earthquake_timestmp = now_time;
pre_earthquake_str = '';
earthquake_flag = E_type;
earthquake_flag_1 = E_type;
break;

default:
earthquake_timestmp = now_time;
pre_earthquake_str = '';
break;
};

//console.log('type='+E_type);
//console.log('now_time = '+ now_time);
//console.log('earthquake_timestmp= '+ earthquake_timestmp);
//console.log('TimeDEF= '+ parseInt(now_time - earthquake_timestmp));
//console.log ('earthquake_flag_BEF= '+ earthquake_flag);

if(now_time - earthquake_timestmp < = 0){
earthquake_str ='地震報告: '+ dateFormat(e_result.cwbopendata.sent,'mm/dd HH:MM') +'發佈\n'+
' '+ e_result.cwbopendata.dataset[0].earthquake[0].reportContent +'\n'+
' 編號:'+ e_result.cwbopendata.dataset[0].earthquake[0].earthquakeNo +'\n'+
' 時間:'+ dateFormat(e_result.cwbopendata.dataset[0].earthquake[0].earthquakeInfo[0].originTime,'mm/dd HH:MM:ss') +'\n'+
' 震央:'+ e_result.cwbopendata.dataset[0].earthquake[0].earthquakeInfo[0].epicenter[0].location + '\n'+
' (E '+e_result.cwbopendata.dataset[0].earthquake[0].earthquakeInfo[0].epicenter[0].epicenterLon[0]._ + ',N '+e_result.cwbopendata.dataset[0].earthquake[0].earthquakeInfo[0].epicenter[0].epicenterLat[0]._ +')\n'+
' 深度:'+ e_result.cwbopendata.dataset[0].earthquake[0].earthquakeInfo[0].depth[0]._ +'Km\n'+
e_result.cwbopendata.dataset[0].earthquake[0].web+'\n'+
'本報告係中央氣象局地震觀測網即時地震資料地震速報之結果。'+E_status+E_type;
earthquake_url = e_result.cwbopendata.dataset[0].earthquake[0].reportImageURI[0];
if (e_result.cwbopendata.identifier[0] != earthquake_flag && e_result.cwbopendata.identifier[0] != earthquake_flag_1) {
api.sendPhoto({chat_id: E_id, photo: earthquake_url});
api.sendMessage({chat_id: E_id, text: earthquake_str});
if (E_status == '1') {
earthquake_flag_1 = e_result.cwbopendata.identifier[0];
}else{
earthquake_flag = e_result.cwbopendata.identifier[0];
}
}

}
// console.log ('earthquake_flag_AFT= '+ earthquake_flag);

});
}else{
console.log(e_error);
}
});
}