Showing
1 changed file
with
284 additions
and
197 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; |
24 | + var eventObj = req.body.events[0]; | ||
25 | + var source = eventObj.source; | ||
26 | + var message = eventObj.message; | ||
19 | 27 | ||
20 | - var eventObj = req.body.events[0]; | 28 | + // request log |
21 | - var source = eventObj.source; | 29 | + console.log('======================', new Date(), '======================'); |
22 | - var message = eventObj.message; | 30 | + console.log('[request]', req.body); |
31 | + console.log('[request source] ', eventObj.source); | ||
32 | + console.log('[request message]', eventObj.message); | ||
33 | + // switch (count) { | ||
34 | + // case 0: | ||
35 | + if (eventObj.message.text == 'start') { | ||
23 | 36 | ||
24 | - // request log | 37 | + count++; |
25 | - console.log('======================', new Date() ,'======================'); | 38 | + start_msg(eventObj.replyToken, eventObj.message.text); |
26 | - console.log('[request]', req.body); | ||
27 | - console.log('[request source] ', eventObj.source); | ||
28 | - console.log('[request message]', eventObj.message); | ||
29 | 39 | ||
30 | - var msg = eventObj.message.text; | 40 | + sleep(5000); |
31 | - if(eventObj.message.text == 'start') | 41 | + |
42 | + for(var i = 0; i<5; i++) | ||
32 | { | 43 | { |
33 | - request.post( | 44 | + option(eventObj.replyToken, opt_1[i]); |
34 | - { | 45 | + console.log(opt_1[i]); |
35 | - url: TARGET_URL, | 46 | + n++; |
36 | - headers: { | 47 | + sleep(5000); |
37 | - 'Authorization': `Bearer ${TOKEN}` | 48 | + } |
38 | - }, | 49 | + } |
39 | - json: { | 50 | + // break; |
40 | - "replyToken":eventObj.replyToken, | 51 | + // case 1: |
41 | - "messages":[ | 52 | + // if (eventObj.message.text === '1' || eventObj.message.text === '2' || eventObj.message.text === '3' || eventObj.message.text == '4' || eventObj.message.texts == '5') { |
42 | - { | 53 | + // q_1(eventObj.replyToken, eventObj.message.text); |
43 | - "type":"text", | 54 | + // } else { |
44 | - "text":"Hi\nThis is Menu recommend Chatbot!\nIf you follow the instructions, I will recommend the menu!" | 55 | + // request.post( |
45 | - }, | 56 | + // { |
46 | - { | 57 | + // url: TARGET_URL, |
47 | - "type":"text", | 58 | + // headers: { |
48 | - "text":"Choose a number\n1. 한식\n2. 중식\n3. 일식\n4. 양식\n5. 기타" | 59 | + // 'Authorization': `Bearer ${TOKEN}` |
49 | - } | 60 | + // }, |
50 | - ] | 61 | + // json: { |
51 | - } | 62 | + // "replyToken": eventObj.replyToken, |
52 | - },(error, response, body) => { | 63 | + // "messages": [ |
53 | - console.log(body) | 64 | + // { |
54 | - }); | 65 | + // "type": "text", |
55 | - count++; | 66 | + // "text": "Please choose a number between 1 to 5!" |
56 | - }else if(count == 1 & !end) | 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) { | ||
159 | + request.post( | ||
57 | { | 160 | { |
58 | - if(msg == '1' | msg == '2' | msg == '3' | msg == '4' | msg == '5') | 161 | + url: PAPAGO_URL, |
59 | - { | 162 | + headers: { |
60 | - choosen.push(msg); | 163 | + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', |
61 | - count++; | 164 | + 'X-Naver-Client-Id': `${PAPAGO_ID}`, |
165 | + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
166 | + }, | ||
167 | + body: 'source=en&target=ko&text=' + "Hi This is Menu recommend Chatbot! If you follow the instructions, I will recommend the menu", | ||
168 | + json: true | ||
169 | + }, (error, response, body) => { | ||
170 | + if (!error && response.statusCode == 200) { | ||
171 | + console.log(body.message); | ||
172 | + var transMessage = body.message.result.translatedText; | ||
62 | request.post( | 173 | request.post( |
63 | { | 174 | { |
64 | url: TARGET_URL, | 175 | url: TARGET_URL, |
... | @@ -66,43 +177,38 @@ do{ | ... | @@ -66,43 +177,38 @@ do{ |
66 | 'Authorization': `Bearer ${TOKEN}` | 177 | 'Authorization': `Bearer ${TOKEN}` |
67 | }, | 178 | }, |
68 | json: { | 179 | json: { |
69 | - "replyToken":eventObj.replyToken, | 180 | + "replyToken": replyToken, |
70 | - "messages":[ | 181 | + "messages": [ |
71 | { | 182 | { |
72 | - "type":"text", | 183 | + "type": "text", |
73 | - "text":"Choose a number\n1. 고기\n2. 해산물\n3. 기타" | 184 | + "text": transMessage |
74 | } | 185 | } |
75 | ] | 186 | ] |
76 | } | 187 | } |
77 | - },(error, response, body) => { | 188 | + }, (error, response, body) => { |
78 | - console.log(body) | ||
79 | - }); | ||
80 | - }else{ | ||
81 | - request.post( | ||
82 | - { | ||
83 | - url: TARGET_URL, | ||
84 | - headers: { | ||
85 | - 'Authorization': `Bearer ${TOKEN}` | ||
86 | - }, | ||
87 | - json: { | ||
88 | - "replyToken":eventObj.replyToken, | ||
89 | - "messages":[ | ||
90 | - { | ||
91 | - "type":"text", | ||
92 | - "text":"Please choose a number between 1 to 5!" | ||
93 | - } | ||
94 | - ] | ||
95 | - } | ||
96 | - },(error, response, body) => { | ||
97 | console.log(body) | 189 | console.log(body) |
98 | }); | 190 | }); |
99 | } | 191 | } |
100 | - }else if(count == 2 & !end) | 192 | + }); |
193 | + sleep(5000); | ||
194 | +} | ||
195 | + | ||
196 | +function option(replyToken, message) | ||
197 | +{ | ||
198 | + request.post( | ||
101 | { | 199 | { |
102 | - if(msg == 1 | msg == 2 | msg==3) | 200 | + url: PAPAGO_URL, |
103 | - { | 201 | + headers: { |
104 | - choosen.push(msg); | 202 | + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', |
105 | - count++; | 203 | + 'X-Naver-Client-Id': `${PAPAGO_ID}`, |
204 | + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
205 | + }, | ||
206 | + body: 'source=en&target=ko&text=' + n + "." + message, | ||
207 | + json: true | ||
208 | + }, (error, response, body) => { | ||
209 | + if (!error && response.statusCode == 200) { | ||
210 | + console.log(body.message); | ||
211 | + var transMessage = body.message.result.translatedText; | ||
106 | request.post( | 212 | request.post( |
107 | { | 213 | { |
108 | url: TARGET_URL, | 214 | url: TARGET_URL, |
... | @@ -110,149 +216,130 @@ do{ | ... | @@ -110,149 +216,130 @@ do{ |
110 | 'Authorization': `Bearer ${TOKEN}` | 216 | 'Authorization': `Bearer ${TOKEN}` |
111 | }, | 217 | }, |
112 | json: { | 218 | json: { |
113 | - "replyToken":eventObj.replyToken, | 219 | + "replyToken": replyToken, |
114 | - "messages":[ | 220 | + "messages": [ |
115 | { | 221 | { |
116 | - "type":"text", | 222 | + "type": "text", |
117 | - "text":"Choose a number\n1. 뜨거운 음식\n2. 찬 음식" | 223 | + "text": transMessage |
118 | } | 224 | } |
119 | ] | 225 | ] |
120 | } | 226 | } |
121 | - },(error, response, body) => { | 227 | + }, (error, response, body) => { |
122 | - console.log(body) | ||
123 | - }); | ||
124 | - }else{ | ||
125 | - request.post( | ||
126 | - { | ||
127 | - url: TARGET_URL, | ||
128 | - headers: { | ||
129 | - 'Authorization': `Bearer ${TOKEN}` | ||
130 | - }, | ||
131 | - json: { | ||
132 | - "replyToken":eventObj.replyToken, | ||
133 | - "messages":[ | ||
134 | - { | ||
135 | - "type":"text", | ||
136 | - "text":"Please choose a number between 1 to 3!" | ||
137 | - } | ||
138 | - ] | ||
139 | - } | ||
140 | - },(error, response, body) => { | ||
141 | console.log(body) | 228 | console.log(body) |
142 | }); | 229 | }); |
143 | } | 230 | } |
144 | - }else if(count == 3 & !end) | 231 | + }); |
232 | + sleep(5000); | ||
233 | +} | ||
234 | + | ||
235 | +function q_1(replyToken, msg) { | ||
236 | + count++; | ||
237 | + choosen.push(msg); | ||
238 | + request.post( | ||
145 | { | 239 | { |
146 | - if(msg == 1 | msg == 2) | 240 | + url: TARGET_URL, |
147 | - { | 241 | + headers: { |
148 | - choosen.push(msg); | 242 | + 'Authorization': `Bearer ${TOKEN}` |
149 | - count++; | 243 | + }, |
150 | - request.post( | 244 | + json: { |
151 | - { | 245 | + "replyToken": replyToken, |
152 | - url: TARGET_URL, | 246 | + "messages": [ |
153 | - headers: { | ||
154 | - 'Authorization': `Bearer ${TOKEN}` | ||
155 | - }, | ||
156 | - json: { | ||
157 | - "replyToken":eventObj.replyToken, | ||
158 | - "messages":[ | ||
159 | - { | ||
160 | - "type":"text", | ||
161 | - "text":"Choose a number\n1. 국물\n2. 국물 없는 음식" | ||
162 | - } | ||
163 | - ] | ||
164 | - } | ||
165 | - },(error, response, body) => { | ||
166 | - console.log(body) | ||
167 | - }); | ||
168 | - }else{ | ||
169 | - request.post( | ||
170 | { | 247 | { |
171 | - url: TARGET_URL, | 248 | + "type": "text", |
172 | - headers: { | 249 | + "text": "Choose a number\n1. 고기\n2. 해산물\n3. 기타" |
173 | - 'Authorization': `Bearer ${TOKEN}` | 250 | + } |
174 | - }, | 251 | + ] |
175 | - json: { | ||
176 | - "replyToken":eventObj.replyToken, | ||
177 | - "messages":[ | ||
178 | - { | ||
179 | - "type":"text", | ||
180 | - "text":"Please choose a number between 1 to 2!" | ||
181 | - } | ||
182 | - ] | ||
183 | - } | ||
184 | - },(error, response, body) => { | ||
185 | - console.log(body) | ||
186 | - }); | ||
187 | } | 252 | } |
188 | - }else if(count == 4 & !end) | 253 | + }, (error, response, body) => { |
254 | + console.log(body) | ||
255 | + }); | ||
256 | +} | ||
257 | + | ||
258 | +function q_2(replyToken, msg) { | ||
259 | + count++; | ||
260 | + choosen.push(msg); | ||
261 | + request.post( | ||
189 | { | 262 | { |
190 | - if(msg == 1 | msg == 2) | 263 | + url: TARGET_URL, |
191 | - { | 264 | + headers: { |
192 | - choosen.push(msg); | 265 | + 'Authorization': `Bearer ${TOKEN}` |
193 | - count++; | 266 | + }, |
194 | - request.post( | 267 | + json: { |
268 | + "replyToken": replyToken, | ||
269 | + "messages": [ | ||
195 | { | 270 | { |
196 | - url: TARGET_URL, | 271 | + "type": "text", |
197 | - headers: { | 272 | + "text": "Choose a number\n1. 뜨거운 음식\n2. 찬 음식" |
198 | - 'Authorization': `Bearer ${TOKEN}` | 273 | + } |
199 | - }, | 274 | + ] |
200 | - json: { | 275 | + } |
201 | - "replyToken":eventObj.replyToken, | 276 | + }, (error, response, body) => { |
202 | - "messages":[ | 277 | + console.log(body) |
203 | - { | 278 | + }); |
204 | - "type":"text", | 279 | +} |
205 | - "text":"Choose a number\n1. 매운 음식\n2. 안매운 음식" | 280 | + |
206 | - } | 281 | +function q_3(replyToken, msg) { |
207 | - ] | 282 | + count++; |
208 | - } | 283 | + choosen.push(msg); |
209 | - },(error, response, body) => { | 284 | + request.post( |
210 | - console.log(body) | 285 | + { |
211 | - }); | 286 | + url: TARGET_URL, |
212 | - }else{ | 287 | + headers: { |
213 | - request.post( | 288 | + 'Authorization': `Bearer ${TOKEN}` |
289 | + }, | ||
290 | + json: { | ||
291 | + "replyToken": replyToken, | ||
292 | + "messages": [ | ||
214 | { | 293 | { |
215 | - url: TARGET_URL, | 294 | + "type": "text", |
216 | - headers: { | 295 | + "text": "Choose a number\n1. 국물\n2. 국물 없는 음식" |
217 | - 'Authorization': `Bearer ${TOKEN}` | 296 | + } |
218 | - }, | 297 | + ] |
219 | - json: { | ||
220 | - "replyToken":eventObj.replyToken, | ||
221 | - "messages":[ | ||
222 | - { | ||
223 | - "type":"text", | ||
224 | - "text":"Please choose a number between 1 to 2!" | ||
225 | - } | ||
226 | - ] | ||
227 | - } | ||
228 | - },(error, response, body) => { | ||
229 | - console.log(body) | ||
230 | - }); | ||
231 | } | 298 | } |
232 | - } | 299 | + }, (error, response, body) => { |
300 | + console.log(body) | ||
301 | + }); | ||
302 | +} | ||
233 | 303 | ||
234 | - if(count == 5) | 304 | +function q_4(replyToken, msg) { |
305 | + count++; | ||
306 | + choosen.push(msg); | ||
307 | + request.post( | ||
235 | { | 308 | { |
236 | - end == true; | 309 | + url: TARGET_URL, |
237 | - } | 310 | + headers: { |
238 | - | 311 | + 'Authorization': `Bearer ${TOKEN}` |
239 | - res.sendStatus(200); | 312 | + }, |
240 | - }); | 313 | + json: { |
241 | -}while(!end); | 314 | + "replyToken": replyToken, |
315 | + "messages": [ | ||
316 | + { | ||
317 | + "type": "text", | ||
318 | + "text": "Choose a number\n1. 매운 음식\n2. 안매운 음식" | ||
319 | + } | ||
320 | + ] | ||
321 | + } | ||
322 | + }, (error, response, body) => { | ||
323 | + console.log(body) | ||
324 | + }); | ||
325 | +} | ||
242 | 326 | ||
327 | +function sleep(ms) { | ||
328 | + const wakeUpTime = Date.now() + ms; | ||
329 | + while (Date.now() < wakeUpTime) {} | ||
330 | + } | ||
243 | 331 | ||
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