EC2 Default User

Correct restart error

Showing 1 changed file with 48 additions and 19 deletions
...@@ -16,8 +16,6 @@ const STDICT_KEY = '14AA284251F616FF2151DBE4CD7B3D4C' ...@@ -16,8 +16,6 @@ const STDICT_KEY = '14AA284251F616FF2151DBE4CD7B3D4C'
16 const bodyParser = require('body-parser'); 16 const bodyParser = require('body-parser');
17 const { json } = require('body-parser'); 17 const { json } = require('body-parser');
18 const { start } = require('repl'); 18 const { start } = require('repl');
19 -const { exit } = require('process');
20 -const { callbackify } = require('util');
21 var app = express(); 19 var app = express();
22 app.use(bodyParser.json()); 20 app.use(bodyParser.json());
23 app.post('/hook', function (req, res) { 21 app.post('/hook', function (req, res) {
...@@ -42,21 +40,20 @@ app.post('/hook', function (req, res) { ...@@ -42,21 +40,20 @@ app.post('/hook', function (req, res) {
42 var playingId = []; 40 var playingId = [];
43 var words = []; 41 var words = [];
44 42
45 -function getIdindex(userId) { 43 +
46 - return playingId.indexOf(userId);
47 -}
48 function exit_(userId) { 44 function exit_(userId) {
49 - var index = getIdindex(userId); 45 + playingId.splice(playingId.indexOf(userId), 1);
50 - playingId.splice(index, 1); 46 + words.splice(playingId.indexOf(userId), 1);
51 - words.splice(index, 1); 47 + console.log(playingId);
48 + console.log(words);
52 } 49 }
53 function isCommand(message) 50 function isCommand(message)
54 { 51 {
55 return message == '/시작' || message == '/종료' ? true : false; 52 return message == '/시작' || message == '/종료' ? true : false;
56 } 53 }
57 -function all_check(replyToken, userId, word, index) { 54 +function all_check(replyToken, word, userId) {
58 - if(words[index] != 0) { 55 + if(words[playingId.indexOf(userId)] != 0) {
59 - if(!check_endtoend(word, index)) { 56 + if(!check_endtoend(word, playingId.indexOf(userId))) {
60 request.post( 57 request.post(
61 { 58 {
62 url: TARGET_URL, 59 url: TARGET_URL,
...@@ -116,7 +113,7 @@ function all_check(replyToken, userId, word, index) { ...@@ -116,7 +113,7 @@ function all_check(replyToken, userId, word, index) {
116 'Referer': 'https://stdict.korean.go.kr/api/search.do', 113 'Referer': 'https://stdict.korean.go.kr/api/search.do',
117 'Content-Type': 'application/x-www-form-urlencoded' 114 'Content-Type': 'application/x-www-form-urlencoded'
118 }, 115 },
119 - body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${word}&` + 'req_type=json&' + 'advanced=y&' + 'method=exact&' + 'type1=word' 116 + body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${word}&` + 'req_type=json&' + 'advanced=y&' + 'method=exact&' + 'type1=word&' + 'pos=1'
120 117
121 },(error, response, body) => { 118 },(error, response, body) => {
122 if(!error && response.statusCode == 200) { 119 if(!error && response.statusCode == 200) {
...@@ -195,7 +192,7 @@ function all_check(replyToken, userId, word, index) { ...@@ -195,7 +192,7 @@ function all_check(replyToken, userId, word, index) {
195 'Referer': 'https://stdict.korean.go.kr/api/search.do', 192 'Referer': 'https://stdict.korean.go.kr/api/search.do',
196 'Content-Type': 'application/x-www-form-urlencoded' 193 'Content-Type': 'application/x-www-form-urlencoded'
197 }, 194 },
198 - body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${word}&` + 'req_type=json&' + 'advanced=y&' + 'method=exact&' + 'type1=word' 195 + body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${word}&` + 'req_type=json&' + 'advanced=y&' + 'method=exact&' + 'type1=word&' + 'pos=1'
199 196
200 },(error, response, body) => { 197 },(error, response, body) => {
201 if(!error && response.statusCode == 200) { 198 if(!error && response.statusCode == 200) {
...@@ -256,7 +253,12 @@ function wordchain(replyToken, message, userId) { ...@@ -256,7 +253,12 @@ function wordchain(replyToken, message, userId) {
256 253
257 if(message == '/시작') { 254 if(message == '/시작') {
258 playingId.push(userId); 255 playingId.push(userId);
259 - words.push(0); 256 + words.splice(playingId.indexOf(userId), 1, 0);
257 + console.log(playingId);
258 + console.log(words);
259 +
260 +
261 +
260 request.post( 262 request.post(
261 { 263 {
262 url: TARGET_URL, 264 url: TARGET_URL,
...@@ -319,10 +321,9 @@ function wordchain(replyToken, message, userId) { ...@@ -319,10 +321,9 @@ function wordchain(replyToken, message, userId) {
319 }); 321 });
320 } else { 322 } else {
321 323
322 - var word = message; 324 + var word = message;;
323 - index = getIdindex(userId);
324 325
325 - if(all_check(replyToken, userId, word, index)) { 326 + if(all_check(replyToken, word, userId)) {
326 let letter = word.slice(-1); 327 let letter = word.slice(-1);
327 328
328 request.post( 329 request.post(
...@@ -332,15 +333,17 @@ function wordchain(replyToken, message, userId) { ...@@ -332,15 +333,17 @@ function wordchain(replyToken, message, userId) {
332 'Referer': 'https://stdict.korean.go.kr/api/search.do', 333 'Referer': 'https://stdict.korean.go.kr/api/search.do',
333 'Content-Type': 'application/x-www-form-urlencoded' 334 'Content-Type': 'application/x-www-form-urlencoded'
334 }, 335 },
335 - body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${letter}&` + 'req_type=json&' + 'advanced=y&' + 'method=start&' + 'type1=word&' + 'letter_s=2' 336 + body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${letter}&` + 'req_type=json&' + 'start=15&' + 'advanced=y&' + 'method=start&' + 'type1=word&' + 'pos=1&' + 'letter_s=2&' + 'letter_e=3'
336 337
337 },(error, response, body) => { 338 },(error, response, body) => {
338 if(!error && response.statusCode == 200) { 339 if(!error && response.statusCode == 200) {
340 + try {
339 console.log(body); 341 console.log(body);
340 const parsedData = JSON.parse(body); 342 const parsedData = JSON.parse(body);
341 var nextword = parsedData.channel.item[0].word; 343 var nextword = parsedData.channel.item[0].word;
342 var word_definition = parsedData.channel.item[0].sense.definition; 344 var word_definition = parsedData.channel.item[0].sense.definition;
343 - words.splice(getIdindex(userId), 1, nextword); 345 + words.splice(playingId.indexOf(userId), 1, nextword);
346 + console.log(words);
344 347
345 request.post( 348 request.post(
346 { 349 {
...@@ -360,6 +363,32 @@ function wordchain(replyToken, message, userId) { ...@@ -360,6 +363,32 @@ function wordchain(replyToken, message, userId) {
360 },(error, response, body) => { 363 },(error, response, body) => {
361 console.log(body); 364 console.log(body);
362 }); 365 });
366 + } catch (error) {
367 + request.post(
368 + {
369 + url: TARGET_URL,
370 + headers: {
371 + 'Authorization': `Bearer ${TOKEN}`
372 + },
373 + json: {
374 + "replyToken":replyToken,
375 + "messages":[
376 + {
377 + "type":"text",
378 + "text":"다음 단어를 찾을 수 없습니다."
379 + },
380 + {
381 + "type":"text",
382 + "text":"승리했습니다."
383 + }
384 + ]
385 + }
386 + },(error, response, body) => {
387 + console.log(body);
388 + });
389 + exit_(userId);
390 + return false;
391 + }
363 } 392 }
364 }); 393 });
365 } 394 }
......