EC2 Default User

Modify replied word

Showing 1 changed file with 46 additions and 95 deletions
...@@ -8,6 +8,10 @@ const HTTPS = require('https'); ...@@ -8,6 +8,10 @@ const HTTPS = require('https');
8 const domain = "2020105659.osschatbot.cf" 8 const domain = "2020105659.osschatbot.cf"
9 const sslport = 23023; 9 const sslport = 23023;
10 10
11 +//Open API
12 +const STDICT_URL = 'https://stdict.korean.go.kr/api/search.do?'
13 +const STDICT_KEY = '14AA284251F616FF2151DBE4CD7B3D4C'
14 +
11 //Webhook 15 //Webhook
12 const bodyParser = require('body-parser'); 16 const bodyParser = require('body-parser');
13 var app = express(); 17 var app = express();
...@@ -21,107 +25,55 @@ app.post('/hook', function (req, res) { ...@@ -21,107 +25,55 @@ app.post('/hook', function (req, res) {
21 // request log 25 // request log
22 console.log('======================', new Date() ,'======================'); 26 console.log('======================', new Date() ,'======================');
23 console.log('[request]', req.body); 27 console.log('[request]', req.body);
24 - console.log('[request source] ', eventObj.source); 28 + console.log('[request source] ', source);
25 - console.log('[request message]', eventObj.message); 29 + console.log('[request message]', message);
30 +
31 + wordchain(eventObj.replyToken, message.text);
26 32
27 - //Reply
28 - request.post(
29 - {
30 - url: TARGET_URL,
31 - headers: {
32 - 'Authorization': `Bearer ${TOKEN}`
33 - },
34 - json: {
35 - "replyToken":eventObj.replyToken,
36 - "messages":[
37 - {
38 - "type":"text",
39 - "text":"안녕하세요"
40 - },
41 - {
42 - "type":"text",
43 - "text":"끝말잇기봇입니다"
44 - }
45 - ]
46 - }
47 - },(error, response, body) => {
48 - console.log(body)
49 - });
50 33
51 res.sendStatus(200); 34 res.sendStatus(200);
52 }); 35 });
53 36
54 -//SSL 37 +function wordchain(replyToken, message) {
55 -try { 38 + let letter = message.slice(-1);
56 - const option = {
57 - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
58 - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
59 - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
60 - };
61 -
62 - HTTPS.createServer(option, app).listen(sslport, () => {
63 - console.log(`[HTTPS] Server is started on port ${sslport}`);
64 - });
65 - } catch (error) {
66 - console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
67 - console.log(error);
68 - }
69 -
70 -
71 39
72 -/* 40 + request.post(
73 -var express = require('express'); 41 + {
74 -const request = require('request'); 42 + url: STDICT_URL,
75 -const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 43 + headers: {
76 -const TOKEN = 'P5+e9JkPRckzKu1Fv7ghZl1MfP0wrYwYcgBVzHWe1OHO4Gtv1PIa9BARYqxoAThaXPSf8MyVKcpDE88V5jOnoS4o2xCNcgH9Ne4wcrPuNX6AT4gsfYEz0Y7n/A4+rT4vXHYARK34YPbkzzQbiXiyiQdB04t89/1O/w1cDnyilFU=' 44 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
77 -const fs = require('fs'); 45 + 'Stdict-Key': `${STDICT_KEY}`
78 -const path = require('path'); 46 + },
79 -const HTTPS = require('https'); 47 + body: 'https://stdict.korean.go.kr/api/search.do?' + `key=${STDICT_KEY}` + `q=${letter}` + 'req_type=json' + 'advanced=y' + 'method=start',
80 -const domain = "2020105659.osschatbot.cf" 48 + json:true
81 -const sslport = 23023; 49 + },(error, response, body) => {
82 - 50 + if(!error && response.statusCode == 200) {
83 -const bodyParser = require('body-parser'); 51 + console.log(body.message);
84 -var app = express(); 52 + var nextword = body.message.word.searchedWord;
85 -app.use(bodyParser.json()); 53 + request.post(
86 -app.post('/hook', function (req, res) { 54 + {
87 - 55 + url: TARGET_URL,
88 - var eventObj = req.body.events[0]; 56 + headers: {
89 - var source = eventObj.source; 57 + 'Authorization': `Bearer ${TOKEN}`
90 - var message = eventObj.message; 58 + },
91 - 59 + json: {
92 - // request log 60 + "replyToken":replyToken,
93 - console.log('======================', new Date() ,'======================'); 61 + "messages":[
94 - console.log('[request]', req.body); 62 + {
95 - console.log('[request source] ', eventObj.source); 63 + "type":"text",
96 - console.log('[request message]', eventObj.message); 64 + "text":nextword
65 + }
66 + ]
67 + }
68 + },(error, response, body) => {
69 + console.log(body)
70 + });
71 + }
72 + });
97 73
98 - request.post( 74 +}
99 - {
100 - url: TARGET_URL,
101 - headers: {
102 - 'Authorization': `Bearer ${TOKEN}`
103 - },
104 - json: {
105 - "replyToken":eventObj.replyToken,
106 - "messages":[
107 - {
108 - "type":"text",
109 - "text":"Hello, user"
110 - },
111 - {
112 - "type":"text",
113 - "text":"May I help you?"
114 - }
115 - ]
116 - }
117 - },(error, response, body) => {
118 - console.log(body)
119 - });
120 -
121 -
122 - res.sendStatus(200);
123 -});
124 75
76 +//SSL
125 try { 77 try {
126 const option = { 78 const option = {
127 ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), 79 ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
...@@ -135,5 +87,4 @@ try { ...@@ -135,5 +87,4 @@ try {
135 } catch (error) { 87 } catch (error) {
136 console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); 88 console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
137 console.log(error); 89 console.log(error);
138 - }
139 - */
...\ No newline at end of file ...\ No newline at end of file
90 + }
...\ No newline at end of file ...\ No newline at end of file
......