최현영

whereur

1 var express = require('express'); 1 var express = require('express');
2 const request = require('request'); 2 const request = require('request');
3 const bodyParser = require('body-parser'); 3 const bodyParser = require('body-parser');
4 +var app = express();
4 var static = require('serve-static'); 5 var static = require('serve-static');
5 6
6 const fs = require('fs'); 7 const fs = require('fs');
...@@ -14,13 +15,12 @@ const TOKEN = 'sqNjA99TptppqvcoVFAnU7Kawsl+s7l+JgnZ7r2is97qrFQraIn0sLQ6mTsIPvXLG ...@@ -14,13 +15,12 @@ const TOKEN = 'sqNjA99TptppqvcoVFAnU7Kawsl+s7l+JgnZ7r2is97qrFQraIn0sLQ6mTsIPvXLG
14 15
15 const domain = "www.chyoss.tk" 16 const domain = "www.chyoss.tk"
16 const sslport = 23023; 17 const sslport = 23023;
17 -var app = express();
18 18
19 app.use(bodyParser.json()); 19 app.use(bodyParser.json());
20 -
21 -var isstart = true;
22 //전역변수 선언 20 //전역변수 선언
23 - 21 +var isstart;
22 +var isforecast = 0;
23 +var jsonForforecast;
24 // body-parser를 이용해 application/x-www-form-urlencoded 파싱 24 // body-parser를 이용해 application/x-www-form-urlencoded 파싱
25 app.use(bodyParser.urlencoded({ extended: false })) 25 app.use(bodyParser.urlencoded({ extended: false }))
26 26
...@@ -54,8 +54,53 @@ router.route('/process/mapcctv').get(function(req, res){ ...@@ -54,8 +54,53 @@ router.route('/process/mapcctv').get(function(req, res){
54 }); 54 });
55 55
56 56
57 +
58 +function jsonforecast(callback){
59 + var url2 = 'http://data.ex.co.kr/openapi/safeDriving/forecast';
60 + var queryParams2 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/
61 + queryParams2 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */
62 + var obj
63 + request(
64 + {
65 + url: url2 + queryParams2,
66 + method: 'GET'
67 + }, function (error, response, body) {
68 + console.log('Status', response.statusCode);
69 + console.log('Headers', JSON.stringify(response.headers));
70 + console.log('Reponse received', body);
71 + obj = JSON.parse(body);
72 + console.log(obj);
73 + callback(obj);
74 + });
75 +
76 +};
77 +
78 +
79 +
57 app.use('/', router); 80 app.use('/', router);
58 81
82 +
83 +function ishello(istext){
84 + if (istext == '종료'){
85 + return false;
86 + }
87 + if(istext == '안녕'){
88 + return true;
89 + }
90 +}
91 +
92 +function firstforecast(isforecast, eventObj){
93 + if(isforecast == '1'){
94 + return 1;
95 + }
96 + else if(isforecast == '2'){
97 + return 2;
98 + }
99 + else{
100 + helloworld(eventObj);
101 + return;
102 + }
103 +}
59 app.post('/hook', function (req, res) { 104 app.post('/hook', function (req, res) {
60 105
61 var eventObj = req.body.events[0]; 106 var eventObj = req.body.events[0];
...@@ -66,11 +111,37 @@ app.post('/hook', function (req, res) { ...@@ -66,11 +111,37 @@ app.post('/hook', function (req, res) {
66 console.log('[request]', req.body); 111 console.log('[request]', req.body);
67 console.log('[request source] ', source); 112 console.log('[request source] ', source);
68 console.log('[request message]', message); 113 console.log('[request message]', message);
69 - helloworld(isstart, eventObj); 114 +
115 + isstart = ishello(message.text);
116 + isforecast = firstforecast(message.text, eventObj);
117 +
118 + if(isstart == true){
119 + console.log('[request hello] ', message);
120 + //console.log(count);
121 + helloworld(eventObj );
122 + isstart = false;
123 + }
124 + if(isstart == false){
125 + console.log('사용자가 종료를 입력했습니다.');
126 + isforecast = null;
127 + }
128 +
129 + if(isforecast == 1){
130 + console.log('[request Order] ', message);
131 + findroad(eventObj);
132 + }
133 + else if (isforecast == 2){
134 + console.log('[request Order] ', message);
135 + viewhtml(eventObj);
136 + }
137 +
138 +
139 +
140 +
70 res.sendStatus(200); 141 res.sendStatus(200);
71 }); 142 });
72 143
73 -function helloworld(isstart, eventObj){ 144 +function helloworld(eventObj){
74 request.post( 145 request.post(
75 { 146 {
76 url: TARGET_URL, 147 url: TARGET_URL,
...@@ -90,7 +161,36 @@ function helloworld(isstart, eventObj){ ...@@ -90,7 +161,36 @@ function helloworld(isstart, eventObj){
90 }, 161 },
91 { 162 {
92 "type":"text", 163 "type":"text",
164 + "text":"현재 교통 예보가 궁금하시다면 1번을, 실시간 교통혼잡 상황을 보실려면 2번을 눌러주세요"
165 + }
166 + ]
167 + }
168 + },(error, response, body) => {
169 + console.log(body)
170 + });
171 +}
172 +
173 +function viewhtml(eventObj){
174 + request.post(
175 + {
176 + url: TARGET_URL,
177 + headers: {
178 + 'Authorization': `Bearer ${TOKEN}`
179 + },
180 + json: {
181 + "replyToken":eventObj.replyToken,
182 + "messages":[
183 + {
184 + "type":"text",
185 + "text":"실시간 교통상황 웹페이지로 안내합니다."
186 + },
187 + {
188 + "type":"text",
93 "text":"https://www.chyoss.tk:23023/public/maprealtime.html" 189 "text":"https://www.chyoss.tk:23023/public/maprealtime.html"
190 + },
191 + {
192 + "type":"text",
193 + "text":"종료를 입력하시면 종료됩니다."
94 } 194 }
95 ] 195 ]
96 } 196 }
...@@ -101,71 +201,47 @@ function helloworld(isstart, eventObj){ ...@@ -101,71 +201,47 @@ function helloworld(isstart, eventObj){
101 201
102 202
103 203
204 +function findroad(eventObj){
205 + jsonforecast(function(object){
206 + var obj = object;
207 + jsonForforecast = obj;
208 + console.log(jsonForforecast);
209 + request.post(
210 + {
211 + url: TARGET_URL,
212 + headers: {
213 + 'Authorization': `Bearer ${TOKEN}`
214 + },
215 + json: {
216 + "replyToken":eventObj.replyToken,
217 + "messages":[
218 + {
219 + "type":"text",
220 + "text":"교통예보를 시작합니다."
221 + },
222 + {
223 + "type":"text",
224 + "text":jsonForforecast.list[0].sdate+ ', ' + jsonForforecast.list[0].stime + '기준입니다.'
225 + },
226 + {
227 + "type":"text",
228 + "text": '현재 전국 교통량은 ' + jsonForforecast.list[0].cjunkook + '대 이며, 지방방향 교통량은 ' + jsonForforecast.list[0].cjibangDir + '대, 서울방향 교통량은 ' + jsonForforecast.list[0].cseoulDir + '대입니다.'
229 + },
230 + {
231 + "type":"text",
232 + "text":"버스기준으로 보시겠습니까 아니면 일반 승용차 기준으로 보시겠습니까? 버스기준으로 보실려면 1번을, 일반 승용차 기준으로 보실려면 2번을 입력해 주세요."
233 + }
234 + ]
235 + }
236 + },(error, response, body) => {
237 + console.log(body)
238 + });
239 + });
240 +
241 +}
242 +
104 243
105 -// function selectlan(language){
106 -// if( language == '일본어'){
107 -
108 -// return 'target=ja';
109 -// }
110 -// else if (language == '영어'){
111 -
112 -// return 'target=en';
113 -// }
114 -// else if (language == '프랑스어'){
115 -
116 -// return 'target=fr';
117 -// }
118 -// }
119 -
120 -// function is(lantext){
121 -// if (lantext == '일본어' || lantext == '영어' || lantext == '프랑스어'){
122 -// return true;
123 -// }else{
124 -// return false;
125 -// }
126 -// }
127 -
128 -
129 -
130 -// function trans(replyToken, selectinput ,message) {
131 -// //var select = selectlan(lan);
132 -// var selectin = selectinput
133 -// request.post(
134 -// {
135 -// url: PAPAGO_URL,
136 -// headers: {
137 -// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
138 -// 'X-Naver-Client-Id': `${PAPAGO_ID}`,
139 -// 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
140 -// },
141 -// body: selectin + message,
142 -// json:true
143 -// },(error, response, body) => {
144 -// if(!error && response.statusCode == 200) {
145 -// console.log(body.message);
146 -// var transMessage = body.message.result.translatedText;
147 -// request.post(
148 -// {
149 -// url: TARGET_URL,
150 -// headers: {
151 -// 'Authorization': `Bearer ${TOKEN}`
152 -// },
153 -// json: {
154 -// "replyToken":replyToken,
155 -// "messages":[
156 -// {
157 -// "type":"text",
158 -// "text":transMessage
159 -// }
160 -// ]
161 -// }
162 -// },(error, response, body) => {
163 -// console.log(body)
164 -// });
165 -// }
166 -// });
167 244
168 -// }
169 245
170 246
171 // var url1 = 'http://data.ex.co.kr/openapi/odtraffic/trafficAmountByCongest'; 247 // var url1 = 'http://data.ex.co.kr/openapi/odtraffic/trafficAmountByCongest';
...@@ -186,21 +262,6 @@ function helloworld(isstart, eventObj){ ...@@ -186,21 +262,6 @@ function helloworld(isstart, eventObj){
186 262
187 263
188 264
189 -// var url2 = 'http://data.ex.co.kr/openapi/safeDriving/forecast';
190 -// var queryParams2 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/
191 -// queryParams2 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */
192 -
193 -// request({
194 -// url: url2 + queryParams2,
195 -// method: 'GET'
196 -// }, function (error, response, body) {
197 -// console.log('Status', response.statusCode);
198 -// console.log('Headers', JSON.stringify(response.headers));
199 -// console.log('Reponse received', body);
200 -// var obj = JSON.parse(body);
201 -// console.log(obj);
202 -// console.log(obj.list[0])
203 -// });
204 265
205 // var url3 = 'http://data.ex.co.kr/openapi/trafficapi/trafficAll'; 266 // var url3 = 'http://data.ex.co.kr/openapi/trafficapi/trafficAll';
206 // var queryParams3 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/ 267 // var queryParams3 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/
......