Showing
4 changed files
with
103 additions
and
13 deletions
... | @@ -6,22 +6,33 @@ mother project : kakaoBot<br> | ... | @@ -6,22 +6,33 @@ mother project : kakaoBot<br> |
6 | 6 | ||
7 | # :page_with_curl:목차 | 7 | # :page_with_curl:목차 |
8 | --- | 8 | --- |
9 | -> 1.소개 | 9 | +> 소개 |
10 | - | 10 | + mother project chatbot를 바탕으로 aws-comprehend api기능을 추가하여 |
11 | -> 2.기본세팅 | 11 | + 어떤 문자를 보냈을때 그 메세지에 내포된 감정을 반환하도록 하였다. |
12 | - | 12 | +> 기본세팅 |
13 | + WHAT WE NEED: | ||
14 | + line messanger api channel token | ||
15 | + aws access,access-secret token | ||
13 | > > 개발 환경 | 16 | > > 개발 환경 |
14 | - | 17 | + visual studio code |
18 | + node js | ||
15 | > > 사용 언어 | 19 | > > 사용 언어 |
16 | - | 20 | + node.js |
17 | > 3.빌드 방법 | 21 | > 3.빌드 방법 |
18 | - | 22 | + 0. npm install |
23 | + 1. npm install aws-sdk | ||
24 | + 2. /home/user 디렉토리의 .aws안의 credentials파일을 수정한다 | ||
25 | + ->aws access key, secret key 입력 | ||
26 | + ->region: us-east-1 | ||
27 | + ->aws session token입력 | ||
19 | > 4.사용 방법 | 28 | > 4.사용 방법 |
20 | - | 29 | + (temp) |
30 | + 1. send message | ||
31 | + 2. it will return your sentiment | ||
21 | > > 사용 API | 32 | > > 사용 API |
22 | - | 33 | + 1. aws comprehend api |
34 | + 2. line notify,messendger api | ||
23 | > liscense | 35 | > liscense |
24 | - | ||
25 | # :speaker:소개 | 36 | # :speaker:소개 |
26 | --- | 37 | --- |
27 | 38 | ... | ... |
... | @@ -2,7 +2,6 @@ var express = require('express'); | ... | @@ -2,7 +2,6 @@ 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 | 4 | ||
5 | - | ||
6 | //papago api | 5 | //papago api |
7 | var request = require('request'); | 6 | var request = require('request'); |
8 | 7 | ||
... | @@ -67,7 +66,6 @@ function handleEvent(event) { | ... | @@ -67,7 +66,6 @@ function handleEvent(event) { |
67 | //언어 감지가 제대로 됐는지 확인 | 66 | //언어 감지가 제대로 됐는지 확인 |
68 | console.log(detect_body.langCode); | 67 | console.log(detect_body.langCode); |
69 | 68 | ||
70 | - | ||
71 | //번역은 한국어->영어 / 영어->한국어만 지원 | 69 | //번역은 한국어->영어 / 영어->한국어만 지원 |
72 | if(detect_body.langCode == 'ko'||detect_body.langCode == 'en'){ | 70 | if(detect_body.langCode == 'ko'||detect_body.langCode == 'en'){ |
73 | source = detect_body.langCode == 'ko' ? 'ko':'en'; | 71 | source = detect_body.langCode == 'ko' ? 'ko':'en'; | ... | ... |
app_reply.js
0 → 100644
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 = '2COGs98oPL/IUlCY23t4YMh98apwu0w2wlCWMjTvpNpHZwJoMWpYJP2/S00b/bwCKRTK0D9zgLbQq0gLokge0cXHb5gCJ6UQBTV4KHiynPN/WKydHNxqAwGlzvN+YUJoP9VG/crGAfy5xaU7k2EANAdB04t89/1O/w1cDnyilFU=' | ||
5 | +const fs = require('fs'); | ||
6 | +const path = require('path'); | ||
7 | +const HTTPS = require('https'); | ||
8 | +const domain = "www.chatbot-test.ml" | ||
9 | +const sslport = 23023; | ||
10 | +const bodyParser = require('body-parser'); | ||
11 | +var app = express(); | ||
12 | +app.use(bodyParser.json()); | ||
13 | +app.post('/hook', function (req, res) { | ||
14 | + | ||
15 | + var eventObj = req.body.events[0]; | ||
16 | + var source = eventObj.source; | ||
17 | + var message = eventObj.message; | ||
18 | + | ||
19 | + // request log | ||
20 | + console.log('======================', new Date() ,'======================'); | ||
21 | + console.log('[request]', req.body); | ||
22 | + console.log('[request source] ', eventObj.source); | ||
23 | + console.log('[request message]', eventObj.message['text']); | ||
24 | + | ||
25 | + | ||
26 | + | ||
27 | + var AWS = require("aws-sdk"); | ||
28 | + AWS.config.update({region:'us-east-1'}); | ||
29 | + var params={LanguageCode:'en',TextList:[eventObj.message['text']]}; | ||
30 | + var comprehend = new AWS.Comprehend(); | ||
31 | + comprehend.batchDetectSentiment(params, function (err, data) { | ||
32 | + if (err) console.log(err, err.stack); // an error occurred | ||
33 | + else {console.log(data.ResultList[0]['Sentiment']); | ||
34 | + send(eventObj.replyToken,data.ResultList[0]['Sentiment']) | ||
35 | + } // successful response | ||
36 | + }); | ||
37 | + | ||
38 | + res.sendStatus(200); | ||
39 | +}); | ||
40 | + | ||
41 | +function send(replyToken,result){ | ||
42 | + request.post( | ||
43 | + { | ||
44 | + url: TARGET_URL, | ||
45 | + headers: { | ||
46 | + 'Authorization': `Bearer ${TOKEN}` | ||
47 | + }, | ||
48 | + json: { | ||
49 | + "replyToken":replyToken, | ||
50 | + "messages":[ | ||
51 | + { | ||
52 | + "type":"text", | ||
53 | + "text":"what can I help you?" | ||
54 | + }, | ||
55 | + { | ||
56 | + "type":"text", | ||
57 | + "text":"you look"+result | ||
58 | + } | ||
59 | + ] | ||
60 | + } | ||
61 | + },(error, response, body) => { | ||
62 | + console.log(body) | ||
63 | + }); | ||
64 | +} | ||
65 | + | ||
66 | + | ||
67 | +try { | ||
68 | + const option = { | ||
69 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
70 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
71 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
72 | + }; | ||
73 | + | ||
74 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
75 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
76 | + }); | ||
77 | + } catch (error) { | ||
78 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
79 | + console.log(error); | ||
80 | + } | ||
81 | + |
1 | 0. npm install | 1 | 0. npm install |
2 | 1. npm install aws-sdk | 2 | 1. npm install aws-sdk |
3 | 2.aws configure | 3 | 2.aws configure |
4 | -->캡처2에 들어가있는 access key, secret key 입력 | 4 | +->aws access key, secret key 입력 |
5 | ->region은 us-east-1 | 5 | ->region은 us-east-1 |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment