Skip to content

Instantly share code, notes, and snippets.

@naoyashiga naoyashiga/index.js
Created Jan 1, 2017

Embed
What would you like to do?
Facebook Messenger Bot, aws lambdaでおうむ返しbot
const request = require('request');
const PAGE_ACCESS_TOKEN = "";
var options = {
hostname: "graph.facebook.com",
path: "/v2.6/me/messages?access_token=" + PAGE_ACCESS_TOKEN,
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
var data = event;
// 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(pageEntry) {
var pageID = pageEntry.id;
var timeOfEvent = pageEntry.time;
// Iterate over each messaging event
pageEntry.messaging.forEach(function(messagingEvent) {
if (messagingEvent.optin) {
// receivedAuthentication(messagingEvent);
} else if (messagingEvent.message) {
receivedMessage(messagingEvent);
} else if (messagingEvent.delivery) {
// receivedDeliveryConfirmation(messagingEvent);
} else if (messagingEvent.postback) {
// receivedPostback(messagingEvent);
} else if (messagingEvent.read) {
// receivedMessageRead(messagingEvent);
} else if (messagingEvent.account_linking) {
// receivedAccountLink(messagingEvent);
} else {
console.log("Webhook received unknown messagingEvent: ", messagingEvent);
}
});
});
}
}
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 isEcho = message.is_echo;
var messageId = message.mid;
var appId = message.app_id;
var metadata = message.metadata;
var messageText = message.text;
var messageAttachments = message.attachments;
var quickReply = message.quick_reply;
if (isEcho) {
console.log("Received echo for message %s and app %d with metadata %s",
messageId, appId, metadata);
return;
} else if (quickReply) {
var quickReplyPayload = quickReply.payload;
console.log("Quick reply for message %s with payload %s",
messageId, quickReplyPayload);
sendTextMessage(senderID, "Quick reply tapped");
return;
}
if (messageText) {
sendTextMessage(senderID, messageText);
} else if (messageAttachments) {
sendTextMessage(senderID, "Message with attachment received");
}
}
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;
if (messageId) {
console.log("Successfully sent message with id %s to recipient %s",
messageId, recipientId);
} else {
console.log("Successfully called Send API for recipient %s",
recipientId);
}
} else {
console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.