Skip to content

Instantly share code, notes, and snippets.

@streetsmartdev
Created August 27, 2017 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save streetsmartdev/2adcb6f58feab2852888167a27ef1ccd to your computer and use it in GitHub Desktop.
Save streetsmartdev/2adcb6f58feab2852888167a27ef1ccd to your computer and use it in GitHub Desktop.
CB-SVRLESS-3 - Replying to messages
module.exports.fbMessages = (event, context, callback) => {
event.body.entry.map((entry) => {
entry.messaging.map((messagingItem) => {
if (messagingItem.message && messagingItem.message.text) {
const url = `https://graph.facebook.com/v2.6/me/messages?access_token=<your access token here>`;
const responsePayload = {
recipient: {
id: messagingItem.sender.id
},
message: {
text: messagingItem.message.text
}
};
nodeFetch(
url,
{
method: 'POST',
body: JSON.stringify(responsePayload),
headers: {'Content-Type': 'application/json'}
}
)
.then(res => res.json())
.then(json => {
console.log("Json result ", json);
callback(null, json)
})
.catch(error => {
console.error("Call failed ", error);
callback(null, error)
});
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment