Showing
1 changed file
with
222 additions
and
135 deletions
1 | -var end = false; | 1 | +var end = 'not'; |
2 | var count = 0; | 2 | var count = 0; |
3 | +var n = 1; | ||
4 | +var choosen = new Array(); | ||
5 | +var opt_1 = ['Korean cuisine', 'Chinese food', 'Japanese food', 'Western food', 'Guitar']; | ||
3 | var express = require('express'); | 6 | var express = require('express'); |
4 | const request = require('request'); | 7 | const request = require('request'); |
5 | -const TARGET_URL = 'https://api.line.me/v2/bot/message/push' | 8 | +const TARGET_URL = '라인 URL' |
6 | -const TOKEN = 'Ur8nbk2bQAc9KavLSSJv0uzlKWCxCBnOJOKyqbRS1mMHWgJlEOPfb7v+X1DDEHocUmhU7KnPFN+KHt5S3SYd5QmF42aTeQpW2gDVTjyqty1Tn+p6QqEhI/uSz659PBHwczEYn0Plx+akfCNHDPJRpAdB04t89/1O/w1cDnyilFU=' | 9 | +const TOKEN = '라인 토큰' |
7 | -const USER_ID = 'Ub5fa0ab321e4713638a85fbfa7ecc20b' | 10 | +const PAPAGO_URL = '파파고 API URL' |
11 | +const PAPAGO_ID = '파파고 ID' | ||
12 | +const PAPAGO_SECRET = '파파고 시크릿' | ||
8 | const fs = require('fs'); | 13 | const fs = require('fs'); |
9 | const path = require('path'); | 14 | const path = require('path'); |
10 | const HTTPS = require('https'); | 15 | const HTTPS = require('https'); |
11 | -const domain = "2021105602.osschatbot2022.tk" | 16 | +const domain = "도메인" |
12 | const sslport = 23023; | 17 | const sslport = 23023; |
13 | const bodyParser = require('body-parser'); | 18 | const bodyParser = require('body-parser'); |
14 | -var choosen = []; | 19 | +const res = require('express/lib/response'); |
15 | var app = express(); | 20 | var app = express(); |
16 | -do{ | 21 | +app.use(bodyParser.json()); |
17 | - app.use(bodyParser.json()); | 22 | +app.post('/hook', function (req, res) { |
18 | - app.post('/hook', function (req, res) { | 23 | + n=1; |
19 | - | ||
20 | var eventObj = req.body.events[0]; | 24 | var eventObj = req.body.events[0]; |
21 | var source = eventObj.source; | 25 | var source = eventObj.source; |
22 | var message = eventObj.message; | 26 | var message = eventObj.message; |
23 | 27 | ||
24 | // request log | 28 | // request log |
25 | - console.log('======================', new Date() ,'======================'); | 29 | + console.log('======================', new Date(), '======================'); |
26 | console.log('[request]', req.body); | 30 | console.log('[request]', req.body); |
27 | console.log('[request source] ', eventObj.source); | 31 | console.log('[request source] ', eventObj.source); |
28 | console.log('[request message]', eventObj.message); | 32 | console.log('[request message]', eventObj.message); |
33 | + // switch (count) { | ||
34 | + // case 0: | ||
35 | + if (eventObj.message.text == 'start') { | ||
29 | 36 | ||
30 | - var msg = eventObj.message.text; | 37 | + count++; |
31 | - if(eventObj.message.text == 'start') | 38 | + start_msg(eventObj.replyToken, eventObj.message.text); |
32 | - { | 39 | + |
33 | - request.post( | 40 | + sleep(5000); |
34 | - { | 41 | + |
35 | - url: TARGET_URL, | 42 | + for(var i = 0; i<5; i++) |
36 | - headers: { | ||
37 | - 'Authorization': `Bearer ${TOKEN}` | ||
38 | - }, | ||
39 | - json: { | ||
40 | - "replyToken":eventObj.replyToken, | ||
41 | - "messages":[ | ||
42 | - { | ||
43 | - "type":"text", | ||
44 | - "text":"Hi\nThis is Menu recommend Chatbot!\nIf you follow the instructions, I will recommend the menu!" | ||
45 | - }, | ||
46 | { | 43 | { |
47 | - "type":"text", | 44 | + option(eventObj.replyToken, opt_1[i]); |
48 | - "text":"Choose a number\n1. 한식\n2. 중식\n3. 일식\n4. 양식\n5. 기타" | 45 | + console.log(opt_1[i]); |
46 | + n++; | ||
47 | + sleep(5000); | ||
49 | } | 48 | } |
50 | - ] | ||
51 | } | 49 | } |
52 | - },(error, response, body) => { | 50 | + // break; |
53 | - console.log(body) | 51 | + // case 1: |
54 | - }); | 52 | + // if (eventObj.message.text === '1' || eventObj.message.text === '2' || eventObj.message.text === '3' || eventObj.message.text == '4' || eventObj.message.texts == '5') { |
55 | - count++; | 53 | + // q_1(eventObj.replyToken, eventObj.message.text); |
56 | - }else if(count == 1 & !end) | 54 | + // } else { |
57 | - { | 55 | + // request.post( |
58 | - if(msg == '1' | msg == '2' | msg == '3' | msg == '4' | msg == '5') | 56 | + // { |
59 | - { | 57 | + // url: TARGET_URL, |
60 | - choosen.push(msg); | 58 | + // headers: { |
61 | - count++; | 59 | + // 'Authorization': `Bearer ${TOKEN}` |
60 | + // }, | ||
61 | + // json: { | ||
62 | + // "replyToken": eventObj.replyToken, | ||
63 | + // "messages": [ | ||
64 | + // { | ||
65 | + // "type": "text", | ||
66 | + // "text": "Please choose a number between 1 to 5!" | ||
67 | + // } | ||
68 | + // ] | ||
69 | + // } | ||
70 | + // }, (error, response, body) => { | ||
71 | + // console.log(body) | ||
72 | + // }); | ||
73 | + // } | ||
74 | + // break; | ||
75 | + // case 2: | ||
76 | + // if (eventObj.message.text === '1' || eventObj.message.text === '2' || eventObj.message.text === '3') { | ||
77 | + // q_2(eventObj.replyToken, eventObj.message.text); | ||
78 | + // } else { | ||
79 | + // request.post( | ||
80 | + // { | ||
81 | + // url: TARGET_URL, | ||
82 | + // headers: { | ||
83 | + // 'Authorization': `Bearer ${TOKEN}` | ||
84 | + // }, | ||
85 | + // json: { | ||
86 | + // "replyToken": eventObj.replyToken, | ||
87 | + // "messages": [ | ||
88 | + // { | ||
89 | + // "type": "text", | ||
90 | + // "text": "Please choose a number between 1 to 3!" | ||
91 | + // } | ||
92 | + // ] | ||
93 | + // } | ||
94 | + // }, (error, response, body) => { | ||
95 | + // console.log(body) | ||
96 | + // }); | ||
97 | + // } | ||
98 | + // break; | ||
99 | + // case 3: | ||
100 | + // if (eventObj.message.text === '1' || eventObj.message.text === '2' || eventObj.message.text === '3') { | ||
101 | + // q_2(eventObj.replyToken, eventObj.message.text); | ||
102 | + // } else { | ||
103 | + // request.post( | ||
104 | + // { | ||
105 | + // url: TARGET_URL, | ||
106 | + // headers: { | ||
107 | + // 'Authorization': `Bearer ${TOKEN}` | ||
108 | + // }, | ||
109 | + // json: { | ||
110 | + // "replyToken": eventObj.replyToken, | ||
111 | + // "messages": [ | ||
112 | + // { | ||
113 | + // "type": "text", | ||
114 | + // "text": "Please choose a number between 1 to 3!" | ||
115 | + // } | ||
116 | + // ] | ||
117 | + // } | ||
118 | + // }, (error, response, body) => { | ||
119 | + // console.log(body) | ||
120 | + // }); | ||
121 | + // } | ||
122 | + // break; | ||
123 | + // case 4: | ||
124 | + // if (eventObj.message.text === '1' || eventObj.message.text === '2') { | ||
125 | + // q_4(eventObj.replyToken, eventObj.message.text); | ||
126 | + // } else { | ||
127 | + // request.post( | ||
128 | + // { | ||
129 | + // url: TARGET_URL, | ||
130 | + // headers: { | ||
131 | + // 'Authorization': `Bearer ${TOKEN}` | ||
132 | + // }, | ||
133 | + // json: { | ||
134 | + // "replyToken": eventObj.replyToken, | ||
135 | + // "messages": [ | ||
136 | + // { | ||
137 | + // "type": "text", | ||
138 | + // "text": "Please choose a number between 1 to 2!" | ||
139 | + // } | ||
140 | + // ] | ||
141 | + // } | ||
142 | + // }, (error, response, body) => { | ||
143 | + // console.log(body) | ||
144 | + // }); | ||
145 | + // } | ||
146 | + // break; | ||
147 | + // case 5: | ||
148 | + // end = 'end'; | ||
149 | + // break; | ||
150 | + // default: | ||
151 | + // break; | ||
152 | + // } | ||
153 | + | ||
154 | + | ||
155 | + res.sendStatus(200); | ||
156 | +}); | ||
157 | + | ||
158 | +function start_msg(replyToken, message) { | ||
62 | request.post( | 159 | request.post( |
63 | { | 160 | { |
64 | - url: TARGET_URL, | 161 | + url: PAPAGO_URL, |
65 | headers: { | 162 | headers: { |
66 | - 'Authorization': `Bearer ${TOKEN}` | 163 | + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', |
164 | + 'X-Naver-Client-Id': `${PAPAGO_ID}`, | ||
165 | + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
67 | }, | 166 | }, |
68 | - json: { | 167 | + body: 'source=en&target=ko&text=' + "Hi This is Menu recommend Chatbot! If you follow the instructions, I will recommend the menu", |
69 | - "replyToken":eventObj.replyToken, | 168 | + json: true |
70 | - "messages":[ | 169 | + }, (error, response, body) => { |
71 | - { | 170 | + if (!error && response.statusCode == 200) { |
72 | - "type":"text", | 171 | + console.log(body.message); |
73 | - "text":"Choose a number\n1. 고기\n2. 해산물\n3. 기타" | 172 | + var transMessage = body.message.result.translatedText; |
74 | - } | ||
75 | - ] | ||
76 | - } | ||
77 | - },(error, response, body) => { | ||
78 | - console.log(body) | ||
79 | - }); | ||
80 | - }else{ | ||
81 | request.post( | 173 | request.post( |
82 | { | 174 | { |
83 | url: TARGET_URL, | 175 | url: TARGET_URL, |
... | @@ -85,43 +177,38 @@ do{ | ... | @@ -85,43 +177,38 @@ do{ |
85 | 'Authorization': `Bearer ${TOKEN}` | 177 | 'Authorization': `Bearer ${TOKEN}` |
86 | }, | 178 | }, |
87 | json: { | 179 | json: { |
88 | - "replyToken":eventObj.replyToken, | 180 | + "replyToken": replyToken, |
89 | - "messages":[ | 181 | + "messages": [ |
90 | { | 182 | { |
91 | - "type":"text", | 183 | + "type": "text", |
92 | - "text":"Please choose a number between 1 to 5!" | 184 | + "text": transMessage |
93 | } | 185 | } |
94 | ] | 186 | ] |
95 | } | 187 | } |
96 | - },(error, response, body) => { | 188 | + }, (error, response, body) => { |
97 | console.log(body) | 189 | console.log(body) |
98 | }); | 190 | }); |
99 | } | 191 | } |
100 | - }else if(count == 2 & !end) | 192 | + }); |
101 | - { | 193 | + sleep(5000); |
102 | - if(msg == 1 | msg == 2 | msg==3) | 194 | +} |
103 | - { | 195 | + |
104 | - choosen.push(msg); | 196 | +function option(replyToken, message) |
105 | - count++; | 197 | +{ |
106 | request.post( | 198 | request.post( |
107 | { | 199 | { |
108 | - url: TARGET_URL, | 200 | + url: PAPAGO_URL, |
109 | headers: { | 201 | headers: { |
110 | - 'Authorization': `Bearer ${TOKEN}` | 202 | + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', |
203 | + 'X-Naver-Client-Id': `${PAPAGO_ID}`, | ||
204 | + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
111 | }, | 205 | }, |
112 | - json: { | 206 | + body: 'source=en&target=ko&text=' + n + "." + message, |
113 | - "replyToken":eventObj.replyToken, | 207 | + json: true |
114 | - "messages":[ | 208 | + }, (error, response, body) => { |
115 | - { | 209 | + if (!error && response.statusCode == 200) { |
116 | - "type":"text", | 210 | + console.log(body.message); |
117 | - "text":"Choose a number\n1. 뜨거운 음식\n2. 찬 음식" | 211 | + var transMessage = body.message.result.translatedText; |
118 | - } | ||
119 | - ] | ||
120 | - } | ||
121 | - },(error, response, body) => { | ||
122 | - console.log(body) | ||
123 | - }); | ||
124 | - }else{ | ||
125 | request.post( | 212 | request.post( |
126 | { | 213 | { |
127 | url: TARGET_URL, | 214 | url: TARGET_URL, |
... | @@ -129,24 +216,25 @@ do{ | ... | @@ -129,24 +216,25 @@ do{ |
129 | 'Authorization': `Bearer ${TOKEN}` | 216 | 'Authorization': `Bearer ${TOKEN}` |
130 | }, | 217 | }, |
131 | json: { | 218 | json: { |
132 | - "replyToken":eventObj.replyToken, | 219 | + "replyToken": replyToken, |
133 | - "messages":[ | 220 | + "messages": [ |
134 | { | 221 | { |
135 | - "type":"text", | 222 | + "type": "text", |
136 | - "text":"Please choose a number between 1 to 3!" | 223 | + "text": transMessage |
137 | } | 224 | } |
138 | ] | 225 | ] |
139 | } | 226 | } |
140 | - },(error, response, body) => { | 227 | + }, (error, response, body) => { |
141 | console.log(body) | 228 | console.log(body) |
142 | }); | 229 | }); |
143 | } | 230 | } |
144 | - }else if(count == 3 & !end) | 231 | + }); |
145 | - { | 232 | + sleep(5000); |
146 | - if(msg == 1 | msg == 2) | 233 | +} |
147 | - { | 234 | + |
148 | - choosen.push(msg); | 235 | +function q_1(replyToken, msg) { |
149 | count++; | 236 | count++; |
237 | + choosen.push(msg); | ||
150 | request.post( | 238 | request.post( |
151 | { | 239 | { |
152 | url: TARGET_URL, | 240 | url: TARGET_URL, |
... | @@ -154,18 +242,22 @@ do{ | ... | @@ -154,18 +242,22 @@ do{ |
154 | 'Authorization': `Bearer ${TOKEN}` | 242 | 'Authorization': `Bearer ${TOKEN}` |
155 | }, | 243 | }, |
156 | json: { | 244 | json: { |
157 | - "replyToken":eventObj.replyToken, | 245 | + "replyToken": replyToken, |
158 | - "messages":[ | 246 | + "messages": [ |
159 | { | 247 | { |
160 | - "type":"text", | 248 | + "type": "text", |
161 | - "text":"Choose a number\n1. 국물\n2. 국물 없는 음식" | 249 | + "text": "Choose a number\n1. 고기\n2. 해산물\n3. 기타" |
162 | } | 250 | } |
163 | ] | 251 | ] |
164 | } | 252 | } |
165 | - },(error, response, body) => { | 253 | + }, (error, response, body) => { |
166 | console.log(body) | 254 | console.log(body) |
167 | }); | 255 | }); |
168 | - }else{ | 256 | +} |
257 | + | ||
258 | +function q_2(replyToken, msg) { | ||
259 | + count++; | ||
260 | + choosen.push(msg); | ||
169 | request.post( | 261 | request.post( |
170 | { | 262 | { |
171 | url: TARGET_URL, | 263 | url: TARGET_URL, |
... | @@ -173,24 +265,22 @@ do{ | ... | @@ -173,24 +265,22 @@ do{ |
173 | 'Authorization': `Bearer ${TOKEN}` | 265 | 'Authorization': `Bearer ${TOKEN}` |
174 | }, | 266 | }, |
175 | json: { | 267 | json: { |
176 | - "replyToken":eventObj.replyToken, | 268 | + "replyToken": replyToken, |
177 | - "messages":[ | 269 | + "messages": [ |
178 | { | 270 | { |
179 | - "type":"text", | 271 | + "type": "text", |
180 | - "text":"Please choose a number between 1 to 2!" | 272 | + "text": "Choose a number\n1. 뜨거운 음식\n2. 찬 음식" |
181 | } | 273 | } |
182 | ] | 274 | ] |
183 | } | 275 | } |
184 | - },(error, response, body) => { | 276 | + }, (error, response, body) => { |
185 | console.log(body) | 277 | console.log(body) |
186 | }); | 278 | }); |
187 | - } | 279 | +} |
188 | - }else if(count == 4 & !end) | 280 | + |
189 | - { | 281 | +function q_3(replyToken, msg) { |
190 | - if(msg == 1 | msg == 2) | ||
191 | - { | ||
192 | - choosen.push(msg); | ||
193 | count++; | 282 | count++; |
283 | + choosen.push(msg); | ||
194 | request.post( | 284 | request.post( |
195 | { | 285 | { |
196 | url: TARGET_URL, | 286 | url: TARGET_URL, |
... | @@ -198,18 +288,22 @@ do{ | ... | @@ -198,18 +288,22 @@ do{ |
198 | 'Authorization': `Bearer ${TOKEN}` | 288 | 'Authorization': `Bearer ${TOKEN}` |
199 | }, | 289 | }, |
200 | json: { | 290 | json: { |
201 | - "replyToken":eventObj.replyToken, | 291 | + "replyToken": replyToken, |
202 | - "messages":[ | 292 | + "messages": [ |
203 | { | 293 | { |
204 | - "type":"text", | 294 | + "type": "text", |
205 | - "text":"Choose a number\n1. 매운 음식\n2. 안매운 음식" | 295 | + "text": "Choose a number\n1. 국물\n2. 국물 없는 음식" |
206 | } | 296 | } |
207 | ] | 297 | ] |
208 | } | 298 | } |
209 | - },(error, response, body) => { | 299 | + }, (error, response, body) => { |
210 | console.log(body) | 300 | console.log(body) |
211 | }); | 301 | }); |
212 | - }else{ | 302 | +} |
303 | + | ||
304 | +function q_4(replyToken, msg) { | ||
305 | + count++; | ||
306 | + choosen.push(msg); | ||
213 | request.post( | 307 | request.post( |
214 | { | 308 | { |
215 | url: TARGET_URL, | 309 | url: TARGET_URL, |
... | @@ -217,42 +311,35 @@ do{ | ... | @@ -217,42 +311,35 @@ do{ |
217 | 'Authorization': `Bearer ${TOKEN}` | 311 | 'Authorization': `Bearer ${TOKEN}` |
218 | }, | 312 | }, |
219 | json: { | 313 | json: { |
220 | - "replyToken":eventObj.replyToken, | 314 | + "replyToken": replyToken, |
221 | - "messages":[ | 315 | + "messages": [ |
222 | { | 316 | { |
223 | - "type":"text", | 317 | + "type": "text", |
224 | - "text":"Please choose a number between 1 to 2!" | 318 | + "text": "Choose a number\n1. 매운 음식\n2. 안매운 음식" |
225 | } | 319 | } |
226 | ] | 320 | ] |
227 | } | 321 | } |
228 | - },(error, response, body) => { | 322 | + }, (error, response, body) => { |
229 | console.log(body) | 323 | console.log(body) |
230 | }); | 324 | }); |
231 | - } | 325 | +} |
232 | - } | ||
233 | 326 | ||
234 | - if(count == 5) | 327 | +function sleep(ms) { |
235 | - { | 328 | + const wakeUpTime = Date.now() + ms; |
236 | - end == true; | 329 | + while (Date.now() < wakeUpTime) {} |
237 | } | 330 | } |
238 | 331 | ||
239 | - res.sendStatus(200); | ||
240 | - }); | ||
241 | -}while(!end); | ||
242 | - | ||
243 | - | ||
244 | try { | 332 | try { |
245 | const option = { | 333 | const option = { |
246 | - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | 334 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'), |
247 | - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | 335 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(), |
248 | - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | 336 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(), |
249 | }; | 337 | }; |
250 | 338 | ||
251 | HTTPS.createServer(option, app).listen(sslport, () => { | 339 | HTTPS.createServer(option, app).listen(sslport, () => { |
252 | console.log(`[HTTPS] Server is started on port ${sslport}`); | 340 | console.log(`[HTTPS] Server is started on port ${sslport}`); |
253 | }); | 341 | }); |
254 | - } catch (error) { | 342 | +} catch (error) { |
255 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | 343 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); |
256 | console.log(error); | 344 | console.log(error); |
257 | - } | ||
258 | - | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
345 | +} | ... | ... |
-
Please register or login to post a comment