Showing
1 changed file
with
148 additions
and
6 deletions
... | @@ -3,13 +3,26 @@ const express = require('express'); | ... | @@ -3,13 +3,26 @@ const express = require('express'); |
3 | const bodyParser = require('body-parser'); | 3 | const bodyParser = require('body-parser'); |
4 | const request = require('request'); | 4 | const request = require('request'); |
5 | const app = express(); | 5 | const app = express(); |
6 | +var fs = require("fs"); | ||
7 | + | ||
8 | +app.use(bodyParser.json()); | ||
9 | +app.use(bodyParser.text()); | ||
10 | +app.use(bodyParser.urlencoded({ | ||
11 | + extended: false | ||
12 | +})); //post에서bodyparser로 받기 위함 | ||
13 | + | ||
14 | +var price; | ||
15 | +var shape = []; | ||
16 | +var kinds = []; | ||
6 | 17 | ||
7 | // 3번 단계에서 얻은 토큰을 넣어줍니다 . 개인 깃에 올릴 경우 이 부분은 절대 커밋하지 마세요. | 18 | // 3번 단계에서 얻은 토큰을 넣어줍니다 . 개인 깃에 올릴 경우 이 부분은 절대 커밋하지 마세요. |
8 | var PAGE_ACCESS_TOKEN = 'EAAIZBdEhUzlcBAAy7a21fSEuW76k1ABpld4ncE0XcrRv902dy56lZAZCAZANmKifUVah2H5j94xZBpmWAVGR9bhVlouzYyykVJ0XD447ju4Wco2G08jroUdF9hU1FZCILBSZAJxmgkoTYbCQ2FQ8xZByvsDGZAPVPvtBHL51ZCTfOiEgZDZD'; | 19 | var PAGE_ACCESS_TOKEN = 'EAAIZBdEhUzlcBAAy7a21fSEuW76k1ABpld4ncE0XcrRv902dy56lZAZCAZANmKifUVah2H5j94xZBpmWAVGR9bhVlouzYyykVJ0XD447ju4Wco2G08jroUdF9hU1FZCILBSZAJxmgkoTYbCQ2FQ8xZByvsDGZAPVPvtBHL51ZCTfOiEgZDZD'; |
9 | 20 | ||
10 | app.set('port', (process.env.PORT || 5000)); | 21 | app.set('port', (process.env.PORT || 5000)); |
11 | 22 | ||
12 | -app.use(bodyParser.urlencoded({ extended: false })); | 23 | +app.use(bodyParser.urlencoded({ |
24 | + extended: false | ||
25 | +})); | ||
13 | app.use(bodyParser.json()); | 26 | app.use(bodyParser.json()); |
14 | 27 | ||
15 | app.get('/', (req, res) => { | 28 | app.get('/', (req, res) => { |
... | @@ -43,7 +56,38 @@ app.post("/webhook", (req, res) => { | ... | @@ -43,7 +56,38 @@ app.post("/webhook", (req, res) => { |
43 | if (messagingEvent.optin) { | 56 | if (messagingEvent.optin) { |
44 | receivedAuthentication(messagingEvent); | 57 | receivedAuthentication(messagingEvent); |
45 | } else if (messagingEvent.message) { | 58 | } else if (messagingEvent.message) { |
59 | + var quest = messagingEvent.message.text; | ||
60 | + var price = quest; | ||
61 | + if (((quest.search('음식') != -1) || (quest.search('추천') != -1)) || ((quest.search('뭐') != -1) && (quest.search('먹') != -1))) { | ||
62 | + receivedMessage_price(messagingEvent); | ||
63 | + } else if (price == Number(price)) { | ||
64 | + price = Number(price); | ||
65 | + sendTextMessage(messagingEvent.sender.id, '원하시는 음식 카테고리가 있나요?'); | ||
66 | + } else if ((quest.search('한식') != -1) || (quest.search('중식') != -1) || (quest.search('일식') != -1) || (quest.search('양식') != -1)) { | ||
67 | + if ((quest.search('한식') != -1)) | ||
68 | + kinds.push('한식'); | ||
69 | + if ((quest.search('중식') != -1)) | ||
70 | + kinds.push('중식'); | ||
71 | + if (quest.search('일식') != -1) | ||
72 | + kinds.push('일식'); | ||
73 | + if ((quest.search('양식') != -1)) | ||
74 | + kinds.push('양식'); | ||
75 | + sendTextMessage(messagingEvent.sender.id, '원하시는 음식 형태가 있나요?'); | ||
76 | + } else if ((quest.search('밥') != -1) || (quest.search('면') != -1) || (quest.search('고기') != -1) || (quest.search('기타') != -1)) { | ||
77 | + if ((quest.search('밥') != -1)) | ||
78 | + shape.push('밥'); | ||
79 | + if ((quest.search('면') != -1)) | ||
80 | + shape.push('면'); | ||
81 | + if (quest.search('고기') != -1) | ||
82 | + shape.push('고기'); | ||
83 | + if ((quest.search('기타') != -1)) | ||
84 | + shape.push('기타'); | ||
85 | + receivedMessage_recommend(messagingEvent); | ||
86 | + } else if ((quest.search('먹을까') != -1) || (quest.search('말까') != -1)) { | ||
87 | + receivedMessage_select(messagingEvent); | ||
88 | + } else { | ||
46 | receivedMessage(messagingEvent); | 89 | receivedMessage(messagingEvent); |
90 | + } | ||
47 | } else if (messagingEvent.postback) { | 91 | } else if (messagingEvent.postback) { |
48 | receivedPostback(messagingEvent); | 92 | receivedPostback(messagingEvent); |
49 | } else { | 93 | } else { |
... | @@ -51,17 +95,109 @@ app.post("/webhook", (req, res) => { | ... | @@ -51,17 +95,109 @@ app.post("/webhook", (req, res) => { |
51 | } | 95 | } |
52 | }); | 96 | }); |
53 | }); | 97 | }); |
54 | - | ||
55 | res.sendStatus(200); | 98 | res.sendStatus(200); |
56 | } | 99 | } |
57 | }); | 100 | }); |
58 | 101 | ||
59 | // 메세지 받고 내보내기 | 102 | // 메세지 받고 내보내기 |
103 | +function receivedMessage_recommend(event) { | ||
104 | + var senderId = event.sender.id; | ||
105 | + var content = event.message.text; | ||
106 | + var bot_message = content; | ||
107 | + fs.readFile('data/food.json', 'utf8', function(err, food_data) { | ||
108 | + if (err) { | ||
109 | + console.log((err)); | ||
110 | + res.status(500).send('Internal Server Error'); | ||
111 | + } else { | ||
112 | + var users = JSON.parse(food_data); | ||
113 | + var set1 = new Set(); | ||
114 | + var set2 = new Set(); | ||
115 | + var set3 = new Set(); | ||
116 | + | ||
117 | + function add(users, price, shape, kinds, callback) { | ||
118 | + for (var foods in users) { | ||
119 | + if (shape.length != 0) { | ||
120 | + for (var i = 0; i < shape.length; i++) { | ||
121 | + if (users[foods]['shape'] == shape[i]) { | ||
122 | + set1.add(foods); | ||
123 | + } | ||
124 | + } | ||
125 | + } else { | ||
126 | + set1.add(foods); | ||
127 | + } | ||
128 | + if (kinds.length != 0) { | ||
129 | + for (var j = 0; j < kinds.length; j++) { | ||
130 | + if (users[foods]['kinds'] == kinds[j]) { | ||
131 | + set2.add(foods); | ||
132 | + } | ||
133 | + } | ||
134 | + } else { | ||
135 | + set2.add(foods); | ||
136 | + } | ||
137 | + if (!price || (price && users[foods]['price'] <= Number(price) + 3000 && users[foods]['price'] >= Number(price) - 3000)) { | ||
138 | + set3.add(foods); | ||
139 | + } | ||
140 | + } | ||
141 | + callback(set1, set2, set3); | ||
142 | + } | ||
143 | + | ||
144 | + add(users, price, shape, kinds, function(set1, set2, set3) { | ||
145 | + let difference1 = new Set([...set1].filter(i => set2.has(i))); | ||
146 | + let difference2 = new Set([...difference1].filter(i => set3.has(i))); | ||
147 | + var food = [...difference2]; | ||
148 | + var length = food.length; | ||
149 | + console.log('food 목록: ' + food); | ||
150 | + console.log('food 갯수: ' + length); | ||
151 | + var rand = Math.floor(Math.random() * length); | ||
152 | + var food_value = food[rand]; | ||
153 | + console.log('랜덤 food 번호: ' + rand); | ||
154 | + price = ''; | ||
155 | + shape = []; | ||
156 | + kinds = []; | ||
157 | + sendTextMessage(senderId, '제가 추천해드리는 음식이에욤:>'); | ||
158 | + sendTextMessage(senderId, food_value); | ||
159 | + // sendTextMessage(senderId, /data/images/food_value.png); | ||
160 | + }); //add closed | ||
161 | + } | ||
162 | + }); //readFile closed | ||
163 | + | ||
164 | +} | ||
165 | + | ||
60 | function receivedMessage(event) { | 166 | function receivedMessage(event) { |
61 | var senderId = event.sender.id; | 167 | var senderId = event.sender.id; |
62 | var content = event.message.text; | 168 | var content = event.message.text; |
63 | var bot_message = content; | 169 | var bot_message = content; |
64 | - sendTextMessage(senderId, bot_message); | 170 | + var happpy = ['오늘은 무엇을 드실건가요?:)', '뿌에에ㅔ엥', '헣', '저한테 왜그러시는거죠?', '뭐지', '그러쿤요!', '훌쩍', '쒸익쒸익', '뭐하시는거죠?', '눈물', '세상에', '이게무슨일이람', 'ㅋㅋㅋㅋㅋㅋㅋㅋ', '무야', '아니이ㅣㅣ', '네에ㅔㅔ', '찡긋', '(당근 흔드는 중)', '도와주세요', '살려줘']; |
171 | + var rand = Math.floor(Math.random() * happpy.length); | ||
172 | + sendTextMessage(senderId, happpy[rand]); | ||
173 | +} | ||
174 | + | ||
175 | +function receivedMessage_select(event) { | ||
176 | + var senderId = event.sender.id; | ||
177 | + var content = event.message.text; | ||
178 | + var answer = ['먹어', '먹지마']; | ||
179 | + var rand = Math.floor(Math.random() * answer.length); | ||
180 | + sendTextMessage(senderId, answer[rand]); | ||
181 | +} | ||
182 | + | ||
183 | +function receivedMessage_price(event) { | ||
184 | + var senderId = event.sender.id; | ||
185 | + var content = event.message.text; | ||
186 | + var bot_message = content; | ||
187 | + var recom = ['제가 추천해드릴까욤??', '에휴 결정장애시구먼요. 제가 추천해드릴게요', '밥먹어요!! 추천해줄게요', '뭐드시고 싶어요?? 추천해줄게욤']; | ||
188 | + var rand = Math.floor(Math.random() * recom.length); | ||
189 | + sendTextMessage(senderId, recom[rand]); | ||
190 | + sendTextMessage(senderId, "가격대를 입력하시겠어요? :)"); | ||
191 | +} | ||
192 | + | ||
193 | +function receivedMessage_map(event) { | ||
194 | + var senderId = event.sender.id; | ||
195 | + var content = event.message.text; | ||
196 | + var bot_message = content; | ||
197 | + var recom = ['제가 추천해드릴까욤??', '에휴 결정장애시구먼요. 제가 추천해드릴게요', '밥먹어요!! 추천해줄게요', '뭐드시고 싶어요?? 추천해줄게욤']; | ||
198 | + var rand = Math.floor(Math.random() * recom.length); | ||
199 | + sendTextMessage(senderId, recom[rand]); | ||
200 | + sendTextMessage(senderId, "가격대를 입력하시겠어요? :)"); | ||
65 | } | 201 | } |
66 | 202 | ||
67 | function receivedPostback(event) { | 203 | function receivedPostback(event) { |
... | @@ -81,11 +217,17 @@ function receivedPostback(event) { | ... | @@ -81,11 +217,17 @@ function receivedPostback(event) { |
81 | function sendTextMessage(recipientId, message) { | 217 | function sendTextMessage(recipientId, message) { |
82 | request({ | 218 | request({ |
83 | url: 'https://graph.facebook.com/v2.6/me/messages', | 219 | url: 'https://graph.facebook.com/v2.6/me/messages', |
84 | - qs: { access_token: PAGE_ACCESS_TOKEN }, | 220 | + qs: { |
221 | + access_token: PAGE_ACCESS_TOKEN | ||
222 | + }, | ||
85 | method: 'POST', | 223 | method: 'POST', |
86 | json: { | 224 | json: { |
87 | - recipient: { id: recipientId }, | 225 | + recipient: { |
88 | - message: { text: message } | 226 | + id: recipientId |
227 | + }, | ||
228 | + message: { | ||
229 | + text: message | ||
230 | + } | ||
89 | } | 231 | } |
90 | }, (error, response, body) => { | 232 | }, (error, response, body) => { |
91 | if (error) { | 233 | if (error) { | ... | ... |
-
Please register or login to post a comment