Showing
1 changed file
with
139 additions
and
76 deletions
1 | -var lenguage = 'not'; | 1 | +var end = false; |
2 | +var count = 0; | ||
2 | var express = require('express'); | 3 | var express = require('express'); |
3 | const request = require('request'); | 4 | const request = require('request'); |
4 | -const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | 5 | +const TARGET_URL = 'https://api.line.me/v2/bot/message/push' |
5 | const TOKEN = 'Ur8nbk2bQAc9KavLSSJv0uzlKWCxCBnOJOKyqbRS1mMHWgJlEOPfb7v+X1DDEHocUmhU7KnPFN+KHt5S3SYd5QmF42aTeQpW2gDVTjyqty1Tn+p6QqEhI/uSz659PBHwczEYn0Plx+akfCNHDPJRpAdB04t89/1O/w1cDnyilFU=' | 6 | const TOKEN = 'Ur8nbk2bQAc9KavLSSJv0uzlKWCxCBnOJOKyqbRS1mMHWgJlEOPfb7v+X1DDEHocUmhU7KnPFN+KHt5S3SYd5QmF42aTeQpW2gDVTjyqty1Tn+p6QqEhI/uSz659PBHwczEYn0Plx+akfCNHDPJRpAdB04t89/1O/w1cDnyilFU=' |
6 | -const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' | 7 | +const USER_ID = 'Ub5fa0ab321e4713638a85fbfa7ecc20b' |
7 | -const PAPAGO_ID = '1Z83W3lUGLqnNbrDAk8A' | ||
8 | -const PAPAGO_SECRET = 'ST7ElRPFPP' | ||
9 | const fs = require('fs'); | 8 | const fs = require('fs'); |
10 | const path = require('path'); | 9 | const path = require('path'); |
11 | const HTTPS = require('https'); | 10 | const HTTPS = require('https'); |
12 | const domain = "2021105602.osschatbot2022.tk" | 11 | const domain = "2021105602.osschatbot2022.tk" |
13 | const sslport = 23023; | 12 | const sslport = 23023; |
14 | const bodyParser = require('body-parser'); | 13 | const bodyParser = require('body-parser'); |
15 | -const res = require('express/lib/response'); | 14 | +var choosen = []; |
16 | var app = express(); | 15 | var app = express(); |
17 | -do | 16 | +do{ |
18 | -{ | ||
19 | app.use(bodyParser.json()); | 17 | app.use(bodyParser.json()); |
20 | app.post('/hook', function (req, res) { | 18 | app.post('/hook', function (req, res) { |
21 | 19 | ||
... | @@ -29,53 +27,82 @@ do | ... | @@ -29,53 +27,82 @@ do |
29 | console.log('[request source] ', eventObj.source); | 27 | console.log('[request source] ', eventObj.source); |
30 | console.log('[request message]', eventObj.message); | 28 | console.log('[request message]', eventObj.message); |
31 | 29 | ||
32 | - if(eventObj.message.text=='영어') | 30 | + var msg = eventObj.message.text; |
31 | + if(eventObj.message.text == 'start') | ||
33 | { | 32 | { |
34 | - lenguage='en'; | 33 | + request.post( |
35 | - }else if(eventObj.message.text=='일본어') | ||
36 | { | 34 | { |
37 | - lenguage = 'ja' | 35 | + url: TARGET_URL, |
38 | - }else if(eventObj.message.text=='프랑스어') | 36 | + headers: { |
37 | + 'Authorization': `Bearer ${TOKEN}` | ||
38 | + }, | ||
39 | + json: { | ||
40 | + "replyToken":eventObj.replyToken, | ||
41 | + "messages":[ | ||
39 | { | 42 | { |
40 | - lenguage = 'fr' | 43 | + "type":"text", |
41 | - }else | 44 | + "text":"Hi\nThis is Menu recommend Chatbot!\nIf you follow the instructions, I will recommend the menu!" |
45 | + }, | ||
42 | { | 46 | { |
43 | - if(lenguage == 'en') | 47 | + "type":"text", |
48 | + "text":"Choose a number\n1. 한식\n2. 중식\n3. 일식\n4. 양식\n5. 기타" | ||
49 | + } | ||
50 | + ] | ||
51 | + } | ||
52 | + },(error, response, body) => { | ||
53 | + console.log(body) | ||
54 | + }); | ||
55 | + count++; | ||
56 | + }else if(count == 1 & !end) | ||
44 | { | 57 | { |
45 | - trans_en(eventObj.replyToken, eventObj.message.text); | 58 | + if(msg == '1' | msg == '2' | msg == '3' | msg == '4' | msg == '5') |
46 | - }else if(lenguage == 'ja') | ||
47 | { | 59 | { |
48 | - trans_ja(eventObj.replyToken, eventObj.message.text); | 60 | + choosen.push(msg); |
49 | - }else if(lenguage == 'fr') | 61 | + count++; |
62 | + request.post( | ||
50 | { | 63 | { |
51 | - trans_fr(eventObj.replyToken, eventObj.message.text); | 64 | + url: TARGET_URL, |
52 | - }else | 65 | + headers: { |
66 | + 'Authorization': `Bearer ${TOKEN}` | ||
67 | + }, | ||
68 | + json: { | ||
69 | + "replyToken":eventObj.replyToken, | ||
70 | + "messages":[ | ||
53 | { | 71 | { |
54 | - trans_en(eventObj.replyToken, '오류'); | 72 | + "type":"text", |
73 | + "text":"Choose a number\n1. 고기\n2. 해산물\n3. 기타" | ||
55 | } | 74 | } |
75 | + ] | ||
56 | } | 76 | } |
57 | - | 77 | + },(error, response, body) => { |
58 | - | 78 | + console.log(body) |
59 | - res.sendStatus(200); | ||
60 | }); | 79 | }); |
61 | -}while(lenguage=='en' | lenguage == 'fr' | lenguage == 'ja'); | 80 | + }else{ |
62 | - | ||
63 | -function trans_en(replyToken, message) { | ||
64 | - | ||
65 | request.post( | 81 | request.post( |
66 | { | 82 | { |
67 | - url: PAPAGO_URL, | 83 | + url: TARGET_URL, |
68 | headers: { | 84 | headers: { |
69 | - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | 85 | + 'Authorization': `Bearer ${TOKEN}` |
70 | - 'X-Naver-Client-Id': `${PAPAGO_ID}`, | ||
71 | - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
72 | }, | 86 | }, |
73 | - body: 'source=ko&target=en&text=' + message, | 87 | + json: { |
74 | - json:true | 88 | + "replyToken":eventObj.replyToken, |
89 | + "messages":[ | ||
90 | + { | ||
91 | + "type":"text", | ||
92 | + "text":"Please choose a number between 1 to 5!" | ||
93 | + } | ||
94 | + ] | ||
95 | + } | ||
75 | },(error, response, body) => { | 96 | },(error, response, body) => { |
76 | - if(!error && response.statusCode == 200) { | 97 | + console.log(body) |
77 | - console.log(body.message); | 98 | + }); |
78 | - var transMessage = body.message.result.translatedText; | 99 | + } |
100 | + }else if(count == 2 & !end) | ||
101 | + { | ||
102 | + if(msg == 1 | msg == 2 | msg==3) | ||
103 | + { | ||
104 | + choosen.push(msg); | ||
105 | + count++; | ||
79 | request.post( | 106 | request.post( |
80 | { | 107 | { |
81 | url: TARGET_URL, | 108 | url: TARGET_URL, |
... | @@ -83,38 +110,62 @@ function trans_en(replyToken, message) { | ... | @@ -83,38 +110,62 @@ function trans_en(replyToken, message) { |
83 | 'Authorization': `Bearer ${TOKEN}` | 110 | 'Authorization': `Bearer ${TOKEN}` |
84 | }, | 111 | }, |
85 | json: { | 112 | json: { |
86 | - "replyToken":replyToken, | 113 | + "replyToken":eventObj.replyToken, |
87 | "messages":[ | 114 | "messages":[ |
88 | { | 115 | { |
89 | "type":"text", | 116 | "type":"text", |
90 | - "text":transMessage | 117 | + "text":"Choose a number\n1. 뜨거운 음식\n2. 찬 음식" |
91 | } | 118 | } |
92 | ] | 119 | ] |
93 | } | 120 | } |
94 | },(error, response, body) => { | 121 | },(error, response, body) => { |
95 | console.log(body) | 122 | console.log(body) |
96 | }); | 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 | + ] | ||
97 | } | 139 | } |
140 | + },(error, response, body) => { | ||
141 | + console.log(body) | ||
98 | }); | 142 | }); |
99 | - | 143 | + } |
100 | -} | 144 | + }else if(count == 3 & !end) |
101 | - | 145 | + { |
102 | -function trans_ja(replyToken, message) { | 146 | + if(msg == 1 | msg == 2) |
103 | - | 147 | + { |
148 | + choosen.push(msg); | ||
149 | + count++; | ||
104 | request.post( | 150 | request.post( |
105 | { | 151 | { |
106 | - url: PAPAGO_URL, | 152 | + url: TARGET_URL, |
107 | headers: { | 153 | headers: { |
108 | - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | 154 | + 'Authorization': `Bearer ${TOKEN}` |
109 | - 'X-Naver-Client-Id': `${PAPAGO_ID}`, | ||
110 | - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
111 | }, | 155 | }, |
112 | - body: 'source=ko&target=ja&text=' + message, | 156 | + json: { |
113 | - json:true | 157 | + "replyToken":eventObj.replyToken, |
158 | + "messages":[ | ||
159 | + { | ||
160 | + "type":"text", | ||
161 | + "text":"Choose a number\n1. 국물\n2. 국물 없는 음식" | ||
162 | + } | ||
163 | + ] | ||
164 | + } | ||
114 | },(error, response, body) => { | 165 | },(error, response, body) => { |
115 | - if(!error && response.statusCode == 200) { | 166 | + console.log(body) |
116 | - console.log(body.message); | 167 | + }); |
117 | - var transMessage = body.message.result.translatedText; | 168 | + }else{ |
118 | request.post( | 169 | request.post( |
119 | { | 170 | { |
120 | url: TARGET_URL, | 171 | url: TARGET_URL, |
... | @@ -122,11 +173,11 @@ function trans_ja(replyToken, message) { | ... | @@ -122,11 +173,11 @@ function trans_ja(replyToken, message) { |
122 | 'Authorization': `Bearer ${TOKEN}` | 173 | 'Authorization': `Bearer ${TOKEN}` |
123 | }, | 174 | }, |
124 | json: { | 175 | json: { |
125 | - "replyToken":replyToken, | 176 | + "replyToken":eventObj.replyToken, |
126 | "messages":[ | 177 | "messages":[ |
127 | { | 178 | { |
128 | "type":"text", | 179 | "type":"text", |
129 | - "text":transMessage | 180 | + "text":"Please choose a number between 1 to 2!" |
130 | } | 181 | } |
131 | ] | 182 | ] |
132 | } | 183 | } |
... | @@ -134,26 +185,31 @@ function trans_ja(replyToken, message) { | ... | @@ -134,26 +185,31 @@ function trans_ja(replyToken, message) { |
134 | console.log(body) | 185 | console.log(body) |
135 | }); | 186 | }); |
136 | } | 187 | } |
137 | - }); | 188 | + }else if(count == 4 & !end) |
138 | - | 189 | + { |
139 | -} | 190 | + if(msg == 1 | msg == 2) |
140 | - | 191 | + { |
141 | -function trans_fr(replyToken, message) { | 192 | + choosen.push(msg); |
142 | - | 193 | + count++; |
143 | request.post( | 194 | request.post( |
144 | { | 195 | { |
145 | - url: PAPAGO_URL, | 196 | + url: TARGET_URL, |
146 | headers: { | 197 | headers: { |
147 | - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | 198 | + 'Authorization': `Bearer ${TOKEN}` |
148 | - 'X-Naver-Client-Id': `${PAPAGO_ID}`, | ||
149 | - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
150 | }, | 199 | }, |
151 | - body: 'source=ko&target=fr&text=' + message, | 200 | + json: { |
152 | - json:true | 201 | + "replyToken":eventObj.replyToken, |
202 | + "messages":[ | ||
203 | + { | ||
204 | + "type":"text", | ||
205 | + "text":"Choose a number\n1. 매운 음식\n2. 안매운 음식" | ||
206 | + } | ||
207 | + ] | ||
208 | + } | ||
153 | },(error, response, body) => { | 209 | },(error, response, body) => { |
154 | - if(!error && response.statusCode == 200) { | 210 | + console.log(body) |
155 | - console.log(body.message); | 211 | + }); |
156 | - var transMessage = body.message.result.translatedText; | 212 | + }else{ |
157 | request.post( | 213 | request.post( |
158 | { | 214 | { |
159 | url: TARGET_URL, | 215 | url: TARGET_URL, |
... | @@ -161,11 +217,11 @@ function trans_fr(replyToken, message) { | ... | @@ -161,11 +217,11 @@ function trans_fr(replyToken, message) { |
161 | 'Authorization': `Bearer ${TOKEN}` | 217 | 'Authorization': `Bearer ${TOKEN}` |
162 | }, | 218 | }, |
163 | json: { | 219 | json: { |
164 | - "replyToken":replyToken, | 220 | + "replyToken":eventObj.replyToken, |
165 | "messages":[ | 221 | "messages":[ |
166 | { | 222 | { |
167 | "type":"text", | 223 | "type":"text", |
168 | - "text":transMessage | 224 | + "text":"Please choose a number between 1 to 2!" |
169 | } | 225 | } |
170 | ] | 226 | ] |
171 | } | 227 | } |
... | @@ -173,9 +229,16 @@ function trans_fr(replyToken, message) { | ... | @@ -173,9 +229,16 @@ function trans_fr(replyToken, message) { |
173 | console.log(body) | 229 | console.log(body) |
174 | }); | 230 | }); |
175 | } | 231 | } |
176 | - }); | 232 | + } |
233 | + | ||
234 | + if(count == 5) | ||
235 | + { | ||
236 | + end == true; | ||
237 | + } | ||
177 | 238 | ||
178 | -} | 239 | + res.sendStatus(200); |
240 | + }); | ||
241 | +}while(!end); | ||
179 | 242 | ||
180 | 243 | ||
181 | try { | 244 | try { | ... | ... |
-
Please register or login to post a comment