Showing
1 changed file
with
81 additions
and
12 deletions
... | @@ -21,6 +21,7 @@ app.use(bodyParser.json()); | ... | @@ -21,6 +21,7 @@ app.use(bodyParser.json()); |
21 | app.post('/hook', function (req, res) { | 21 | app.post('/hook', function (req, res) { |
22 | 22 | ||
23 | var eventObj = req.body.events[0]; | 23 | var eventObj = req.body.events[0]; |
24 | + console.log(eventObj); | ||
24 | var source = eventObj.source; | 25 | var source = eventObj.source; |
25 | var message = eventObj.message; | 26 | var message = eventObj.message; |
26 | 27 | ||
... | @@ -30,15 +31,89 @@ app.post('/hook', function (req, res) { | ... | @@ -30,15 +31,89 @@ app.post('/hook', function (req, res) { |
30 | console.log('[request source] ', source); | 31 | console.log('[request source] ', source); |
31 | console.log('[request message]', message); | 32 | console.log('[request message]', message); |
32 | 33 | ||
33 | - wordchain(eventObj.replyToken, message.text); | 34 | + |
35 | + wordchain(eventObj.replyToken, message.text, source.userId); | ||
34 | 36 | ||
35 | 37 | ||
36 | res.sendStatus(200); | 38 | res.sendStatus(200); |
37 | }); | 39 | }); |
38 | 40 | ||
39 | -function wordchain(replyToken, message) { | 41 | +var playingId = []; |
42 | +function isCommand(message) | ||
43 | +{ | ||
44 | + return message == '/시작' || message == '/종료' ? true : false; | ||
45 | +} | ||
40 | 46 | ||
41 | - let letter = message.slice(-1); | 47 | +function wordchain(replyToken, message, userId) { |
48 | + if (isCommand(message)){ | ||
49 | + | ||
50 | + if(message == '/시작') { | ||
51 | + playingId.push(userId); | ||
52 | + request.post( | ||
53 | + { | ||
54 | + url: TARGET_URL, | ||
55 | + headers: { | ||
56 | + 'Authorization': `Bearer ${TOKEN}` | ||
57 | + }, | ||
58 | + json: { | ||
59 | + "replyToken":replyToken, | ||
60 | + "messages":[ | ||
61 | + { | ||
62 | + "type":"text", | ||
63 | + "text": "게임이 시작되었습니다.\n단어를 제시해주세요" | ||
64 | + } | ||
65 | + ] | ||
66 | + } | ||
67 | + },(error, response, body) => { | ||
68 | + console.log(body); | ||
69 | + }); | ||
70 | + } else { | ||
71 | + var num = playingId.indexOf(userId); | ||
72 | + playingId.splice(num, 1); | ||
73 | + request.post( | ||
74 | + { | ||
75 | + url: TARGET_URL, | ||
76 | + headers: { | ||
77 | + 'Authorization': `Bearer ${TOKEN}` | ||
78 | + }, | ||
79 | + json: { | ||
80 | + "replyToken":replyToken, | ||
81 | + "messages":[ | ||
82 | + { | ||
83 | + "type":"text", | ||
84 | + "text": "게임이 종료되었습니다." | ||
85 | + } | ||
86 | + ] | ||
87 | + } | ||
88 | + },(error, response, body) => { | ||
89 | + console.log(body); | ||
90 | + }); | ||
91 | + }; | ||
92 | + } else { | ||
93 | + | ||
94 | + if(!playingId.includes(userId)) { | ||
95 | + request.post( | ||
96 | + { | ||
97 | + url: TARGET_URL, | ||
98 | + headers: { | ||
99 | + 'Authorization': `Bearer ${TOKEN}` | ||
100 | + }, | ||
101 | + json: { | ||
102 | + "replyToken":replyToken, | ||
103 | + "messages":[ | ||
104 | + { | ||
105 | + "type":"text", | ||
106 | + "text": "안녕하세요. 끝말잇기봇입니다.\n-표준어 내의 명사만 가능\n\t(표준국어대사전에 근거)\n-한 글자 단어 금지\n-중복 단어 금지\n시작: '/시작', 종료: '/종료' 입력" | ||
107 | + } | ||
108 | + ] | ||
109 | + } | ||
110 | + },(error, response, body) => { | ||
111 | + console.log(body); | ||
112 | + }); | ||
113 | + } else { | ||
114 | + var word = message; | ||
115 | + let letter = word.slice(-1); | ||
116 | + console.log(letter); | ||
42 | 117 | ||
43 | request.post( | 118 | request.post( |
44 | { | 119 | { |
... | @@ -56,9 +131,6 @@ function wordchain(replyToken, message) { | ... | @@ -56,9 +131,6 @@ function wordchain(replyToken, message) { |
56 | var nextword = parsedData.channel.item[0].word; | 131 | var nextword = parsedData.channel.item[0].word; |
57 | var word_definition = parsedData.channel.item[0].sense.definition; | 132 | var word_definition = parsedData.channel.item[0].sense.definition; |
58 | 133 | ||
59 | - | ||
60 | - | ||
61 | - | ||
62 | request.post( | 134 | request.post( |
63 | { | 135 | { |
64 | url: TARGET_URL, | 136 | url: TARGET_URL, |
... | @@ -70,11 +142,7 @@ function wordchain(replyToken, message) { | ... | @@ -70,11 +142,7 @@ function wordchain(replyToken, message) { |
70 | "messages":[ | 142 | "messages":[ |
71 | { | 143 | { |
72 | "type":"text", | 144 | "type":"text", |
73 | - "text":nextword | 145 | + "text":nextword + ': ' + word_definition |
74 | - }, | ||
75 | - { | ||
76 | - "type":"text", | ||
77 | - "text":': ' + word_definition | ||
78 | } | 146 | } |
79 | ] | 147 | ] |
80 | } | 148 | } |
... | @@ -83,7 +151,8 @@ function wordchain(replyToken, message) { | ... | @@ -83,7 +151,8 @@ function wordchain(replyToken, message) { |
83 | }); | 151 | }); |
84 | } | 152 | } |
85 | }); | 153 | }); |
86 | - | 154 | + } |
155 | + } | ||
87 | } | 156 | } |
88 | 157 | ||
89 | //SSL | 158 | //SSL | ... | ... |
-
Please register or login to post a comment