은승우

all

Showing 1 changed file with 40 additions and 32 deletions
1 -var express = require('express'); 1 +var express = require("express");
2 var app = express(); 2 var app = express();
3 const line = require('@line/bot-sdk'); 3 const line = require('@line/bot-sdk');
4 +var request = require('request');
4 var https=require('https'); 5 var https=require('https');
5 -const fs = require('fs'); 6 +var http=require('http');
6 - 7 +const lex = require('greenlock-express').create({
7 -var key = fs.readFileSync('/etc/letsencrypt/live/oss.chatbot.bu.to/privkey.pem'); 8 + version: 'draft-11', // 버전2
8 -var cert = fs.readFileSync('/etc/letsencrypt/live/oss.chatbot.bu.to/cert.pem'); 9 + store: require('greenlock-store-fs'),
9 -var ca = fs.readFileSync('/etc/letsencrypt/live/oss.chatbot.bu.to/fullchain.pem'); 10 + configDir: '/etc/letsencrypt', // 또는 ~/letsencrypt/etc
10 - 11 + approveDomains: (opts, certs, cb) => {
11 -https.createServer({ 12 + if (certs) {
12 -key: key, 13 + opts.domains = ['oss.chatbot.bu.to', 'oss.chatbot.bu.to'];
13 -cert: cert, 14 + } else {
14 -ca: ca 15 + opts.email = 'sweun1@naver.com';
15 -}, app).listen(80,()=>{ 16 + opts.agreeTos = true;
16 - console.log('server on 80'); 17 + }
17 -}); 18 + cb(null, { options: opts, certs });
18 19
19 -https.createServer({ 20 + },
20 - key: key, 21 + renewWithin: 81 * 24 * 60 * 60 * 1000,
21 - cert: cert, 22 + renewBy: 80 * 24 * 60 * 60 * 1000,
22 - ca: ca 23 +});//papago api
23 - }, app).listen(443,()=>{
24 - console.log('server on 443');
25 - });
26 24
27 -//papago api
28 -var request = require('request');
29 25
26 +https.createServer(lex.httpsOptions, lex.middleware(app)).listen((process.env.SSL_PORT || 443),()=>{
27 + console.log("server on 443");
28 +});
29 +http.createServer(lex.middleware(require('redirect-https')())).listen(process.env.PORT || 80,()=>{
30 + console.log("server on 80");
31 +});
30 //번역 api_url 32 //번역 api_url
31 var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt'; 33 var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt';
32 34
...@@ -44,7 +46,6 @@ const config = { ...@@ -44,7 +46,6 @@ const config = {
44 46
45 }; 47 };
46 48
47 -
48 // create LINE SDK client 49 // create LINE SDK client
49 const client = new line.Client(config); 50 const client = new line.Client(config);
50 // create Express app 51 // create Express app
...@@ -52,15 +53,20 @@ const client = new line.Client(config); ...@@ -52,15 +53,20 @@ const client = new line.Client(config);
52 53
53 // register a webhook handler with middleware 54 // register a webhook handler with middleware
54 // about the middleware, please refer to doc 55 // about the middleware, please refer to doc
56 +
57 +
55 app.post('/webhook', line.middleware(config), (req, res) => { 58 app.post('/webhook', line.middleware(config), (req, res) => {
56 - console.log("webhook"); 59 +console.log(res.statusCode);
57 Promise 60 Promise
58 .all(req.body.events.map(handleEvent)) 61 .all(req.body.events.map(handleEvent))
59 .then((result) => res.json(result)) 62 .then((result) => res.json(result))
63 + .catch((err)=>{console.log(err);
64 + console.log(err.originalError.response)
65 + })
60 }); 66 });
61 // event handler 67 // event handler
62 function handleEvent(event) { 68 function handleEvent(event) {
63 - console.log("handleevent"); 69 + console.log(event.message);
64 if (event.type !== 'message' || event.message.type !== 'text') { 70 if (event.type !== 'message' || event.message.type !== 'text') {
65 // ignore non-text-message event 71 // ignore non-text-message event
66 return Promise.resolve(null); 72 return Promise.resolve(null);
...@@ -72,11 +78,10 @@ function handleEvent(event) { ...@@ -72,11 +78,10 @@ function handleEvent(event) {
72 form : {'query': event.message.text}, 78 form : {'query': event.message.text},
73 headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} 79 headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
74 }; 80 };
75 - console.log("1");
76 //papago 언어 감지 81 //papago 언어 감지
77 - request.post(detect_options,function(error,response,body){ 82 + request.post(detect_options,async (error,response,body)=>{
78 - console.log(response.statusCode);
79 if(!error && response.statusCode == 200){ 83 if(!error && response.statusCode == 200){
84 + console.log(response.body);
80 var detect_body = JSON.parse(response.body); 85 var detect_body = JSON.parse(response.body);
81 var source = ''; 86 var source = '';
82 var target = ''; 87 var target = '';
...@@ -95,11 +100,11 @@ function handleEvent(event) { ...@@ -95,11 +100,11 @@ function handleEvent(event) {
95 url: translate_api_url, 100 url: translate_api_url,
96 // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text) 101 // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
97 form: {'source':source, 'target':target, 'text':event.message.text}, 102 form: {'source':source, 'target':target, 'text':event.message.text},
98 - headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} 103 + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret,"Content-Type": "application/x-www-form-urlencoded"}
99 }; 104 };
100 105
101 // Naver Post API 106 // Naver Post API
102 - request.post(options, function(error, response, body){ 107 + await request.post(options, function(error, response, body){
103 // Translate API Sucess 108 // Translate API Sucess
104 if(!error && response.statusCode == 200){ 109 if(!error && response.statusCode == 200){
105 // JSON 110 // JSON
...@@ -107,7 +112,7 @@ function handleEvent(event) { ...@@ -107,7 +112,7 @@ function handleEvent(event) {
107 // Message 잘 찍히는지 확인 112 // Message 잘 찍히는지 확인
108 113
109 result.text = objBody.message.result.translatedText; 114 result.text = objBody.message.result.translatedText;
110 - console.log(result.text); 115 + console.log(result);
111 //번역된 문장 보내기 116 //번역된 문장 보내기
112 client.replyMessage(event.replyToken,result).then(resolve).catch(reject); 117 client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
113 } 118 }
...@@ -120,6 +125,9 @@ function handleEvent(event) { ...@@ -120,6 +125,9 @@ function handleEvent(event) {
120 } 125 }
121 126
122 } 127 }
128 + else{
129 + console.log("status code is not 200");
130 + }
123 131
124 }); 132 });
125 133
......