Showing
1 changed file
with
0 additions
and
87 deletions
I_to_D_chatbot/app.js
deleted
100644 → 0
| 1 | -var express = require('express'); | ||
| 2 | -const request = require('request'); | ||
| 3 | -const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | ||
| 4 | -const TOKEN = 'A3e2T6lFz02I+kWl6BydLTnWCtxcsvioAr6g4FJMgvZyRcxzy+EdJntPO09XKEHOF08Pgg+L9rNEFKEYrhdhRNg5bmiGZdiaTJfp1DaY/uTVMjRU+hRM00OwvaWq4mxYt/GYLEDzrsQ3O8ezd9WbqQdB04t89/1O/w1cDnyilFU=' | ||
| 5 | -const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' | ||
| 6 | -const PAPAGO_ID = '파파고 ID' | ||
| 7 | -const PAPAGO_SECRET = '파파고 Client Secret' | ||
| 8 | -const fs = require('fs'); | ||
| 9 | -const path = require('path'); | ||
| 10 | -const HTTPS = require('https'); | ||
| 11 | -const domain = "2019102197.osschatbot2022.ml" | ||
| 12 | -const sslport = 23023; | ||
| 13 | -const bodyParser = require('body-parser'); | ||
| 14 | -var app = express(); | ||
| 15 | -app.use(bodyParser.json()); | ||
| 16 | -app.post('/hook', function (req, res) { | ||
| 17 | - | ||
| 18 | - var eventObj = req.body.events[0]; | ||
| 19 | - var source = eventObj.source; | ||
| 20 | - var message = eventObj.message; | ||
| 21 | - | ||
| 22 | - // request log | ||
| 23 | - console.log('======================', new Date() ,'======================'); | ||
| 24 | - console.log('[request]', req.body); | ||
| 25 | - console.log('[request source] ', eventObj.source); | ||
| 26 | - console.log('[request message]', eventObj.message); | ||
| 27 | - | ||
| 28 | - trans(eventObj.replyToken, eventObj.message.text); | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - res.sendStatus(200); | ||
| 32 | -}); | ||
| 33 | - | ||
| 34 | -function trans(replyToken, message) { | ||
| 35 | - | ||
| 36 | - request.post( | ||
| 37 | - { | ||
| 38 | - url: PAPAGO_URL, | ||
| 39 | - headers: { | ||
| 40 | - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | ||
| 41 | - 'X-Naver-Client-Id': `${PAPAGO_ID}`, | ||
| 42 | - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
| 43 | - }, | ||
| 44 | - body: 'source=ko&target=en&text=' + message, | ||
| 45 | - json:true | ||
| 46 | - },(error, response, body) => { | ||
| 47 | - if(!error && response.statusCode == 200) { | ||
| 48 | - console.log(body.message); | ||
| 49 | - var transMessage = body.message.result.translatedText; | ||
| 50 | - request.post( | ||
| 51 | - { | ||
| 52 | - url: TARGET_URL, | ||
| 53 | - headers: { | ||
| 54 | - 'Authorization': `Bearer ${TOKEN}` | ||
| 55 | - }, | ||
| 56 | - json: { | ||
| 57 | - "replyToken":replyToken, | ||
| 58 | - "messages":[ | ||
| 59 | - { | ||
| 60 | - "type":"text", | ||
| 61 | - "text":transMessage | ||
| 62 | - } | ||
| 63 | - ] | ||
| 64 | - } | ||
| 65 | - },(error, response, body) => { | ||
| 66 | - console.log(body) | ||
| 67 | - }); | ||
| 68 | - } | ||
| 69 | - }); | ||
| 70 | - | ||
| 71 | -} | ||
| 72 | - | ||
| 73 | -try { | ||
| 74 | - const option = { | ||
| 75 | - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
| 76 | - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
| 77 | - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
| 78 | - }; | ||
| 79 | - | ||
| 80 | - HTTPS.createServer(option, app).listen(sslport, () => { | ||
| 81 | - console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
| 82 | - }); | ||
| 83 | - } catch (error) { | ||
| 84 | - console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
| 85 | - console.log(error); | ||
| 86 | - } | ||
| 87 | - | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment