Closed constructor-igor closed 7 years ago
/**
* Responds to any HTTP request that can provide a "message" field in the body.
*
* @param {!Object} req Cloud Function request context.
* @param {!Object} res Cloud Function response context.
*/
const
request = require('request');
const PAGE_ACCESS_TOKEN = PAGE_ACCESS_TOKEN from FaceBook
const VERIFY_TOKEN = VERIFY_TOKEN from Facebook
exports.helloWorld = function helloWorld(req, res) {
if (req.method === 'GET') {
console.log('Received a verify request with token', req.query['hub.verify_token']);
if (req.query['hub.verify_token'] === VERIFY_TOKEN) {
return res.send(req.query['hub.challenge']);
}
return res.send('Error, wrong validation token');
}
var data = req.body;
if (data.object === 'page') {
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);
}
});
});
}
// const messagingEvents = flatten(req.body.entry.map(entry => entry.messaging));
// console.log(`Recieved ${messagingEvents.length} messages`);
res.sendStatus(200);
};
function receivedMessage0(event) {
// Putting a stub for now, we'll expand it in the following steps
console.log("Message data: ", event.message);
}
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, messageText);
}
} else if (messageAttachments) {
sendTextMessage(senderID, "Message with attachment received");
}
}
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: PAGE_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);
}
});
}
package.json
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"request": "^2.72.0"
}
}
function UserAction() {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "Your Rest URL Here", false);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send();
var response = JSON.parse(xhttp.responseText);
}
moved to https://github.com/constructor-igor/HebrewCalendarBots/issues/1
https://aws.amazon.com/blogs/compute/create-and-deploy-a-chat-bot-to-aws-lambda-in-five-minutes/ https://blog.alana.cloud/lets-code-a-serverless-chatbot-for-facebook-messenger-8551c3ee7e6 https://serverless.com/blog/building-a-facebook-messenger-chatbot-with-serverless/ https://github.com/GoogleCloudPlatform/cloud-functions-emulator
https://developers.facebook.com/docs/messenger-platform/guides/quick-start FB chat bot (java script) sample: https://github.com/constructor-igor/messenger-platform-samples