Merge branch 'master' of http://khuhub.khu.ac.kr/2016104172/LINEBOT
Showing
3 changed files
with
229 additions
and
70 deletions
| 1 | -var express = require("express"); | 1 | +'use strict'; |
| 2 | -var request = require("request"); | 2 | +let express = require("express"), |
| 3 | -var bodyParser = require("body-parser"); | 3 | + bodyParser= require("body-parser"), |
| 4 | + app = express(), | ||
| 5 | + request = require('request'), | ||
| 6 | + config = require('config'), | ||
| 7 | + images = require("./pics"); | ||
| 4 | 8 | ||
| 5 | -var app = express(); | 9 | +app.use(bodyParser.urlencoded({ extended : false})); |
| 6 | -app.use(bodyParser.urlencoded({extended: false})); | ||
| 7 | app.use(bodyParser.json()); | 10 | app.use(bodyParser.json()); |
| 8 | -app.listen((process.env.PORT || 5000)); | ||
| 9 | 11 | ||
| 10 | -// Server index page | 12 | +let users = {}; |
| 11 | -app.get("/", function (req, res) { | ||
| 12 | - res.send("Deployed!"); | ||
| 13 | -}); | ||
| 14 | 13 | ||
| 15 | -// Facebook Webhook | 14 | +app.listen(process.env.PORT || 8989, () => console.log('Example app listening on por 8989!')); |
| 16 | -// Used for verification | 15 | + |
| 17 | -app.get("/webhook", function (req, res) { | 16 | +app.get('/', (req, res) => res.send('Hello World!')); |
| 18 | - if (req.query["hub.verify_token"] === "process.env.VERIFICATION_TOKEN") { | 17 | + |
| 19 | - console.log("Verified webhook"); | 18 | + |
| 20 | - res.status(200).send(req.query["hub.challenge"]); | 19 | +// Adds support for GET requests to our webhook |
| 21 | - } else { | 20 | +app.get('/webhook', (req, res) => { |
| 22 | - console.error("Verification failed. The tokens do not match."); | 21 | + |
| 23 | - res.sendStatus(403); | 22 | + // Your verify token, Should be a random string. |
| 23 | + let VERIFY_TOKEN = "2016104171"; | ||
| 24 | + | ||
| 25 | + // Parse the query params | ||
| 26 | + let mode = req.query['hub.mode']; | ||
| 27 | + let token = req.query['hub.verify_token']; | ||
| 28 | + let challenge = req.query['hub.challenge']; | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + // Checks if a token and mode is in the query string of the request | ||
| 32 | + if(mode && token) { | ||
| 33 | + | ||
| 34 | + // Checks the mode and token sent is correcct | ||
| 35 | + if (mode === 'subscribe' && token === VERIFY_TOKEN) { | ||
| 36 | + | ||
| 37 | + // Responds with the challenge token from the request | ||
| 38 | + console.log('WEBHOOK_VERIFIED'); | ||
| 39 | + res.status(200).send(challenge); | ||
| 40 | + | ||
| 41 | + } else { | ||
| 42 | + // Responds with '403 Forbidden' if verify tokens do not match | ||
| 43 | + res.sendStatus(403); | ||
| 44 | + } | ||
| 24 | } | 45 | } |
| 25 | }); | 46 | }); |
| 26 | 47 | ||
| 27 | -// All callbacks for Messenger will be POST-ed here | 48 | +// Creates the endpoint for our webhook |
| 28 | -app.post("/webhook", function (req, res) { | 49 | +app.post('/webhook', (req, res) => { |
| 29 | - // Make sure this is a page subscription | 50 | + |
| 30 | - if (req.body.object == "page") { | 51 | + let body = req.body; |
| 31 | - // Iterate over each entry | 52 | + |
| 32 | - // There may be multiple entries if batched | 53 | + if (body.object === 'page') { |
| 33 | - req.body.entry.forEach(function(entry) { | 54 | + |
| 34 | - // Iterate over each messaging event | 55 | + // Iterates over each entry - there may be multiple if batched |
| 35 | - entry.messaging.forEach(function(event) { | 56 | + body.entry.forEach(function(entry) { |
| 36 | - if (event.postback) { | 57 | + |
| 37 | - processPostback(event); | 58 | + // Gets the message. entry.messaging is an array, but |
| 38 | - } | 59 | + // will only ever contain one message, so we get index 0 |
| 39 | - }); | 60 | + let webhook_event = entry.messaging[0]; |
| 61 | + console.log(webhook_event); | ||
| 62 | + | ||
| 63 | + // Get the sender PSID | ||
| 64 | + let sender_psid = webhook_event.sender.id; | ||
| 65 | + console.log('Sender PSID: ' + sender_psid); | ||
| 66 | + | ||
| 67 | + // Check if the event is a message or postback and | ||
| 68 | + // pass the event to the appropriate handler function | ||
| 69 | + if (webhook_event.message) { | ||
| 70 | + handleMessage(sender_psid, webhook_event.message); | ||
| 71 | + } else if (webhook_event.postback) { | ||
| 72 | + handlePostback(sender_psid, webhook_event.postback); | ||
| 73 | + } | ||
| 40 | }); | 74 | }); |
| 41 | 75 | ||
| 42 | - res.sendStatus(200); | 76 | + // Returns a '200 OK' response to all requests |
| 77 | + res.status(200).send('EVENT_RECEIVED'); | ||
| 78 | + } else { | ||
| 79 | + // Returns a '404 Not Found' if event is not from a page subscription | ||
| 80 | + res.sendStatus(404); | ||
| 43 | } | 81 | } |
| 82 | + | ||
| 44 | }); | 83 | }); |
| 45 | 84 | ||
| 46 | -function processPostback(event) { | 85 | +// Views - handle Message, handle Postback |
| 47 | - var senderId = event.sender.id; | 86 | + |
| 48 | - var payload = event.postback.payload; | 87 | +// Handles message events |
| 49 | - | 88 | +const handleMessage = (sender_psid, received_message) => { |
| 50 | - if (payload === "Greeting") { | 89 | + let response; |
| 51 | - // Get user's first name from the User Profile API | 90 | + |
| 52 | - // and include it in the greeting | 91 | + if(received_message.text){ |
| 53 | - request({ | 92 | + |
| 54 | - url: "https://graph.facebook.com/v2.6/" + senderId, | 93 | + // Create the payload for a basic text message |
| 55 | - qs: { | 94 | + response = askTemplate() |
| 56 | - access_token: process.env.PAGE_ACCESS_TOKEN, | 95 | + } |
| 57 | - fields: "first_name" | 96 | + |
| 58 | - }, | 97 | + // Sends the reponse message |
| 59 | - method: "GET" | 98 | + callSendAPI(sender_psid, response; |
| 60 | - }, function(error, response, body) { | 99 | +} |
| 61 | - var greeting = ""; | 100 | + |
| 62 | - if (error) { | 101 | +const handlePostback = (sender_psid, received_postback) => { |
| 63 | - console.log("Error getting user's name: " + error); | 102 | + let response; |
| 64 | - } else { | 103 | + |
| 65 | - var bodyObj = JSON.parse(body); | 104 | + // Get the payload for the postback |
| 66 | - name = bodyObj.first_name; | 105 | + let payload = received_postback.payload; |
| 67 | - greeting = "Hi " + name + ". "; | 106 | + |
| 68 | - } | 107 | + // Set the response based on the postback payload |
| 69 | - var message = greeting + "My name is SP Movie Bot. I can tell you various details regarding movies. What movie would you like to know about?"; | 108 | + if (payload === 'CAT_PICS') { |
| 70 | - sendMessage(senderId, {text: message}); | 109 | + response = imageTemplate('cats', sender_psid); |
| 110 | + callSendAPI(sender_psid, response, function(){ | ||
| 111 | + callSendAPI(sender_psid, askTemplate('Show me more')); | ||
| 71 | }); | 112 | }); |
| 113 | + } else if (payload === 'DOG_PICS') { | ||
| 114 | + response = imageTemplate('dogs', sender_psid); | ||
| 115 | + callSendAPI(sender_psid, response, function(){ | ||
| 116 | + callSendAPI(sender_psid, askTemplate('Show me more')); | ||
| 117 | + }); | ||
| 118 | + } else if(payload === 'GET_STARTED'){ | ||
| 119 | + response = askTemplate('Are you a Cat or Dog Person?'); | ||
| 120 | + callSendAPI(sender_psid, response); | ||
| 72 | } | 121 | } |
| 122 | + // Send the message to acknowledge the postback | ||
| 73 | } | 123 | } |
| 74 | 124 | ||
| 75 | -// sends message to user | 125 | +const askTemplate = (text) => { |
| 76 | -function sendMessage(recipientId, message) { | 126 | + return { |
| 77 | - request({ | 127 | + "attachment":{ |
| 78 | - url: "https://graph.facebook.com/v2.6/me/messages", | 128 | + "type":"template", |
| 79 | - qs: {access_token: process.env.PAGE_ACCESS_TOKEN}, | 129 | + "payload":{ |
| 80 | - method: "POST", | 130 | + "template_type":"button", |
| 81 | - json: { | 131 | + "text": text, |
| 82 | - recipient: {id: recipientId}, | 132 | + "buttons":[ |
| 83 | - message: message, | 133 | + { |
| 134 | + "type":"postback", | ||
| 135 | + "title":"Cats", | ||
| 136 | + "payload":"CAT_PICS" | ||
| 137 | + }, | ||
| 138 | + { | ||
| 139 | + "type":"postback", | ||
| 140 | + "title":"Dogs", | ||
| 141 | + "payload":"DOG_PICS" | ||
| 142 | + } | ||
| 143 | + ] | ||
| 144 | + } | ||
| 84 | } | 145 | } |
| 85 | - }, function(error, response, body) { | 146 | + } |
| 86 | - if (error) { | 147 | +} |
| 87 | - console.log("Error sending message: " + response.error); | 148 | + |
| 149 | +// Sends response messages via the Send API | ||
| 150 | +const callSendAPI = (sender_psid, response, cb = null) => { | ||
| 151 | + // Construct the message body | ||
| 152 | + let request_body = { | ||
| 153 | + "recipient": { | ||
| 154 | + "id": sender_psid | ||
| 155 | + }, | ||
| 156 | + "message": response | ||
| 157 | + }; | ||
| 158 | + | ||
| 159 | + // Send the HTTP request to the Messenger Platform | ||
| 160 | + request({ | ||
| 161 | + "uri": "https://graph.facebook.com/v2.6/me/messages", | ||
| 162 | + "qs": { "access_token": config.get('facebook.page.access_token') }, | ||
| 163 | + "method": "POST", | ||
| 164 | + "json": request_body | ||
| 165 | + }, (err, res, body) => { | ||
| 166 | + if (!err) { | ||
| 167 | + if(cb){ | ||
| 168 | + cb(); | ||
| 169 | + } | ||
| 170 | + } else { | ||
| 171 | + console.error("Unable to send message:" + err); | ||
| 88 | } | 172 | } |
| 89 | }); | 173 | }); |
| 90 | } | 174 | } |
| 175 | + | ||
| 176 | +const imageTemplate= (type, sender_id) => { | ||
| 177 | + return { | ||
| 178 | + "attachment":{ | ||
| 179 | + "type":"image", | ||
| 180 | + "payload":{ | ||
| 181 | + "url": getImage(type, sender_id), | ||
| 182 | + "is_reusable":true | ||
| 183 | + } | ||
| 184 | + } | ||
| 185 | + } | ||
| 186 | +} | ||
| 187 | + | ||
| 188 | +let users = {}; | ||
| 189 | + | ||
| 190 | +const getImage= (type, sender_id) => { | ||
| 191 | + // create user if doesn't exist | ||
| 192 | + if(users[sender_id] === undefined){ | ||
| 193 | + users = Object.assign({ | ||
| 194 | + [sender_id] : { | ||
| 195 | + 'cats_count' : 0, | ||
| 196 | + 'dogs_count' : 0 | ||
| 197 | + } | ||
| 198 | + }, users); | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + let count = images[type].length, // total available images by type | ||
| 202 | + user = users[sender_id], // // user requesting image | ||
| 203 | + user_type_count = user[type+'_count']; | ||
| 204 | + | ||
| 205 | + | ||
| 206 | + // update user before returning image | ||
| 207 | + let updated_user = { | ||
| 208 | + [sender_id] : Object.assign(user, { | ||
| 209 | + [type+'_count'] : count === user_type_count + 1 ? 0 : user_type_count + 1 | ||
| 210 | + }) | ||
| 211 | + }; | ||
| 212 | + // update users | ||
| 213 | + users = Object.assign(users, updated_user); | ||
| 214 | + | ||
| 215 | + console.log(users); | ||
| 216 | + return images[type][user_type_count]; | ||
| 217 | +} | ... | ... |
pics.js
0 → 100644
| 1 | +module.exports = { | ||
| 2 | + cats : [ | ||
| 3 | + 'https://i.imgur.com/Qbg7CeM.jpg', | ||
| 4 | + 'https://i.imgur.com/nUzkpJY.jpg', | ||
| 5 | + 'https://i.imgur.com/NpDcKph.jpg', | ||
| 6 | + 'https://i.imgur.com/oJtSDaO.jpg', | ||
| 7 | + 'https://i.redd.it/82ajpsrd17111.jpg', | ||
| 8 | + 'https://i.redd.it/00km1d2rt0111.jpg', | ||
| 9 | + 'https://i.redd.it/rdbavhp0y7111.jpg', | ||
| 10 | + 'https://i.redd.it/5hn3mg0n98111.jpg', | ||
| 11 | + 'https://i.redd.it/d23pb8mta6111.jpg', | ||
| 12 | + 'https://i.redd.it/d2gyrwgy7oz01.jpg', | ||
| 13 | + 'https://i.redd.it/z4sgl84q72z01.jpg', | ||
| 14 | + 'https://i.redd.it/wvykzo8n1cy01.jpg' | ||
| 15 | + ], | ||
| 16 | + | ||
| 17 | + dogs : [ | ||
| 18 | + 'https://i.redd.it/6tjihi2qe7111.jpg', | ||
| 19 | + 'https://i.imgur.com/etRCs56.jpg', | ||
| 20 | + 'https://i.redd.it/nibw50f8y4111.jpg', | ||
| 21 | + 'https://i.redd.it/izcvnvj1o7111.jpg', | ||
| 22 | + 'https://i.redd.it/eqs1g9dldz011.jpg', | ||
| 23 | + 'https://i.redd.it/civ9dnu9u1111.jpg', | ||
| 24 | + 'https://i.redd.it/kk03qwclkp011.jpg', | ||
| 25 | + 'https://i.redd.it/2694pupjne011.jpg', | ||
| 26 | + 'https://i.redd.it/qk49ls5y6oy01.jpg', | ||
| 27 | + 'https://i.imgur.com/oM3mKgB.jpg', | ||
| 28 | + 'https://i.redd.it/8kx2riaulux01.jpg' | ||
| 29 | + ] | ||
| 30 | +}; |
-
Please register or login to post a comment