Showing
1 changed file
with
203 additions
and
61 deletions
| ... | @@ -3,97 +3,239 @@ const express = require('express'); | ... | @@ -3,97 +3,239 @@ 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) => { |
| 16 | - res.send('Hello world'); | 29 | + res.send('Hello world'); |
| 17 | }) | 30 | }) |
| 18 | 31 | ||
| 19 | // 페이스북 연결 | 32 | // 페이스북 연결 |
| 20 | app.get('/webhook', (req, res) => { | 33 | app.get('/webhook', (req, res) => { |
| 21 | - //if (req.query['hub.verify_token'] === 'VERIFY_TOKEN') { | 34 | + //if (req.query['hub.verify_token'] === 'VERIFY_TOKEN') { |
| 22 | - if (req.query['hub.verify_token'] === PAGE_ACCESS_TOKEN) { | 35 | + if (req.query['hub.verify_token'] === PAGE_ACCESS_TOKEN) { |
| 23 | - res.send(req.query['hub.challenge']); | 36 | + res.send(req.query['hub.challenge']); |
| 24 | - } | 37 | + } |
| 25 | - res.send('Error, wrong token'); | 38 | + res.send('Error, wrong token'); |
| 26 | }) | 39 | }) |
| 27 | 40 | ||
| 28 | app.post("/webhook", (req, res) => { | 41 | app.post("/webhook", (req, res) => { |
| 29 | - console.log("WEBHOOK GET IT WORKS"); | 42 | + console.log("WEBHOOK GET IT WORKS"); |
| 30 | - var data = req.body; | 43 | + var data = req.body; |
| 31 | - console.log(data); | 44 | + console.log(data); |
| 32 | - | 45 | + |
| 33 | - // Make sure this is a page subscription | 46 | + // Make sure this is a page subscription |
| 34 | - if (data.object == 'page') { | 47 | + if (data.object == 'page') { |
| 35 | - // Iterate over each entry | 48 | + // Iterate over each entry |
| 36 | - // There may be multiple if batched | 49 | + // There may be multiple if batched |
| 37 | - data.entry.forEach((pageEntry) => { | 50 | + data.entry.forEach((pageEntry) => { |
| 38 | - var pageID = pageEntry.id; | 51 | + var pageID = pageEntry.id; |
| 39 | - var timeOfEvent = pageEntry.time; | 52 | + var timeOfEvent = pageEntry.time; |
| 40 | - | 53 | + |
| 41 | - // Iterate over each messaging event | 54 | + // Iterate over each messaging event |
| 42 | - pageEntry.messaging.forEach((messagingEvent) => { | 55 | + pageEntry.messaging.forEach((messagingEvent) => { |
| 43 | - if (messagingEvent.optin) { | 56 | + if (messagingEvent.optin) { |
| 44 | - receivedAuthentication(messagingEvent); | 57 | + receivedAuthentication(messagingEvent); |
| 45 | - } else if (messagingEvent.message) { | 58 | + } else if (messagingEvent.message) { |
| 46 | - receivedMessage(messagingEvent); | 59 | + var quest = messagingEvent.message.text; |
| 47 | - } else if (messagingEvent.postback) { | 60 | + var price = quest; |
| 48 | - receivedPostback(messagingEvent); | 61 | + if (((quest.search('음식') != -1) || (quest.search('추천') != -1)) || ((quest.search('뭐') != -1) && (quest.search('먹') != -1))) { |
| 49 | - } else { | 62 | + receivedMessage_price(messagingEvent); |
| 50 | - console.log("Webhook received unknown messagingEvent: ", messagingEvent); | 63 | + } else if (price == Number(price)) { |
| 51 | - } | 64 | + price = Number(price); |
| 52 | - }); | 65 | + sendTextMessage(messagingEvent.sender.id, '원하시는 음식 카테고리가 있나요?'); |
| 53 | - }); | 66 | + } else if ((quest.search('한식') != -1) || (quest.search('중식') != -1) || (quest.search('일식') != -1) || (quest.search('양식') != -1)) { |
| 54 | - | 67 | + if ((quest.search('한식') != -1)) |
| 55 | - res.sendStatus(200); | 68 | + kinds.push('한식'); |
| 56 | - } | 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 { | ||
| 89 | + receivedMessage(messagingEvent); | ||
| 90 | + } | ||
| 91 | + } else if (messagingEvent.postback) { | ||
| 92 | + receivedPostback(messagingEvent); | ||
| 93 | + } else { | ||
| 94 | + console.log("Webhook received unknown messagingEvent: ", messagingEvent); | ||
| 95 | + } | ||
| 96 | + }); | ||
| 97 | + }); | ||
| 98 | + res.sendStatus(200); | ||
| 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) { |
| 68 | - console.log("RECEIVED POSTBACK IT WORKS"); | 204 | + console.log("RECEIVED POSTBACK IT WORKS"); |
| 69 | - var senderID = event.sender.id; | 205 | + var senderID = event.sender.id; |
| 70 | - var recipientID = event.recipient.id; | 206 | + var recipientID = event.recipient.id; |
| 71 | - var timeOfPostback = event.timestamp; | 207 | + var timeOfPostback = event.timestamp; |
| 72 | 208 | ||
| 73 | - var payload = event.postback.payload; | 209 | + var payload = event.postback.payload; |
| 74 | 210 | ||
| 75 | - console.log("Received postback for user %d and page %d with payload '%s' " + | 211 | + console.log("Received postback for user %d and page %d with payload '%s' " + |
| 76 | - "at %d", senderID, recipientID, payload, timeOfPostback); | 212 | + "at %d", senderID, recipientID, payload, timeOfPostback); |
| 77 | 213 | ||
| 78 | - sendTextMessage(senderID, "Postback called"); | 214 | + sendTextMessage(senderID, "Postback called"); |
| 79 | } | 215 | } |
| 80 | 216 | ||
| 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: { |
| 85 | - method: 'POST', | 221 | + access_token: PAGE_ACCESS_TOKEN |
| 86 | - json: { | 222 | + }, |
| 87 | - recipient: { id: recipientId }, | 223 | + method: 'POST', |
| 88 | - message: { text: message } | 224 | + json: { |
| 89 | - } | 225 | + recipient: { |
| 90 | - }, (error, response, body) => { | 226 | + id: recipientId |
| 91 | - if (error) { | 227 | + }, |
| 92 | - console.log('Error sending message: ' + response.error); | 228 | + message: { |
| 93 | - } | 229 | + text: message |
| 94 | - }); | 230 | + } |
| 231 | + } | ||
| 232 | + }, (error, response, body) => { | ||
| 233 | + if (error) { | ||
| 234 | + console.log('Error sending message: ' + response.error); | ||
| 235 | + } | ||
| 236 | + }); | ||
| 95 | } | 237 | } |
| 96 | 238 | ||
| 97 | app.listen(app.get('port'), () => { | 239 | app.listen(app.get('port'), () => { |
| 98 | - console.log('running on port', app.get('port')); | 240 | + console.log('running on port', app.get('port')); |
| 99 | }) | 241 | }) | ... | ... |
-
Please register or login to post a comment