EC2 Default User

Modify replied word

Showing 1 changed file with 29 additions and 78 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 -
72 -/*
73 -var express = require('express');
74 -const request = require('request');
75 -const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
76 -const TOKEN = 'P5+e9JkPRckzKu1Fv7ghZl1MfP0wrYwYcgBVzHWe1OHO4Gtv1PIa9BARYqxoAThaXPSf8MyVKcpDE88V5jOnoS4o2xCNcgH9Ne4wcrPuNX6AT4gsfYEz0Y7n/A4+rT4vXHYARK34YPbkzzQbiXiyiQdB04t89/1O/w1cDnyilFU='
77 -const fs = require('fs');
78 -const path = require('path');
79 -const HTTPS = require('https');
80 -const domain = "2020105659.osschatbot.cf"
81 -const sslport = 23023;
82 -
83 -const bodyParser = require('body-parser');
84 -var app = express();
85 -app.use(bodyParser.json());
86 -app.post('/hook', function (req, res) {
87 -
88 - var eventObj = req.body.events[0];
89 - var source = eventObj.source;
90 - var message = eventObj.message;
91 -
92 - // request log
93 - console.log('======================', new Date() ,'======================');
94 - console.log('[request]', req.body);
95 - console.log('[request source] ', eventObj.source);
96 - console.log('[request message]', eventObj.message);
97 39
98 request.post( 40 request.post(
99 { 41 {
42 + url: STDICT_URL,
43 + headers: {
44 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
45 + 'Stdict-Key': `${STDICT_KEY}`
46 + },
47 + body: 'https://stdict.korean.go.kr/api/search.do?' + `key=${STDICT_KEY}` + `q=${letter}` + 'req_type=json' + 'advanced=y' + 'method=start',
48 + json:true
49 + },(error, response, body) => {
50 + if(!error && response.statusCode == 200) {
51 + console.log(body.message);
52 + var nextword = body.message.word.searchedWord;
53 + request.post(
54 + {
100 url: TARGET_URL, 55 url: TARGET_URL,
101 headers: { 56 headers: {
102 'Authorization': `Bearer ${TOKEN}` 57 'Authorization': `Bearer ${TOKEN}`
103 }, 58 },
104 json: { 59 json: {
105 - "replyToken":eventObj.replyToken, 60 + "replyToken":replyToken,
106 "messages":[ 61 "messages":[
107 { 62 {
108 "type":"text", 63 "type":"text",
109 - "text":"Hello, user" 64 + "text":nextword
110 - },
111 - {
112 - "type":"text",
113 - "text":"May I help you?"
114 } 65 }
115 ] 66 ]
116 } 67 }
117 },(error, response, body) => { 68 },(error, response, body) => {
118 console.log(body) 69 console.log(body)
119 }); 70 });
71 + }
72 + });
120 73
74 +}
121 75
122 - res.sendStatus(200); 76 +//SSL
123 -});
124 -
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'),
...@@ -136,4 +88,3 @@ try { ...@@ -136,4 +88,3 @@ try {
136 console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); 88 console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
137 console.log(error); 89 console.log(error);
138 } 90 }
...\ No newline at end of file ...\ No newline at end of file
139 - */
...\ No newline at end of file ...\ No newline at end of file
......