Last active
August 3, 2017 02:36
-
-
Save lbrenman/4c966267ec659b4d60d6c8f45c197e1f to your computer and use it in GitHub Desktop.
API Builder Facebook Chat Bot (Echo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Arrow = require('arrow'); | |
var fbwebhook = Arrow.API.extend({ | |
group: 'webhooks', | |
path: '/api/fbwebhook', | |
method: 'GET', | |
description: 'this is an api that shows how to handle requests from the FB chatbot', | |
parameters: { | |
'hub.mode': { | |
description: 'hub.mode', | |
optional: true | |
}, | |
'hub.challenge': { | |
description: 'hub.challenge', | |
optional: true | |
}, | |
'hub.verify_token': { | |
description: 'hub.verify_token', | |
optional: true | |
} | |
}, | |
action: function(req, resp, next) { | |
console.log('fbwebhook - fbwebhook API called (GET)'); | |
if (req.query['hub.mode'] === 'subscribe' && | |
req.query['hub.verify_token'] === '<MY VERIFY TOKEN>') { | |
console.log("Validating webhook"); | |
resp.response.status(200); | |
resp.send(req.query['hub.challenge']); | |
} else { | |
console.error("Failed validation. Make sure the validation tokens match."); | |
resp.response.status(403); | |
} | |
next(); | |
} | |
}); | |
module.exports = fbwebhook; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Arrow = require('arrow'); | |
var request = require('request'); | |
function sendGenericMessage(recipientId, messageText) { | |
// To be expanded in later sections | |
} | |
function sendTextMessage(recipientId, messageText) { | |
var messageData = { | |
recipient: { | |
id: recipientId | |
}, | |
message: { | |
text: messageText | |
} | |
}; | |
callSendAPI(messageData); | |
} | |
function callSendAPI(messageData) { | |
request({ | |
uri: 'https://graph.facebook.com/v2.6/me/messages', | |
qs: { access_token: '<MY ACCESS TOKEN>' }, | |
method: 'POST', | |
json: messageData | |
}, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var recipientId = body.recipient_id; | |
var messageId = body.message_id; | |
console.log("Successfully sent generic message with id %s to recipient %s", | |
messageId, recipientId); | |
} else { | |
console.error("Unable to send message."); | |
console.error(response); | |
console.error(error); | |
} | |
}); | |
} | |
function receivedMessage(event) { | |
var senderID = event.sender.id; | |
var recipientID = event.recipient.id; | |
var timeOfMessage = event.timestamp; | |
var message = event.message; | |
console.log("Received message for user %d and page %d at %d with message:", | |
senderID, recipientID, timeOfMessage); | |
console.log(JSON.stringify(message)); | |
var messageId = message.mid; | |
var messageText = message.text; | |
var messageAttachments = message.attachments; | |
if (messageText) { | |
// If we receive a text message, check to see if it matches a keyword | |
// and send back the example. Otherwise, just echo the text we received. | |
switch (messageText) { | |
case 'generic': | |
sendGenericMessage(senderID); | |
break; | |
default: | |
sendTextMessage(senderID, "The Chatbot detected that you wrote: "+messageText); | |
} | |
} else if (messageAttachments) { | |
sendTextMessage(senderID, "Message with attachment received"); | |
} | |
} | |
var fbwebhookp = Arrow.API.extend({ | |
group: 'webhooks', | |
path: '/api/fbwebhook', | |
method: 'POST', | |
description: 'this is an api that shows how to handle requests from the FB chatbot', | |
parameters: { | |
'object': { | |
description: 'object', | |
optional: true | |
}, | |
'entry': { | |
description: 'entry', | |
optional: true | |
} | |
}, | |
action: function(req, resp, next) { | |
console.log('fbwebhook - fbwebhook API called (POST)'); | |
var data = req.body; | |
// Make sure this is a page subscription | |
if (data.object === 'page') { | |
// Iterate over each entry - there may be multiple if batched | |
data.entry.forEach(function(entry) { | |
var pageID = entry.id; | |
var timeOfEvent = entry.time; | |
// Iterate over each messaging event | |
entry.messaging.forEach(function(event) { | |
if (event.message) { | |
receivedMessage(event); | |
} else { | |
console.log("Webhook received unknown event: ", event); | |
} | |
}); | |
}); | |
// Assume all went well. | |
// | |
// You must send back a 200, within 20 seconds, to let us know | |
// you've successfully received the callback. Otherwise, the request | |
// will time out and we will keep trying to resend. | |
resp.response.status(200); | |
next(); | |
} else { | |
resp.response.status(200); | |
next(); | |
} | |
} | |
}); | |
module.exports = fbwebhookp; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment