Created
August 27, 2017 07:58
-
-
Save streetsmartdev/2adcb6f58feab2852888167a27ef1ccd to your computer and use it in GitHub Desktop.
CB-SVRLESS-3 - Replying to messages
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
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