최현준

기상청 api 이용 날씨 정보 획득 기능 추가

Showing 1 changed file with 126 additions and 3 deletions
var express = require('express');
const request = require('request');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = 'm79m/yBsTVzZeC9pYqSLzEL00Hp0HqsVE/0ZsZxW2HNAwdnb9TwDgBcOyCn3/aJASXsE8ekpyGg4Gts/4r8LO72OTZec9Np5Mh9g1vrgyDj5theWv4g2miE5F1Cqax4X3waj1aIzDGHcUZdHD6fQvAdB04t89/1O/w1cDnyilFU='
const TOKEN = '9odQvyPPtDEzqHdfdk8Q8ua6dgrffTQuJnpkYuM6Q0VTREhmU+NJ0BAVCIfo2gEDtD37TYC0TZ7MrijHZeA592eUwzb+quftcHXBa0hye5iZHK0G4HiZCnB9C2bLgQvgQVJ47DwGeBQlwaNVJvze1AdB04t89/1O/w1cDnyilFU='
const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
const PAPAGO_D_URL = 'https://openapi.naver.com/v1/papago/detectLangs'
const PAPAGO_ID = 'tA41WEd_nbzy0nZcUQks'
const PAPAGO_SECRET = '6DHwov9gg2'
const WEATHER_TARGET_URL = 'http://apis.data.go.kr/1360000/VilageFcstInfoService/getVilageFcst';
const weather_key = 'CuIdcpULvcissE3Jk7AyAXok4bR4j6xyNkhfmWxOI9%2BxCs%2FkI%2F4v4j4rz3DDsQZVeuJTrWQhzQ3CV4JkeykMSw%3D%3D';
const bodyParser = require('body-parser');
var app = express();
var async = require('async');
const { resolve } = require('path');
var Language_Detect_Array = new Array();
var User_Info_Array = new Array();
var Weahter_Info_Array = new Array();
Weahter_Info_Array[0] =
{
"Location" : '수원',
"nx" : 61,
"ny" : 120
}
Weahter_Info_Array[1] =
{
"Location" : '서울',
"nx" : 60,
"ny" : 127
}
Language_Detect_Array[0] =
{
"Message" : '영어',
......@@ -46,8 +63,8 @@ app.post('/hook', function (req, res) {
var message = eventObj.message.text;
var replyToken = eventObj.replyToken;
var userid = eventObj.source.userId;
AsyncFunction(message, replyToken, userid);
if(message == '날씨') weathercheck(replyToken); else AsyncFunction(message, replyToken, userid);
res.sendStatus(200);
});
......@@ -63,6 +80,15 @@ async function AsyncFunction(message, replyToken, userid)
console.log("log-Message: ", Messaging);
}
async function weathercheck(replyToken)
{
var timevalue = make_time()
var url = make_url(timevalue)
var persentofweawther = await weather_get(url);
var Messaging = await messaging(persentofweawther, replyToken, 1);
console.log("log-Message: ", Messaging);
}
function detector (message)
//언어 감지
{
......@@ -205,6 +231,103 @@ function Detect_Papago_Language_Change (TransToKoreanMessage, User_Location)
}
}
function make_time()
{
var today = new Date();
var value = new Object();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
var hours = today.getHours();
var minutes = today.getMinutes();
for (var i = 1; i<8; i++)
{
var temp = i+1;
if(3*i-1 <= hours && 3*temp-1 >= hours)
{
if(3*i-1<10)
value.hours = '0'+3*i-1;
else
value.hours = 3*i-1;
value.hours = 3*i-1+'00';
break;
}
}
if(minutes < 30){
// 30분보다 작으면 한시간 전 값
hours = hours - 1;
if(hours < 0){
// 자정 이전은 전날로 계산
today.setDate(today.getDate() - 1);
dd = today.getDate();
mm = today.getMonth()+1;
yyyy = today.getFullYear();
hours = 23;
}
}
if(hours<10) {
hours='0'+hours
}
if(mm<10) {
mm='0'+mm
}
if(dd<10) {
dd='0'+dd
}
today = yyyy+""+mm+""+dd;
value.today = today;
return value;
}
function make_url(timevalue)
{
var serviceKey = weather_key;
var pageNo = '1';
var numOfRows = '5';
var dataType = 'JSON';
var base_date = timevalue.today;
var base_time = timevalue.hours;
var nx = '61';
var ny = '120';
var url = `${WEATHER_TARGET_URL}`;
url += '?serviceKey='+serviceKey;
url += '&pageNo='+pageNo;
url += '&numOfRows='+numOfRows;
url += '&dataType='+dataType;
url += '&base_date='+base_date;
url += '&base_time='+base_time;
url += '&nx='+nx;
url += '&ny='+ny;
return url;
}
function weather_get (url)
{
return new Promise(function(resolve, reject)
{
request.get(
{
url: url,
},(error, response, body) =>
{
if(!error && response.statusCode == 200)
{
var weatherObj = JSON.parse(response.body);
resolve('4시간 안에 비가 올 확률은'+weatherObj.response.body.items.item[0].fcstValue+'%입니다.');
}
else{
console.log('Error', error);
}
}
)
});
}
app.listen(3000, function () {
console.log('Linebot listening on port 3000!');
});
\ No newline at end of file
......