Ubuntu

0613 우산여부확인

...@@ -2,11 +2,17 @@ var express = require('express');//express 모듈을 사용 ...@@ -2,11 +2,17 @@ var express = require('express');//express 모듈을 사용
2 var qs = require('querystring') 2 var qs = require('querystring')
3 const request = require('request');//request 모듈을 사용 3 const request = require('request');//request 모듈을 사용
4 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'//라인 api 4 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'//라인 api
5 +
6 +var NOW = new Date()
7 +var YEAR = NOW.getFullYear()
8 +var MONTH= NOW.getMonth()+1
9 +var DATE= NOW.getDate();
10 +var NOWDATE= YEAR.toString() + MONTH.toString() + DATE.toString()
11 +const WEATHER_URL=`http://apis.data.go.kr/1360000/VilageFcstInfoService/getVilageFcst?serviceKey=StHx15%2FPZFKIJvR5AtCu8uyowAtnEYHpAXR%2B5XYYN6OWDt%2BCz15z%2Fxn%2FDiV%2FLN9%2BY5YSPZUPUnCq982CiBupmw%3D%3D&numOfRows=10&pageNo=1&base_date=${NOWDATE}&base_time=0230&nx=55&ny=127&dataType=JSON`
12 +
13 +
5 const TOKEN = 'RbPAXRYWub0evg2yyi7oiHytZsZsE0JtbZgRYZNZDU1vjpJkOwGqwh+aKTYKVhVHh6LZOUVZLl84NQQlNWNbXR9hUhPEiLEK0cie4O3OlKUuEe/3wAsjPu7HbRi1zn9BsR3Qr4pcqmiIKP8HRUKvEwdB04t89/1O/w1cDnyilFU=' 14 const TOKEN = 'RbPAXRYWub0evg2yyi7oiHytZsZsE0JtbZgRYZNZDU1vjpJkOwGqwh+aKTYKVhVHh6LZOUVZLl84NQQlNWNbXR9hUhPEiLEK0cie4O3OlKUuEe/3wAsjPu7HbRi1zn9BsR3Qr4pcqmiIKP8HRUKvEwdB04t89/1O/w1cDnyilFU='
6 -const MAP_URL = 'https://naveropenapi.apigw.ntruss.com/map-static/v2/raster?w=300&h=300&center=127.1054221,37.3591614&level=16&format=jpg' 15 +
7 -const GEO_URL = 'https://naveropenapi.apigw.ntruss.com/map-geocode/v2/geocode'
8 -const MAP_ID = '8p40s890zx'
9 -const MAP_SECRET = 'twTXrNPSuUYNU4MG7bV9KjXzpTADVDQAlMFK7kAZ'
10 const fs = require('fs'); 16 const fs = require('fs');
11 const path = require('path'); 17 const path = require('path');
12 const HTTPS = require('https'); 18 const HTTPS = require('https');
...@@ -14,7 +20,6 @@ const domain = "www.osschat.tk" ...@@ -14,7 +20,6 @@ const domain = "www.osschat.tk"
14 const sslport = 23023; 20 const sslport = 23023;
15 const bodyParser = require('body-parser'); 21 const bodyParser = require('body-parser');
16 var app = express(); 22 var app = express();
17 -var target = 'en'
18 var premessage='premessage' 23 var premessage='premessage'
19 var start ='start' 24 var start ='start'
20 25
...@@ -22,6 +27,8 @@ var start ='start' ...@@ -22,6 +27,8 @@ var start ='start'
22 app.use(bodyParser.json()); 27 app.use(bodyParser.json());
23 app.use(express.static('statics')); 28 app.use(express.static('statics'));
24 29
30 +
31 +
25 app.post('/hook', function (req, res) { 32 app.post('/hook', function (req, res) {
26 33
27 var eventObj = req.body.events[0]; 34 var eventObj = req.body.events[0];
...@@ -34,8 +41,7 @@ app.post('/hook', function (req, res) { ...@@ -34,8 +41,7 @@ app.post('/hook', function (req, res) {
34 console.log('[request source] ', eventObj.source); 41 console.log('[request source] ', eventObj.source);
35 console.log('[request message]', eventObj.message); 42 console.log('[request message]', eventObj.message);
36 43
37 - trans(eventObj.replyToken, eventObj.message.text); 44 + rain(eventObj.replyToken, eventObj.message.text);
38 - geo(eventObj.replyToken, eventObj.message.text);
39 45
40 46
41 res.sendStatus(200); 47 res.sendStatus(200);
...@@ -43,32 +49,47 @@ app.post('/hook', function (req, res) { ...@@ -43,32 +49,47 @@ app.post('/hook', function (req, res) {
43 49
44 50
45 51
46 -function geo (replyToekn, message){ 52 +function rain (replyToken, message){
47 - 53 + if(message != '오늘 옷')
48 - if(premessage != '길찾기')
49 { 54 {
50 return 55 return
51 } 56 }
52 - start=qs.escape(message)
53 -
54 - const GEO_URL = `https://naveropenapi.apigw.ntruss.com/map-geocode/v2/geocode?query=${start}`
55 -
56 request.get( 57 request.get(
57 { 58 {
58 - url: GEO_URL, 59 + url: WEATHER_URL,
60 + json:true
61 + },(error, response, body) => {
62 + if(!error && response.statusCode == 200) {
63 + console.log(body.message);
64 + if(body.POP>=50){
65 + var rainMessage = "꼭! 우산을 챙기세요";
66 + }else{
67 + var rainMessage= "우산이 굳이 필요하지는 않습니다."
68 + }
69 + request.post(
70 + {
71 + url: TARGET_URL,
59 headers: { 72 headers: {
60 - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 73 + 'Authorization': `Bearer ${TOKEN}`
61 - 'X-NCP-APIGW-API-KEY-ID': `${MAP_ID}`, 74 + },
62 - 'X-NCP-APIGW-API-KEY': `${MAP_SECRET}` 75 + json: {
76 + "replyToken":replyToken,
77 + "messages":[
78 + {
79 + "type":"text",
80 + "text":rainMessage
63 } 81 }
64 - },function(error, response, body) { 82 + ]
83 + }
84 + },(error, response, body) => {
65 console.log(body) 85 console.log(body)
66 }); 86 });
87 + }
88 + });
67 } 89 }
68 90
69 91
70 - 92 +/*
71 -
72 93
73 function trans(replyToken, message) { 94 function trans(replyToken, message) {
74 95
...@@ -109,6 +130,7 @@ function trans(replyToken, message) { ...@@ -109,6 +130,7 @@ function trans(replyToken, message) {
109 }); 130 });
110 131
111 } 132 }
133 +*/
112 134
113 try { 135 try {
114 const option = { 136 const option = {
......