taegwon

fixed app.js

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,101 @@ do ...@@ -29,53 +27,101 @@ 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=='일본어') 34 + {
35 + url: TARGET_URL,
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 + {
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)
36 { 57 {
37 - lenguage = 'ja' 58 + if(msg == '1' | msg == '2' | msg == '3' | msg == '4' | msg == '5')
38 - }else if(eventObj.message.text=='프랑스어')
39 - {
40 - lenguage = 'fr'
41 - }else
42 - {
43 - if(lenguage == 'en')
44 - {
45 - trans_en(eventObj.replyToken, eventObj.message.text);
46 - }else if(lenguage == 'ja')
47 - {
48 - trans_ja(eventObj.replyToken, eventObj.message.text);
49 - }else if(lenguage == 'fr')
50 { 59 {
51 - trans_fr(eventObj.replyToken, eventObj.message.text); 60 + choosen.push(msg);
52 - }else 61 + count++;
53 - { 62 + request.post(
54 - trans_en(eventObj.replyToken, '오류'); 63 + {
64 + url: TARGET_URL,
65 + headers: {
66 + 'Authorization': `Bearer ${TOKEN}`
67 + },
68 + json: {
69 + "replyToken":eventObj.replyToken,
70 + "messages":[
71 + {
72 + "type":"text",
73 + "text":"Choose a number\n1. 고기\n2. 해산물\n3. 기타"
74 + }
75 + ]
76 + }
77 + },(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)
98 + });
55 } 99 }
56 - } 100 + }else if(count == 2 & !end)
57 -
58 -
59 - res.sendStatus(200);
60 - });
61 -}while(lenguage=='en' | lenguage == 'fr' | lenguage == 'ja');
62 -
63 -function trans_en(replyToken, message) {
64 -
65 - request.post(
66 { 101 {
67 - url: PAPAGO_URL, 102 + if(msg == 1 | msg == 2 | msg==3)
68 - headers: { 103 + {
69 - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 104 + choosen.push(msg);
70 - 'X-Naver-Client-Id': `${PAPAGO_ID}`, 105 + count++;
71 - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` 106 + request.post(
72 - }, 107 + {
73 - body: 'source=ko&target=en&text=' + message, 108 + url: TARGET_URL,
74 - json:true 109 + headers: {
75 - },(error, response, body) => { 110 + 'Authorization': `Bearer ${TOKEN}`
76 - if(!error && response.statusCode == 200) { 111 + },
77 - console.log(body.message); 112 + json: {
78 - var transMessage = body.message.result.translatedText; 113 + "replyToken":eventObj.replyToken,
114 + "messages":[
115 + {
116 + "type":"text",
117 + "text":"Choose a number\n1. 뜨거운 음식\n2. 찬 음식"
118 + }
119 + ]
120 + }
121 + },(error, response, body) => {
122 + console.log(body)
123 + });
124 + }else{
79 request.post( 125 request.post(
80 { 126 {
81 url: TARGET_URL, 127 url: TARGET_URL,
...@@ -83,11 +129,11 @@ function trans_en(replyToken, message) { ...@@ -83,11 +129,11 @@ function trans_en(replyToken, message) {
83 'Authorization': `Bearer ${TOKEN}` 129 'Authorization': `Bearer ${TOKEN}`
84 }, 130 },
85 json: { 131 json: {
86 - "replyToken":replyToken, 132 + "replyToken":eventObj.replyToken,
87 "messages":[ 133 "messages":[
88 { 134 {
89 "type":"text", 135 "type":"text",
90 - "text":transMessage 136 + "text":"Please choose a number between 1 to 3!"
91 } 137 }
92 ] 138 ]
93 } 139 }
...@@ -95,26 +141,31 @@ function trans_en(replyToken, message) { ...@@ -95,26 +141,31 @@ function trans_en(replyToken, message) {
95 console.log(body) 141 console.log(body)
96 }); 142 });
97 } 143 }
98 - }); 144 + }else if(count == 3 & !end)
99 -
100 -}
101 -
102 -function trans_ja(replyToken, message) {
103 -
104 - request.post(
105 { 145 {
106 - url: PAPAGO_URL, 146 + if(msg == 1 | msg == 2)
107 - headers: { 147 + {
108 - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 148 + choosen.push(msg);
109 - 'X-Naver-Client-Id': `${PAPAGO_ID}`, 149 + count++;
110 - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` 150 + request.post(
111 - }, 151 + {
112 - body: 'source=ko&target=ja&text=' + message, 152 + url: TARGET_URL,
113 - json:true 153 + headers: {
114 - },(error, response, body) => { 154 + 'Authorization': `Bearer ${TOKEN}`
115 - if(!error && response.statusCode == 200) { 155 + },
116 - console.log(body.message); 156 + json: {
117 - var transMessage = body.message.result.translatedText; 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{
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 -
139 -}
140 -
141 -function trans_fr(replyToken, message) {
142 -
143 - request.post(
144 { 189 {
145 - url: PAPAGO_URL, 190 + if(msg == 1 | msg == 2)
146 - headers: { 191 + {
147 - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 192 + choosen.push(msg);
148 - 'X-Naver-Client-Id': `${PAPAGO_ID}`, 193 + count++;
149 - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` 194 + request.post(
150 - }, 195 + {
151 - body: 'source=ko&target=fr&text=' + message, 196 + url: TARGET_URL,
152 - json:true 197 + headers: {
153 - },(error, response, body) => { 198 + 'Authorization': `Bearer ${TOKEN}`
154 - if(!error && response.statusCode == 200) { 199 + },
155 - console.log(body.message); 200 + json: {
156 - var transMessage = body.message.result.translatedText; 201 + "replyToken":eventObj.replyToken,
202 + "messages":[
203 + {
204 + "type":"text",
205 + "text":"Choose a number\n1. 매운 음식\n2. 안매운 음식"
206 + }
207 + ]
208 + }
209 + },(error, response, body) => {
210 + console.log(body)
211 + });
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 + }
177 233
178 -} 234 + if(count == 5)
235 + {
236 + end == true;
237 + }
238 +
239 + res.sendStatus(200);
240 + });
241 +}while(!end);
179 242
180 243
181 try { 244 try {
...@@ -192,4 +255,4 @@ try { ...@@ -192,4 +255,4 @@ try {
192 console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); 255 console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
193 console.log(error); 256 console.log(error);
194 } 257 }
195 -
...\ No newline at end of file ...\ No newline at end of file
258 +
...\ No newline at end of file ...\ No newline at end of file
......