Showing
1 changed file
with
76 additions
and
22 deletions
... | @@ -14,20 +14,26 @@ var async = require('async'); | ... | @@ -14,20 +14,26 @@ var async = require('async'); |
14 | const { resolve } = require('path'); | 14 | const { resolve } = require('path'); |
15 | var Language_Detect_Array = new Array(); | 15 | var Language_Detect_Array = new Array(); |
16 | var User_Info_Array = new Array(); | 16 | var User_Info_Array = new Array(); |
17 | -var Weahter_Info_Array = new Array(); | 17 | +var Weahter_Detect_Array = new Array(); |
18 | 18 | ||
19 | -Weahter_Info_Array[0] = | 19 | +Weahter_Detect_Array[0] = |
20 | { | 20 | { |
21 | - "Location" : '수원', | 21 | + "Region" : '수원', |
22 | "nx" : 61, | 22 | "nx" : 61, |
23 | "ny" : 120 | 23 | "ny" : 120 |
24 | } | 24 | } |
25 | -Weahter_Info_Array[1] = | 25 | +Weahter_Detect_Array[1] = |
26 | { | 26 | { |
27 | - "Location" : '서울', | 27 | + "Region" : '서울', |
28 | "nx" : 60, | 28 | "nx" : 60, |
29 | "ny" : 127 | 29 | "ny" : 127 |
30 | } | 30 | } |
31 | +Weahter_Detect_Array[2] = | ||
32 | +{ | ||
33 | + "Region" : '밀양', | ||
34 | + "nx" : 92, | ||
35 | + "ny" : 83 | ||
36 | +} | ||
31 | 37 | ||
32 | Language_Detect_Array[0] = | 38 | Language_Detect_Array[0] = |
33 | { | 39 | { |
... | @@ -53,7 +59,8 @@ Language_Detect_Array[3] = | ... | @@ -53,7 +59,8 @@ Language_Detect_Array[3] = |
53 | User_Info_Array[0] = | 59 | User_Info_Array[0] = |
54 | { | 60 | { |
55 | UserId : null, | 61 | UserId : null, |
56 | - UserPapagoLanguage : '' | 62 | + UserPapagoLanguage : '', |
63 | + UserRegion : '수원', | ||
57 | } | 64 | } |
58 | 65 | ||
59 | app.use(bodyParser.json()); | 66 | app.use(bodyParser.json()); |
... | @@ -64,7 +71,7 @@ app.post('/hook', function (req, res) { | ... | @@ -64,7 +71,7 @@ app.post('/hook', function (req, res) { |
64 | var replyToken = eventObj.replyToken; | 71 | var replyToken = eventObj.replyToken; |
65 | var userid = eventObj.source.userId; | 72 | var userid = eventObj.source.userId; |
66 | 73 | ||
67 | - if(message == '날씨') weathercheck(replyToken); else AsyncFunction(message, replyToken, userid); | 74 | + AsyncFunction(message, replyToken, userid); |
68 | 75 | ||
69 | res.sendStatus(200); | 76 | res.sendStatus(200); |
70 | }); | 77 | }); |
... | @@ -74,18 +81,29 @@ async function AsyncFunction(message, replyToken, userid) | ... | @@ -74,18 +81,29 @@ async function AsyncFunction(message, replyToken, userid) |
74 | var User_Location = search_user_info(userid); | 81 | var User_Location = search_user_info(userid); |
75 | var DetectLanguage = await detector(message); | 82 | var DetectLanguage = await detector(message); |
76 | if(DetectLanguage != 'ko') var TransToKoreanMessage = await trans(DetectLanguage, message, 'ko'); else var TransToKoreanMessage = message; | 83 | if(DetectLanguage != 'ko') var TransToKoreanMessage = await trans(DetectLanguage, message, 'ko'); else var TransToKoreanMessage = message; |
77 | - var DetectLanguageChange = Detect_Papago_Language_Change(TransToKoreanMessage, User_Location); | 84 | + console.log(TransToKoreanMessage); |
85 | + var DetectChange = Detect_Papago_Language_Change(TransToKoreanMessage, User_Location); | ||
86 | + if (DetectChange) DetectChange = Detect_Weather_Region_Change(TransToKoreanMessage, User_Location); | ||
87 | + if(TransToKoreanMessage != '날씨') | ||
88 | + { | ||
78 | if(DetectLanguage != User_Info_Array[User_Location].UserPapagoLanguage) var TransToUserSettingMessage = await trans(DetectLanguage, message, User_Info_Array[User_Location].UserPapagoLanguage); | 89 | if(DetectLanguage != User_Info_Array[User_Location].UserPapagoLanguage) var TransToUserSettingMessage = await trans(DetectLanguage, message, User_Info_Array[User_Location].UserPapagoLanguage); |
79 | - var Messaging = await messaging(TransToUserSettingMessage, replyToken, DetectLanguageChange); | 90 | + var Messaging = await messaging(TransToUserSettingMessage, replyToken, DetectChange); |
80 | console.log("log-Message: ", Messaging); | 91 | console.log("log-Message: ", Messaging); |
92 | + } | ||
93 | + else | ||
94 | + weathercheck(replyToken, userid, DetectLanguage); | ||
81 | } | 95 | } |
82 | 96 | ||
83 | -async function weathercheck(replyToken) | 97 | +async function weathercheck(replyToken, userid, DetectLanguage) |
84 | { | 98 | { |
99 | + var User_Location = search_user_info(userid); | ||
100 | + var location = user_location(User_Location); | ||
85 | var timevalue = make_time() | 101 | var timevalue = make_time() |
86 | - var url = make_url(timevalue) | 102 | + var url = make_url(timevalue, location); |
87 | - var persentofweawther = await weather_get(url); | 103 | + var message = await weather_get(url, User_Location); |
88 | - var Messaging = await messaging(persentofweawther, replyToken, 1); | 104 | + if(DetectLanguage != 'ko') |
105 | + message = await trans ('ko', message, DetectLanguage) | ||
106 | + var Messaging = await messaging(message, replyToken, 1); | ||
89 | console.log("log-Message: ", Messaging); | 107 | console.log("log-Message: ", Messaging); |
90 | } | 108 | } |
91 | 109 | ||
... | @@ -122,12 +140,12 @@ function detector (message) | ... | @@ -122,12 +140,12 @@ function detector (message) |
122 | }) | 140 | }) |
123 | } | 141 | } |
124 | 142 | ||
125 | -function messaging(message, replyToken, DetectLanguageChange) | 143 | +function messaging(message, replyToken, DetectChange) |
126 | //메시지 보내기 | 144 | //메시지 보내기 |
127 | { | 145 | { |
128 | return new Promise(function(resolve, reject) | 146 | return new Promise(function(resolve, reject) |
129 | { | 147 | { |
130 | - if(DetectLanguageChange) | 148 | + if(DetectChange) |
131 | request.post | 149 | request.post |
132 | ( | 150 | ( |
133 | { | 151 | { |
... | @@ -203,7 +221,8 @@ function search_user_info (userid) | ... | @@ -203,7 +221,8 @@ function search_user_info (userid) |
203 | User_Info_Array[User_Info_Array.length] = | 221 | User_Info_Array[User_Info_Array.length] = |
204 | { | 222 | { |
205 | UserId : userid, | 223 | UserId : userid, |
206 | - UserPapagoLanguage : 'en' | 224 | + UserPapagoLanguage : 'en', |
225 | + UserRegion : '수원' | ||
207 | } | 226 | } |
208 | return User_Info_Array.length-1; | 227 | return User_Info_Array.length-1; |
209 | } | 228 | } |
... | @@ -213,7 +232,6 @@ function search_user_info (userid) | ... | @@ -213,7 +232,6 @@ function search_user_info (userid) |
213 | function Detect_Papago_Language_Change (TransToKoreanMessage, User_Location) | 232 | function Detect_Papago_Language_Change (TransToKoreanMessage, User_Location) |
214 | //번역 언어 변경 감지 | 233 | //번역 언어 변경 감지 |
215 | { | 234 | { |
216 | - console.log('user의 정보는',User_Info_Array[User_Location]); | ||
217 | for(var i in Language_Detect_Array) // Detect Language change | 235 | for(var i in Language_Detect_Array) // Detect Language change |
218 | { | 236 | { |
219 | if (TransToKoreanMessage == Language_Detect_Array[i].Message) | 237 | if (TransToKoreanMessage == Language_Detect_Array[i].Message) |
... | @@ -221,6 +239,7 @@ function Detect_Papago_Language_Change (TransToKoreanMessage, User_Location) | ... | @@ -221,6 +239,7 @@ function Detect_Papago_Language_Change (TransToKoreanMessage, User_Location) |
221 | //언어 변경 감지o | 239 | //언어 변경 감지o |
222 | User_Info_Array[User_Location].UserPapagoLanguage = Language_Detect_Array[i].PapagoLanguage; | 240 | User_Info_Array[User_Location].UserPapagoLanguage = Language_Detect_Array[i].PapagoLanguage; |
223 | return 0; | 241 | return 0; |
242 | + | ||
224 | break; | 243 | break; |
225 | } | 244 | } |
226 | else if ( i == Language_Detect_Array.length-1) | 245 | else if ( i == Language_Detect_Array.length-1) |
... | @@ -231,6 +250,26 @@ function Detect_Papago_Language_Change (TransToKoreanMessage, User_Location) | ... | @@ -231,6 +250,26 @@ function Detect_Papago_Language_Change (TransToKoreanMessage, User_Location) |
231 | } | 250 | } |
232 | } | 251 | } |
233 | 252 | ||
253 | +function Detect_Weather_Region_Change (TransToKoreanMessage, User_Location) | ||
254 | +//지역 변경 감지 | ||
255 | +{ | ||
256 | + for(var i in Weahter_Detect_Array) | ||
257 | + { | ||
258 | + if (TransToKoreanMessage == Weahter_Detect_Array[i].Region) | ||
259 | + { | ||
260 | + //지역 변경 감지o | ||
261 | + User_Info_Array[User_Location].UserRegion = Weahter_Detect_Array[i].Region | ||
262 | + return 0; | ||
263 | + break; | ||
264 | + } | ||
265 | + else if ( i == Weahter_Detect_Array.length-1) | ||
266 | + { | ||
267 | + //지역 변경 감지x | ||
268 | + return 1; | ||
269 | + } | ||
270 | + } | ||
271 | +} | ||
272 | + | ||
234 | function make_time() | 273 | function make_time() |
235 | { | 274 | { |
236 | var today = new Date(); | 275 | var today = new Date(); |
... | @@ -282,7 +321,7 @@ function make_time() | ... | @@ -282,7 +321,7 @@ function make_time() |
282 | return value; | 321 | return value; |
283 | } | 322 | } |
284 | 323 | ||
285 | -function make_url(timevalue) | 324 | +function make_url(timevalue, location) |
286 | { | 325 | { |
287 | var serviceKey = weather_key; | 326 | var serviceKey = weather_key; |
288 | var pageNo = '1'; | 327 | var pageNo = '1'; |
... | @@ -290,8 +329,8 @@ function make_url(timevalue) | ... | @@ -290,8 +329,8 @@ function make_url(timevalue) |
290 | var dataType = 'JSON'; | 329 | var dataType = 'JSON'; |
291 | var base_date = timevalue.today; | 330 | var base_date = timevalue.today; |
292 | var base_time = timevalue.hours; | 331 | var base_time = timevalue.hours; |
293 | - var nx = '61'; | 332 | + var nx = location.nx; |
294 | - var ny = '120'; | 333 | + var ny = location.ny; |
295 | 334 | ||
296 | var url = `${WEATHER_TARGET_URL}`; | 335 | var url = `${WEATHER_TARGET_URL}`; |
297 | url += '?serviceKey='+serviceKey; | 336 | url += '?serviceKey='+serviceKey; |
... | @@ -305,8 +344,23 @@ function make_url(timevalue) | ... | @@ -305,8 +344,23 @@ function make_url(timevalue) |
305 | return url; | 344 | return url; |
306 | } | 345 | } |
307 | 346 | ||
347 | +function user_location(User_Location) | ||
348 | +{ | ||
349 | + var location_info = new Object(); | ||
350 | + for(var i in Weahter_Detect_Array) | ||
351 | + { | ||
352 | + if (User_Info_Array[User_Location].UserRegion == Weahter_Detect_Array[i].Region) | ||
353 | + { | ||
354 | + location_info.nx = Weahter_Detect_Array[i].nx; | ||
355 | + location_info.ny = Weahter_Detect_Array[i].ny; | ||
356 | + return location_info; | ||
357 | + break; | ||
358 | + } | ||
359 | + } | ||
360 | +} | ||
361 | + | ||
308 | 362 | ||
309 | -function weather_get (url) | 363 | +function weather_get (url, User_Location) |
310 | { | 364 | { |
311 | return new Promise(function(resolve, reject) | 365 | return new Promise(function(resolve, reject) |
312 | { | 366 | { |
... | @@ -318,7 +372,7 @@ function weather_get (url) | ... | @@ -318,7 +372,7 @@ function weather_get (url) |
318 | if(!error && response.statusCode == 200) | 372 | if(!error && response.statusCode == 200) |
319 | { | 373 | { |
320 | var weatherObj = JSON.parse(response.body); | 374 | var weatherObj = JSON.parse(response.body); |
321 | - resolve('4시간 안에 비가 올 확률은'+weatherObj.response.body.items.item[0].fcstValue+'%입니다.'); | 375 | + resolve(User_Info_Array[User_Location].UserRegion+'에 4시간 안에 비가 올 확률은 '+weatherObj.response.body.items.item[0].fcstValue+'% 입니다.'); |
322 | } | 376 | } |
323 | else{ | 377 | else{ |
324 | console.log('Error', error); | 378 | console.log('Error', error); | ... | ... |
-
Please register or login to post a comment