최현준

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

Showing 1 changed file with 126 additions and 3 deletions
1 var express = require('express'); 1 var express = require('express');
2 const request = require('request'); 2 const request = require('request');
3 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 3 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
4 -const TOKEN = 'm79m/yBsTVzZeC9pYqSLzEL00Hp0HqsVE/0ZsZxW2HNAwdnb9TwDgBcOyCn3/aJASXsE8ekpyGg4Gts/4r8LO72OTZec9Np5Mh9g1vrgyDj5theWv4g2miE5F1Cqax4X3waj1aIzDGHcUZdHD6fQvAdB04t89/1O/w1cDnyilFU=' 4 +const TOKEN = '9odQvyPPtDEzqHdfdk8Q8ua6dgrffTQuJnpkYuM6Q0VTREhmU+NJ0BAVCIfo2gEDtD37TYC0TZ7MrijHZeA592eUwzb+quftcHXBa0hye5iZHK0G4HiZCnB9C2bLgQvgQVJ47DwGeBQlwaNVJvze1AdB04t89/1O/w1cDnyilFU='
5 const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' 5 const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
6 const PAPAGO_D_URL = 'https://openapi.naver.com/v1/papago/detectLangs' 6 const PAPAGO_D_URL = 'https://openapi.naver.com/v1/papago/detectLangs'
7 const PAPAGO_ID = 'tA41WEd_nbzy0nZcUQks' 7 const PAPAGO_ID = 'tA41WEd_nbzy0nZcUQks'
8 const PAPAGO_SECRET = '6DHwov9gg2' 8 const PAPAGO_SECRET = '6DHwov9gg2'
9 +const WEATHER_TARGET_URL = 'http://apis.data.go.kr/1360000/VilageFcstInfoService/getVilageFcst';
10 +const weather_key = 'CuIdcpULvcissE3Jk7AyAXok4bR4j6xyNkhfmWxOI9%2BxCs%2FkI%2F4v4j4rz3DDsQZVeuJTrWQhzQ3CV4JkeykMSw%3D%3D';
9 const bodyParser = require('body-parser'); 11 const bodyParser = require('body-parser');
10 var app = express(); 12 var app = express();
11 var async = require('async'); 13 var async = require('async');
12 const { resolve } = require('path'); 14 const { resolve } = require('path');
13 var Language_Detect_Array = new Array(); 15 var Language_Detect_Array = new Array();
14 var User_Info_Array = new Array(); 16 var User_Info_Array = new Array();
17 +var Weahter_Info_Array = new Array();
18 +
19 +Weahter_Info_Array[0] =
20 +{
21 + "Location" : '수원',
22 + "nx" : 61,
23 + "ny" : 120
24 +}
25 +Weahter_Info_Array[1] =
26 +{
27 + "Location" : '서울',
28 + "nx" : 60,
29 + "ny" : 127
30 +}
31 +
15 Language_Detect_Array[0] = 32 Language_Detect_Array[0] =
16 { 33 {
17 "Message" : '영어', 34 "Message" : '영어',
...@@ -46,8 +63,8 @@ app.post('/hook', function (req, res) { ...@@ -46,8 +63,8 @@ app.post('/hook', function (req, res) {
46 var message = eventObj.message.text; 63 var message = eventObj.message.text;
47 var replyToken = eventObj.replyToken; 64 var replyToken = eventObj.replyToken;
48 var userid = eventObj.source.userId; 65 var userid = eventObj.source.userId;
49 - 66 +
50 - AsyncFunction(message, replyToken, userid); 67 + if(message == '날씨') weathercheck(replyToken); else AsyncFunction(message, replyToken, userid);
51 68
52 res.sendStatus(200); 69 res.sendStatus(200);
53 }); 70 });
...@@ -63,6 +80,15 @@ async function AsyncFunction(message, replyToken, userid) ...@@ -63,6 +80,15 @@ async function AsyncFunction(message, replyToken, userid)
63 console.log("log-Message: ", Messaging); 80 console.log("log-Message: ", Messaging);
64 } 81 }
65 82
83 +async function weathercheck(replyToken)
84 +{
85 + var timevalue = make_time()
86 + var url = make_url(timevalue)
87 + var persentofweawther = await weather_get(url);
88 + var Messaging = await messaging(persentofweawther, replyToken, 1);
89 + console.log("log-Message: ", Messaging);
90 +}
91 +
66 function detector (message) 92 function detector (message)
67 //언어 감지 93 //언어 감지
68 { 94 {
...@@ -205,6 +231,103 @@ function Detect_Papago_Language_Change (TransToKoreanMessage, User_Location) ...@@ -205,6 +231,103 @@ function Detect_Papago_Language_Change (TransToKoreanMessage, User_Location)
205 } 231 }
206 } 232 }
207 233
234 +function make_time()
235 +{
236 + var today = new Date();
237 + var value = new Object();
238 + var dd = today.getDate();
239 + var mm = today.getMonth()+1;
240 + var yyyy = today.getFullYear();
241 + var hours = today.getHours();
242 + var minutes = today.getMinutes();
243 +
244 + for (var i = 1; i<8; i++)
245 + {
246 + var temp = i+1;
247 + if(3*i-1 <= hours && 3*temp-1 >= hours)
248 + {
249 + if(3*i-1<10)
250 + value.hours = '0'+3*i-1;
251 + else
252 + value.hours = 3*i-1;
253 + value.hours = 3*i-1+'00';
254 + break;
255 + }
256 + }
257 +
258 + if(minutes < 30){
259 + // 30분보다 작으면 한시간 전 값
260 + hours = hours - 1;
261 + if(hours < 0){
262 + // 자정 이전은 전날로 계산
263 + today.setDate(today.getDate() - 1);
264 + dd = today.getDate();
265 + mm = today.getMonth()+1;
266 + yyyy = today.getFullYear();
267 + hours = 23;
268 + }
269 + }
270 + if(hours<10) {
271 + hours='0'+hours
272 + }
273 + if(mm<10) {
274 + mm='0'+mm
275 + }
276 + if(dd<10) {
277 + dd='0'+dd
278 + }
279 + today = yyyy+""+mm+""+dd;
280 +
281 + value.today = today;
282 + return value;
283 +}
284 +
285 +function make_url(timevalue)
286 +{
287 + var serviceKey = weather_key;
288 + var pageNo = '1';
289 + var numOfRows = '5';
290 + var dataType = 'JSON';
291 + var base_date = timevalue.today;
292 + var base_time = timevalue.hours;
293 + var nx = '61';
294 + var ny = '120';
295 +
296 + var url = `${WEATHER_TARGET_URL}`;
297 + url += '?serviceKey='+serviceKey;
298 + url += '&pageNo='+pageNo;
299 + url += '&numOfRows='+numOfRows;
300 + url += '&dataType='+dataType;
301 + url += '&base_date='+base_date;
302 + url += '&base_time='+base_time;
303 + url += '&nx='+nx;
304 + url += '&ny='+ny;
305 + return url;
306 +}
307 +
308 +
309 +function weather_get (url)
310 +{
311 + return new Promise(function(resolve, reject)
312 + {
313 + request.get(
314 + {
315 + url: url,
316 + },(error, response, body) =>
317 + {
318 + if(!error && response.statusCode == 200)
319 + {
320 + var weatherObj = JSON.parse(response.body);
321 + resolve('4시간 안에 비가 올 확률은'+weatherObj.response.body.items.item[0].fcstValue+'%입니다.');
322 + }
323 + else{
324 + console.log('Error', error);
325 + }
326 + }
327 + )
328 + });
329 +}
330 +
208 app.listen(3000, function () { 331 app.listen(3000, function () {
209 console.log('Linebot listening on port 3000!'); 332 console.log('Linebot listening on port 3000!');
210 }); 333 });
...\ No newline at end of file ...\ No newline at end of file
......