EC2 Default User

Correct restart error

Showing 1 changed file with 53 additions and 24 deletions
......@@ -16,8 +16,6 @@ const STDICT_KEY = '14AA284251F616FF2151DBE4CD7B3D4C'
const bodyParser = require('body-parser');
const { json } = require('body-parser');
const { start } = require('repl');
const { exit } = require('process');
const { callbackify } = require('util');
var app = express();
app.use(bodyParser.json());
app.post('/hook', function (req, res) {
......@@ -42,21 +40,20 @@ app.post('/hook', function (req, res) {
var playingId = [];
var words = [];
function getIdindex(userId) {
return playingId.indexOf(userId);
}
function exit_(userId) {
var index = getIdindex(userId);
playingId.splice(index, 1);
words.splice(index, 1);
playingId.splice(playingId.indexOf(userId), 1);
words.splice(playingId.indexOf(userId), 1);
console.log(playingId);
console.log(words);
}
function isCommand(message)
{
return message == '/시작' || message == '/종료' ? true : false;
}
function all_check(replyToken, userId, word, index) {
if(words[index] != 0) {
if(!check_endtoend(word, index)) {
function all_check(replyToken, word, userId) {
if(words[playingId.indexOf(userId)] != 0) {
if(!check_endtoend(word, playingId.indexOf(userId))) {
request.post(
{
url: TARGET_URL,
......@@ -116,7 +113,7 @@ function all_check(replyToken, userId, word, index) {
'Referer': 'https://stdict.korean.go.kr/api/search.do',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${word}&` + 'req_type=json&' + 'advanced=y&' + 'method=exact&' + 'type1=word'
body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${word}&` + 'req_type=json&' + 'advanced=y&' + 'method=exact&' + 'type1=word&' + 'pos=1'
},(error, response, body) => {
if(!error && response.statusCode == 200) {
......@@ -195,7 +192,7 @@ function all_check(replyToken, userId, word, index) {
'Referer': 'https://stdict.korean.go.kr/api/search.do',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${word}&` + 'req_type=json&' + 'advanced=y&' + 'method=exact&' + 'type1=word'
body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${word}&` + 'req_type=json&' + 'advanced=y&' + 'method=exact&' + 'type1=word&' + 'pos=1'
},(error, response, body) => {
if(!error && response.statusCode == 200) {
......@@ -256,7 +253,12 @@ function wordchain(replyToken, message, userId) {
if(message == '/시작') {
playingId.push(userId);
words.push(0);
words.splice(playingId.indexOf(userId), 1, 0);
console.log(playingId);
console.log(words);
request.post(
{
url: TARGET_URL,
......@@ -319,10 +321,9 @@ function wordchain(replyToken, message, userId) {
});
} else {
var word = message;
index = getIdindex(userId);
var word = message;;
if(all_check(replyToken, userId, word, index)) {
if(all_check(replyToken, word, userId)) {
let letter = word.slice(-1);
request.post(
......@@ -332,17 +333,19 @@ function wordchain(replyToken, message, userId) {
'Referer': 'https://stdict.korean.go.kr/api/search.do',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${letter}&` + 'req_type=json&' + 'advanced=y&' + 'method=start&' + 'type1=word&' + 'letter_s=2'
body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${letter}&` + 'req_type=json&' + 'start=15&' + 'advanced=y&' + 'method=start&' + 'type1=word&' + 'pos=1&' + 'letter_s=2&' + 'letter_e=3'
},(error, response, body) => {
if(!error && response.statusCode == 200) {
console.log(body);
const parsedData = JSON.parse(body);
var nextword = parsedData.channel.item[0].word;
var word_definition = parsedData.channel.item[0].sense.definition;
words.splice(getIdindex(userId), 1, nextword);
try {
console.log(body);
const parsedData = JSON.parse(body);
var nextword = parsedData.channel.item[0].word;
var word_definition = parsedData.channel.item[0].sense.definition;
words.splice(playingId.indexOf(userId), 1, nextword);
console.log(words);
request.post(
request.post(
{
url: TARGET_URL,
headers: {
......@@ -360,6 +363,32 @@ function wordchain(replyToken, message, userId) {
},(error, response, body) => {
console.log(body);
});
} catch (error) {
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":replyToken,
"messages":[
{
"type":"text",
"text":"다음 단어를 찾을 수 없습니다."
},
{
"type":"text",
"text":"승리했습니다."
}
]
}
},(error, response, body) => {
console.log(body);
});
exit_(userId);
return false;
}
}
});
}
......