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

Telegram BOT FUNCTION 36HR氣象預報

2016-10-21-07-57-46
//天氣狀況
if(RegExp('天氣$',"i").test(message.text) == true || RegExp('^/weather',"i").test(message.text) == true){
	console.log('偵測到關鍵字:天氣狀況');
	weather_str = message.text;
	weather_str = weather_str.replace(/ /g,'');
	weather_str = weather_str.replace(/ |天氣|狀況|的|怎樣/g,'');
	weather_str = weather_str.replace(/台/g,'臺');
	if(weather_str == '' || RegExp('臺北市|新北市|桃園市|臺中市|臺南市|高雄市|基隆市|新竹縣|新竹市|苗栗縣|彰化縣|南投縣|雲林縣|嘉義縣|嘉義市|屏東縣|宜蘭縣|花蓮縣|臺東縣|澎湖縣|金門縣|連江縣',"i").test(weather_str) == false){
		api.sendMessage({
			chat_id: message.from.id,
			text: '請選擇你所在城市...',
			reply_markup: JSON.stringify({
			ReplyKeyboardHide: true,
			resize_keyboard: true,
			inline_keyboard: [
				[{text: '臺北市',callback_data: 'weather_0'},{text: '新北市',callback_data: 'weather_1'},{text: '桃園市',callback_data: 'weather_2'},{text: '臺中市',callback_data: 'weather_3'}],
				[{text: '臺南市',callback_data: 'weather_4'},{text: '高雄市',callback_data: 'weather_5'},{text: '基隆市',callback_data: 'weather_6'},{text: '新竹縣',callback_data: 'weather_7'}],
				[{text: '新竹市',callback_data: 'weather_8'},{text: '苗栗縣',callback_data: 'weather_9'},{text: '彰化縣',callback_data: 'weather_10'},{text: '南投縣',callback_data: 'weather_11'}],
				[{text: '雲林縣',callback_data: 'weather_12'},{text: '嘉義縣',callback_data: 'weather_13'},{text: '嘉義市',callback_data: 'weather_14'},{text: '屏東縣',callback_data: 'weather_15'}],
				[{text: '宜蘭縣',callback_data: 'weather_16'},{text: '花蓮縣',callback_data: 'weather_17'},{text: '臺東縣',callback_data: 'weather_18'},{text: '澎湖縣',callback_data: 'weather_19'}],
				[{text: '金門縣',callback_data: 'weather_20'},{text: '連江縣',callback_data: 'weather_21'}]
			]
		})
	});
 }else{
	country_ID = 0;
	func_wather(message.chat.id,weather_str,country_ID);
	}
 }
 //天氣狀況
api.on('inline.callback.query', function(message){
	var L_data = message.data;
	if (RegExp("weather_").test(message.data) == true){
		var weather_prfix = L_data.replace(/weather_/g,'');
		L_data = 'weather_query';
	}
	switch(L_data){
//天氣區
		case "weather_query":
		console.log('偵測到關鍵字:天氣預報');
		country_ID = weather_prfix;
		weather_str = weather_prfix;
		func_wather(message.message.chat.id,weather_str,country_ID);
			break;
	}
});
function func_wather(W_C_ID,MSG_TXT,W_ID){
	weather_str = MSG_TXT;
	request({
		url: 'http://opendata.cwb.gov.tw/govdownload?dataid=F-C0032-001&authorizationkey=rdec-key-123-45678-011121314',
		method: "GET"
	}, function(w_error, w_response, w_body) {
		parser.parseString(w_body,function (w_err, w_result) {
			for (var i=0;i0) {
				weather_i = W_ID;
			}
			if(RegExp(weather_str,'i').test(w_result.cwbopendata.dataset[0].location[i].locationName) == true ){
				weather_i = i;
			}
		}
		time_line_1 = dateFormat(w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[0].time[0].startTime[0], 'mm/dd HH:MM')+'~'+ dateFormat(w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[0].time[0].endTime[0], 'mm/dd HH:MM');
		time_line_2 = dateFormat(w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[0].time[1].startTime[0], 'mm/dd HH:MM')+'~'+ dateFormat(w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[0].time[1].endTime[0], 'mm/dd HH:MM');
		time_line_3 = dateFormat(w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[0].time[2].startTime[0], 'mm/dd HH:MM')+'~'+ dateFormat(w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[0].time[2].endTime[0], 'mm/dd HH:MM');
		weather_pre_str = w_result.cwbopendata.dataset[0].location[weather_i].locationName +' 天氣預報\n\n'+
		time_line_1 +'\n'+
		' '+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[0].time[0].parameter[0].parameterName +' 氣溫:'+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[2].time[0].parameter[0].parameterName +'~'+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[1].time[0].parameter[0].parameterName +'度\n'+
		' '+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[3].time[0].parameter[0].parameterName +' 降雨機率 '+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[4].time[0].parameter[0].parameterName + '(%)\n\n'+
		time_line_2 +'\n'+
		' '+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[0].time[1].parameter[0].parameterName +' 氣溫:'+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[2].time[1].parameter[0].parameterName +'~'+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[1].time[1].parameter[0].parameterName +'度\n'+
		' '+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[3].time[0].parameter[0].parameterName +' 降雨機率 '+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[4].time[1].parameter[0].parameterName + '(%)\n\n'+
		time_line_3 +'\n'+
		' '+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[0].time[2].parameter[0].parameterName +' 氣溫:'+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[2].time[2].parameter[0].parameterName +'~'+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[1].time[2].parameter[0].parameterName +'度\n'+
		' '+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[3].time[0].parameter[0].parameterName +' 降雨機率 '+ w_result.cwbopendata.dataset[0].location[weather_i].weatherElement[4].time[2].parameter[0].parameterName + '(%)\n\n'+
		'更新時間:'+ dateFormat(w_result.cwbopendata.dataset[0].datasetInfo[0].issueTime[0], 'yyyy/mm/dd HH:MM') ;
		api.sendMessage({chat_id: W_C_ID, text: weather_pre_str});
	});
	});
}
//天氣狀況