Showing
1 changed file
with
220 additions
and
7 deletions
... | @@ -16,12 +16,13 @@ const STDICT_KEY = '14AA284251F616FF2151DBE4CD7B3D4C' | ... | @@ -16,12 +16,13 @@ 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'); | ||
19 | var app = express(); | 21 | var app = express(); |
20 | app.use(bodyParser.json()); | 22 | app.use(bodyParser.json()); |
21 | app.post('/hook', function (req, res) { | 23 | app.post('/hook', function (req, res) { |
22 | 24 | ||
23 | var eventObj = req.body.events[0]; | 25 | var eventObj = req.body.events[0]; |
24 | - console.log(eventObj); | ||
25 | var source = eventObj.source; | 26 | var source = eventObj.source; |
26 | var message = eventObj.message; | 27 | var message = eventObj.message; |
27 | 28 | ||
... | @@ -39,16 +40,223 @@ app.post('/hook', function (req, res) { | ... | @@ -39,16 +40,223 @@ app.post('/hook', function (req, res) { |
39 | }); | 40 | }); |
40 | 41 | ||
41 | var playingId = []; | 42 | var playingId = []; |
43 | +var words = []; | ||
44 | + | ||
45 | +function getIdindex(userId) { | ||
46 | + return playingId.indexOf(userId); | ||
47 | +} | ||
48 | +function exit_(userId) { | ||
49 | + var index = getIdindex(userId); | ||
50 | + playingId.splice(index, 1); | ||
51 | + words.splice(index, 1); | ||
52 | +} | ||
42 | function isCommand(message) | 53 | function isCommand(message) |
43 | { | 54 | { |
44 | return message == '/시작' || message == '/종료' ? true : false; | 55 | return message == '/시작' || message == '/종료' ? true : false; |
45 | } | 56 | } |
57 | +function all_check(replyToken, userId, word, index) { | ||
58 | + if(words[index] != 0) { | ||
59 | + if(!check_endtoend(word, index)) { | ||
60 | + request.post( | ||
61 | + { | ||
62 | + url: TARGET_URL, | ||
63 | + headers: { | ||
64 | + 'Authorization': `Bearer ${TOKEN}` | ||
65 | + }, | ||
66 | + json: { | ||
67 | + "replyToken":replyToken, | ||
68 | + "messages":[ | ||
69 | + { | ||
70 | + "type":"text", | ||
71 | + "text":"끝음절로 시작해야합니다." | ||
72 | + }, | ||
73 | + { | ||
74 | + "type":"text", | ||
75 | + "text":"패배했습니다." | ||
76 | + } | ||
77 | + ] | ||
78 | + } | ||
79 | + },(error, response, body) => { | ||
80 | + console.log(body); | ||
81 | + }); | ||
82 | + exit_(userId); | ||
83 | + return false; | ||
84 | + } else if(!check_length(word)) { | ||
85 | + request.post( | ||
86 | + { | ||
87 | + url: TARGET_URL, | ||
88 | + headers: { | ||
89 | + 'Authorization': `Bearer ${TOKEN}` | ||
90 | + }, | ||
91 | + json: { | ||
92 | + "replyToken":replyToken, | ||
93 | + "messages":[ | ||
94 | + { | ||
95 | + "type":"text", | ||
96 | + "text":"한 글자 단어입니다." | ||
97 | + }, | ||
98 | + { | ||
99 | + "type":"text", | ||
100 | + "text":"패배했습니다." | ||
101 | + } | ||
102 | + ] | ||
103 | + } | ||
104 | + },(error, response, body) => { | ||
105 | + console.log(body); | ||
106 | + }); | ||
107 | + exit_(userId); | ||
108 | + return false; | ||
109 | + } else { | ||
110 | + var uncondition = 0; | ||
111 | + if(uncondition == 0) { | ||
112 | + request.post( | ||
113 | + { | ||
114 | + url: STDICT_URL, | ||
115 | + headers: { | ||
116 | + 'Referer': 'https://stdict.korean.go.kr/api/search.do', | ||
117 | + 'Content-Type': 'application/x-www-form-urlencoded' | ||
118 | + }, | ||
119 | + body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${word}&` + 'req_type=json&' + 'advanced=y&' + 'method=exact&' + 'type1=word' | ||
120 | + | ||
121 | + },(error, response, body) => { | ||
122 | + if(!error && response.statusCode == 200) { | ||
123 | + try {console.log(body); | ||
124 | + const parsedData = JSON.parse(body); | ||
125 | + num = parsedData.channel.total; | ||
126 | + console.log(num); | ||
127 | + } catch (error) { | ||
128 | + console.log("없는 단어"); | ||
129 | + { | ||
130 | + request.post( | ||
131 | + { | ||
132 | + url: TARGET_URL, | ||
133 | + headers: { | ||
134 | + 'Authorization': `Bearer ${TOKEN}` | ||
135 | + }, | ||
136 | + json: { | ||
137 | + "replyToken":replyToken, | ||
138 | + "messages":[ | ||
139 | + { | ||
140 | + "type":"text", | ||
141 | + "text":"없는 단어입니다." | ||
142 | + }, | ||
143 | + { | ||
144 | + "type":"text", | ||
145 | + "text":"패배했습니다." | ||
146 | + } | ||
147 | + ] | ||
148 | + } | ||
149 | + },(error, response, body) => { | ||
150 | + console.log(body); | ||
151 | + }); | ||
152 | + exit_(userId); | ||
153 | + return false; | ||
154 | + } | ||
155 | + } | ||
156 | + } | ||
157 | + | ||
158 | + }); | ||
159 | + | ||
160 | + } | ||
161 | + } | ||
162 | + } else { | ||
163 | + if(!check_length(word)) { | ||
164 | + request.post( | ||
165 | + { | ||
166 | + url: TARGET_URL, | ||
167 | + headers: { | ||
168 | + 'Authorization': `Bearer ${TOKEN}` | ||
169 | + }, | ||
170 | + json: { | ||
171 | + "replyToken":replyToken, | ||
172 | + "messages":[ | ||
173 | + { | ||
174 | + "type":"text", | ||
175 | + "text":"한 글자 단어입니다." | ||
176 | + }, | ||
177 | + { | ||
178 | + "type":"text", | ||
179 | + "text":"패배했습니다." | ||
180 | + } | ||
181 | + ] | ||
182 | + } | ||
183 | + },(error, response, body) => { | ||
184 | + console.log(body); | ||
185 | + }); | ||
186 | + exit_(userId); | ||
187 | + return false; | ||
188 | + } else { | ||
189 | + var uncondition = 0; | ||
190 | + if(uncondition == 0) { | ||
191 | + request.post( | ||
192 | + { | ||
193 | + url: STDICT_URL, | ||
194 | + headers: { | ||
195 | + 'Referer': 'https://stdict.korean.go.kr/api/search.do', | ||
196 | + 'Content-Type': 'application/x-www-form-urlencoded' | ||
197 | + }, | ||
198 | + body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${word}&` + 'req_type=json&' + 'advanced=y&' + 'method=exact&' + 'type1=word' | ||
199 | + | ||
200 | + },(error, response, body) => { | ||
201 | + if(!error && response.statusCode == 200) { | ||
202 | + try {console.log(body); | ||
203 | + const parsedData = JSON.parse(body); | ||
204 | + var num = parsedData.channel.total; | ||
205 | + console.log(num); | ||
206 | + } catch (error) { | ||
207 | + console.log("없는 단어"); | ||
208 | + { | ||
209 | + request.post( | ||
210 | + { | ||
211 | + url: TARGET_URL, | ||
212 | + headers: { | ||
213 | + 'Authorization': `Bearer ${TOKEN}` | ||
214 | + }, | ||
215 | + json: { | ||
216 | + "replyToken":replyToken, | ||
217 | + "messages":[ | ||
218 | + { | ||
219 | + "type":"text", | ||
220 | + "text":"없는 단어입니다." | ||
221 | + }, | ||
222 | + { | ||
223 | + "type":"text", | ||
224 | + "text":"패배했습니다." | ||
225 | + } | ||
226 | + ] | ||
227 | + } | ||
228 | + },(error, response, body) => { | ||
229 | + console.log(body); | ||
230 | + }); | ||
231 | + exit_(userId); | ||
232 | + return false; | ||
233 | + } | ||
234 | + } | ||
235 | + } | ||
236 | + | ||
237 | + }); | ||
238 | + | ||
239 | + } | ||
240 | + } | ||
241 | + } | ||
242 | + return true; | ||
243 | +} | ||
244 | +function check_endtoend(word, index) { | ||
245 | + return word.slice(0, 1) == words[index].slice(-1); | ||
246 | +} | ||
247 | +function check_exist(word) { | ||
248 | + | ||
249 | +} | ||
250 | +function check_length(word) { | ||
251 | + return word.length != 1; | ||
252 | +} | ||
46 | 253 | ||
47 | function wordchain(replyToken, message, userId) { | 254 | function wordchain(replyToken, message, userId) { |
48 | if (isCommand(message)){ | 255 | if (isCommand(message)){ |
49 | 256 | ||
50 | if(message == '/시작') { | 257 | if(message == '/시작') { |
51 | playingId.push(userId); | 258 | playingId.push(userId); |
259 | + words.push(0); | ||
52 | request.post( | 260 | request.post( |
53 | { | 261 | { |
54 | url: TARGET_URL, | 262 | url: TARGET_URL, |
... | @@ -60,7 +268,7 @@ function wordchain(replyToken, message, userId) { | ... | @@ -60,7 +268,7 @@ function wordchain(replyToken, message, userId) { |
60 | "messages":[ | 268 | "messages":[ |
61 | { | 269 | { |
62 | "type":"text", | 270 | "type":"text", |
63 | - "text": "게임이 시작되었습니다.\n단어를 제시해주세요" | 271 | + "text": "게임이 시작되었습니다.\n단어를 제시해주세요." |
64 | } | 272 | } |
65 | ] | 273 | ] |
66 | } | 274 | } |
... | @@ -68,8 +276,6 @@ function wordchain(replyToken, message, userId) { | ... | @@ -68,8 +276,6 @@ function wordchain(replyToken, message, userId) { |
68 | console.log(body); | 276 | console.log(body); |
69 | }); | 277 | }); |
70 | } else { | 278 | } else { |
71 | - var num = playingId.indexOf(userId); | ||
72 | - playingId.splice(num, 1); | ||
73 | request.post( | 279 | request.post( |
74 | { | 280 | { |
75 | url: TARGET_URL, | 281 | url: TARGET_URL, |
... | @@ -88,6 +294,7 @@ function wordchain(replyToken, message, userId) { | ... | @@ -88,6 +294,7 @@ function wordchain(replyToken, message, userId) { |
88 | },(error, response, body) => { | 294 | },(error, response, body) => { |
89 | console.log(body); | 295 | console.log(body); |
90 | }); | 296 | }); |
297 | + exit_(userId); | ||
91 | }; | 298 | }; |
92 | } else { | 299 | } else { |
93 | 300 | ||
... | @@ -103,7 +310,7 @@ function wordchain(replyToken, message, userId) { | ... | @@ -103,7 +310,7 @@ function wordchain(replyToken, message, userId) { |
103 | "messages":[ | 310 | "messages":[ |
104 | { | 311 | { |
105 | "type":"text", | 312 | "type":"text", |
106 | - "text": "안녕하세요. 끝말잇기봇입니다.\n-표준어 내의 명사만 가능\n\t(표준국어대사전에 근거)\n-한 글자 단어 금지\n-중복 단어 금지\n시작: '/시작', 종료: '/종료' 입력" | 313 | + "text": "안녕하세요. 끝말잇기봇입니다.\n-표준어 내의 명사만 가능\n\t(표준국어대사전에 근거)\n-한 글자 단어 금지\n시작: '/시작', 종료: '/종료' 입력" |
107 | } | 314 | } |
108 | ] | 315 | ] |
109 | } | 316 | } |
... | @@ -111,9 +318,12 @@ function wordchain(replyToken, message, userId) { | ... | @@ -111,9 +318,12 @@ function wordchain(replyToken, message, userId) { |
111 | console.log(body); | 318 | console.log(body); |
112 | }); | 319 | }); |
113 | } else { | 320 | } else { |
321 | + | ||
114 | var word = message; | 322 | var word = message; |
323 | + index = getIdindex(userId); | ||
324 | + | ||
325 | + if(all_check(replyToken, userId, word, index)) { | ||
115 | let letter = word.slice(-1); | 326 | let letter = word.slice(-1); |
116 | - console.log(letter); | ||
117 | 327 | ||
118 | request.post( | 328 | request.post( |
119 | { | 329 | { |
... | @@ -122,7 +332,7 @@ function wordchain(replyToken, message, userId) { | ... | @@ -122,7 +332,7 @@ function wordchain(replyToken, message, userId) { |
122 | 'Referer': 'https://stdict.korean.go.kr/api/search.do', | 332 | 'Referer': 'https://stdict.korean.go.kr/api/search.do', |
123 | 'Content-Type': 'application/x-www-form-urlencoded' | 333 | 'Content-Type': 'application/x-www-form-urlencoded' |
124 | }, | 334 | }, |
125 | - body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${letter}&` + 'req_type=json&' + 'advanced=y&' + 'method=start&' + 'letter_s=2' | 335 | + body: `key=14AA284251F616FF2151DBE4CD7B3D4C&` + `q=${letter}&` + 'req_type=json&' + 'advanced=y&' + 'method=start&' + 'type1=word&' + 'letter_s=2' |
126 | 336 | ||
127 | },(error, response, body) => { | 337 | },(error, response, body) => { |
128 | if(!error && response.statusCode == 200) { | 338 | if(!error && response.statusCode == 200) { |
... | @@ -130,6 +340,7 @@ function wordchain(replyToken, message, userId) { | ... | @@ -130,6 +340,7 @@ function wordchain(replyToken, message, userId) { |
130 | const parsedData = JSON.parse(body); | 340 | const parsedData = JSON.parse(body); |
131 | var nextword = parsedData.channel.item[0].word; | 341 | var nextword = parsedData.channel.item[0].word; |
132 | var word_definition = parsedData.channel.item[0].sense.definition; | 342 | var word_definition = parsedData.channel.item[0].sense.definition; |
343 | + words.splice(getIdindex(userId), 1, nextword); | ||
133 | 344 | ||
134 | request.post( | 345 | request.post( |
135 | { | 346 | { |
... | @@ -153,6 +364,8 @@ function wordchain(replyToken, message, userId) { | ... | @@ -153,6 +364,8 @@ function wordchain(replyToken, message, userId) { |
153 | }); | 364 | }); |
154 | } | 365 | } |
155 | } | 366 | } |
367 | + | ||
368 | + } | ||
156 | } | 369 | } |
157 | 370 | ||
158 | //SSL | 371 | //SSL | ... | ... |
-
Please register or login to post a comment