Toggle navigation
Toggle navigation
This project
Loading...
Sign in
정윤지
/
Word_Chain_Chatbot
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
EC2 Default User
2021-12-06 15:30:45 +0000
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
d00659077f1f0c8dbf9004904e5ec9fc06e6ae65
d0065907
1 parent
4c29db85
Modify replied word
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
95 deletions
app.js
app.js
View file @
d006590
...
...
@@ -8,6 +8,10 @@ const HTTPS = require('https');
const
domain
=
"2020105659.osschatbot.cf"
const
sslport
=
23023
;
//Open API
const
STDICT_URL
=
'https://stdict.korean.go.kr/api/search.do?'
const
STDICT_KEY
=
'14AA284251F616FF2151DBE4CD7B3D4C'
//Webhook
const
bodyParser
=
require
(
'body-parser'
);
var
app
=
express
();
...
...
@@ -21,107 +25,55 @@ app.post('/hook', function (req, res) {
// request log
console
.
log
(
'======================'
,
new
Date
()
,
'======================'
);
console
.
log
(
'[request]'
,
req
.
body
);
console
.
log
(
'[request source] '
,
eventObj
.
source
);
console
.
log
(
'[request message]'
,
eventObj
.
message
);
console
.
log
(
'[request source] '
,
source
);
console
.
log
(
'[request message]'
,
message
);
wordchain
(
eventObj
.
replyToken
,
message
.
text
);
//Reply
request
.
post
(
{
url
:
TARGET_URL
,
headers
:
{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:
{
"replyToken"
:
eventObj
.
replyToken
,
"messages"
:[
{
"type"
:
"text"
,
"text"
:
"안녕하세요"
},
{
"type"
:
"text"
,
"text"
:
"끝말잇기봇입니다"
}
]
}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
res
.
sendStatus
(
200
);
});
//SSL
try
{
const
option
=
{
ca
:
fs
.
readFileSync
(
'/etc/letsencrypt/live/'
+
domain
+
'/fullchain.pem'
),
key
:
fs
.
readFileSync
(
path
.
resolve
(
process
.
cwd
(),
'/etc/letsencrypt/live/'
+
domain
+
'/privkey.pem'
),
'utf8'
).
toString
(),
cert
:
fs
.
readFileSync
(
path
.
resolve
(
process
.
cwd
(),
'/etc/letsencrypt/live/'
+
domain
+
'/cert.pem'
),
'utf8'
).
toString
(),
};
HTTPS
.
createServer
(
option
,
app
).
listen
(
sslport
,
()
=>
{
console
.
log
(
`[HTTPS] Server is started on port
${
sslport
}
`
);
});
}
catch
(
error
)
{
console
.
log
(
'[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'
);
console
.
log
(
error
);
}
function
wordchain
(
replyToken
,
message
)
{
let
letter
=
message
.
slice
(
-
1
);
/*
var express = require('express');
const request = require('request');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = 'P5+e9JkPRckzKu1Fv7ghZl1MfP0wrYwYcgBVzHWe1OHO4Gtv1PIa9BARYqxoAThaXPSf8MyVKcpDE88V5jOnoS4o2xCNcgH9Ne4wcrPuNX6AT4gsfYEz0Y7n/A4+rT4vXHYARK34YPbkzzQbiXiyiQdB04t89/1O/w1cDnyilFU='
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
const domain = "2020105659.osschatbot.cf"
const sslport = 23023;
const bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.post('/hook', function (req, res) {
var eventObj = req.body.events[0];
var source = eventObj.source;
var message = eventObj.message;
// request log
console.log('======================', new Date() ,'======================');
console.log('[request]', req.body);
console.log('[request source] ', eventObj.source);
console.log('[request message]', eventObj.message);
request
.
post
(
{
url
:
STDICT_URL
,
headers
:
{
'Content-Type'
:
'application/x-www-form-urlencoded; charset=UTF-8'
,
'Stdict-Key'
:
`
${
STDICT_KEY
}
`
},
body
:
'https://stdict.korean.go.kr/api/search.do?'
+
`key=
${
STDICT_KEY
}
`
+
`q=
${
letter
}
`
+
'req_type=json'
+
'advanced=y'
+
'method=start'
,
json
:
true
},(
error
,
response
,
body
)
=>
{
if
(
!
error
&&
response
.
statusCode
==
200
)
{
console
.
log
(
body
.
message
);
var
nextword
=
body
.
message
.
word
.
searchedWord
;
request
.
post
(
{
url
:
TARGET_URL
,
headers
:
{
'Authorization'
:
`Bearer
${
TOKEN
}
`
},
json
:
{
"replyToken"
:
replyToken
,
"messages"
:[
{
"type"
:
"text"
,
"text"
:
nextword
}
]
}
},(
error
,
response
,
body
)
=>
{
console
.
log
(
body
)
});
}
});
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":eventObj.replyToken,
"messages":[
{
"type":"text",
"text":"Hello, user"
},
{
"type":"text",
"text":"May I help you?"
}
]
}
},(error, response, body) => {
console.log(body)
});
res.sendStatus(200);
});
}
//SSL
try
{
const
option
=
{
ca
:
fs
.
readFileSync
(
'/etc/letsencrypt/live/'
+
domain
+
'/fullchain.pem'
),
...
...
@@ -135,5 +87,4 @@ try {
}
catch
(
error
)
{
console
.
log
(
'[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'
);
console
.
log
(
error
);
}
*/
\ No newline at end of file
}
\ No newline at end of file
...
...
Please
register
or
login
to post a comment