김대욱

new

Showing 1 changed file with 112 additions and 124 deletions
...@@ -17,142 +17,130 @@ app.post('/hook', function (req, res) { ...@@ -17,142 +17,130 @@ app.post('/hook', function (req, res) {
17 17
18 var eventObj = req.body.events[0]; 18 var eventObj = req.body.events[0];
19 var source = eventObj.source; 19 var source = eventObj.source;
20 + var kind = eventObj.kind;
20 var message = eventObj.message; 21 var message = eventObj.message;
21 22
22 // request log 23 // request log
23 console.log('======================', new Date() ,'======================'); 24 console.log('======================', new Date() ,'======================');
24 console.log('[request]', req.body); 25 console.log('[request]', req.body);
25 console.log('[request source] ', eventObj.source); 26 console.log('[request source] ', eventObj.source);
27 + console.log('[request kind] ', eventObj.kind);
26 console.log('[request message]', eventObj.message); 28 console.log('[request message]', eventObj.message);
27 - if(message == "영어"){
28 - transen(eventObj.replyToken, eventObj.message.text);
29 - }
30 - else if(message == "일본어"){
31 - transja(eventObj.replyToken, eventObj.message.text);
32 - }
33 - else if(message == "프랑스어"){
34 - transfr(eventObj.replyToken, eventObj.message.text);
35 - }
36 -
37 -
38 29
30 + trans(eventObj.replyToken, eventObj.kind, eventObj.message.text);
39 31
40 res.sendStatus(200); 32 res.sendStatus(200);
41 }); 33 });
42 34
43 -function transen(replyToken, message) { 35 +function trans(replyToken, kind, message) {
44 - 36 + if(kind == "영어"){
45 - request.post( 37 + request.post(
46 - { 38 + {
47 - url: PAPAGO_URL, 39 + url: PAPAGO_URL,
48 - headers: { 40 + headers: {
49 - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 41 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
50 - 'X-Naver-Client-Id': `${PAPAGO_ID}`, 42 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
51 - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` 43 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
52 - }, 44 + },
53 - body: 'source=ko&target=en&text=' + message, 45 + body: 'source=ko&target=en&text=' + message,
54 - json:true 46 + json:true
55 - },(error, response, body) => { 47 + },(error, response, body) => {
56 - if(!error && response.statusCode == 200) { 48 + if(!error && response.statusCode == 200) {
57 - console.log(body.message); 49 + console.log(body.message);
58 - var transMessage = body.message.result.translatedText; 50 + var transMessage = body.message.result.translatedText;
59 - request.post( 51 + request.post(
60 - { 52 + {
61 - url: TARGET_URL, 53 + url: TARGET_URL,
62 - headers: { 54 + headers: {
63 - 'Authorization': `Bearer ${TOKEN}` 55 + 'Authorization': `Bearer ${TOKEN}`
64 - }, 56 + },
65 - json: { 57 + json: {
66 - "replyToken":replyToken, 58 + "replyToken":replyToken,
67 - "messages":[ 59 + "messages":[
68 - { 60 + {
69 - "type":"text", 61 + "type":"text",
70 - "text":transMessage 62 + "text":transMessage
71 - } 63 + }
72 - ] 64 + ]
73 - } 65 + }
74 - },(error, response, body) => { 66 + },(error, response, body) => {
75 - console.log(body) 67 + console.log(body)
76 - }); 68 + });
77 - } 69 + }
78 - }); 70 + });
79 - 71 + }
80 -} 72 + else if(kind == "일본어"){
81 -function transfr(replyToken, message) { 73 + request.post(
82 - 74 + {
83 - request.post( 75 + url: PAPAGO_URL,
84 - { 76 + headers: {
85 - url: PAPAGO_URL, 77 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
86 - headers: { 78 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
87 - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 79 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
88 - 'X-Naver-Client-Id': `${PAPAGO_ID}`, 80 + },
89 - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` 81 + body: 'source=ko&target=ja&text=' + message,
90 - }, 82 + json:true
91 - body: 'source=ko&target=fr&text=' + message, 83 + },(error, response, body) => {
92 - json:true 84 + if(!error && response.statusCode == 200) {
93 - },(error, response, body) => { 85 + console.log(body.message);
94 - if(!error && response.statusCode == 200) { 86 + var transMessage = body.message.result.translatedText;
95 - console.log(body.message); 87 + request.post(
96 - var transMessage = body.message.result.translatedText; 88 + {
97 - request.post( 89 + url: TARGET_URL,
98 - { 90 + headers: {
99 - url: TARGET_URL, 91 + 'Authorization': `Bearer ${TOKEN}`
100 - headers: { 92 + },
101 - 'Authorization': `Bearer ${TOKEN}` 93 + json: {
102 - }, 94 + "replyToken":replyToken,
103 - json: { 95 + "messages":[
104 - "replyToken":replyToken, 96 + {
105 - "messages":[ 97 + "type":"text",
106 - { 98 + "text":transMessage
107 - "type":"text", 99 + }
108 - "text":transMessage 100 + ]
109 - } 101 + }
110 - ] 102 + },(error, response, body) => {
111 - } 103 + console.log(body)
112 - },(error, response, body) => { 104 + });
113 - console.log(body) 105 + }
114 - }); 106 + });
115 - } 107 + }
116 - }); 108 + else if(kind == "프랑스어"){
117 - 109 + request.post(
118 -} 110 + {
119 -function transja(replyToken, message) { 111 + url: PAPAGO_URL,
120 - 112 + headers: {
121 - request.post( 113 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
122 - { 114 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
123 - url: PAPAGO_URL, 115 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
124 - headers: { 116 + },
125 - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 117 + body: 'source=ko&target=fr&text=' + message,
126 - 'X-Naver-Client-Id': `${PAPAGO_ID}`, 118 + json:true
127 - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` 119 + },(error, response, body) => {
128 - }, 120 + if(!error && response.statusCode == 200) {
129 - body: 'source=ko&target=ja&text=' + message, 121 + console.log(body.message);
130 - json:true 122 + var transMessage = body.message.result.translatedText;
131 - },(error, response, body) => { 123 + request.post(
132 - if(!error && response.statusCode == 200) { 124 + {
133 - console.log(body.message); 125 + url: TARGET_URL,
134 - var transMessage = body.message.result.translatedText; 126 + headers: {
135 - request.post( 127 + 'Authorization': `Bearer ${TOKEN}`
136 - { 128 + },
137 - url: TARGET_URL, 129 + json: {
138 - headers: { 130 + "replyToken":replyToken,
139 - 'Authorization': `Bearer ${TOKEN}` 131 + "messages":[
140 - }, 132 + {
141 - json: { 133 + "type":"text",
142 - "replyToken":replyToken, 134 + "text":transMessage
143 - "messages":[ 135 + }
144 - { 136 + ]
145 - "type":"text", 137 + }
146 - "text":transMessage 138 + },(error, response, body) => {
147 - } 139 + console.log(body)
148 - ] 140 + });
149 - } 141 + }
150 - },(error, response, body) => { 142 + });
151 - console.log(body) 143 + }
152 - });
153 - }
154 - });
155 -
156 } 144 }
157 145
158 try { 146 try {
......