Facebook Events and the Bot Framework
When Facebook sends an event to your bot, it sends an Activity
with an Event
ActivityType. For some events, the event data is in the Activity.Value
property. For other events, like a PostBack from a Quick Reply, the activity will be a Message
and the data will be in Activity.ChannelData
. For example, your bot might receive a postBack event as an activity like this:
{
channelId: 'facebook',
[...]
type: 'message',
channelData: {
recipient: {...},
sender: {...},
message: {
[...],
quick_reply: {
[...],
payload: '<your payload>'
}
}
}
}
Handling Facebook Events
This answer is going to pull heavily from the Facebook Events Sample. I highly recommend looking at that for additional help.
Capture Messages and Events
First, you want to capture the facebook messages and events with onMessage()
and onEvent()
:
this.onMessage(async (turnContext) => {
console.log('Processing a Message Activity.');
if (!await this.processFacebookPayload(turnContext, turnContext.activity.channelData)) {
if (turnContext.activity.channelId !== 'facebook') {
await turnContext.sendActivity('This sample is intended to be used with a Facebook bot.');
}
await this.showChoices(turnContext);
}
});
this.onEvent(async (turnContext) => {
console.log('Processing an Event Activity.');
await this.processFacebookPayload(turnContext, turnContext.activity.value);
});
Process the Messages/Events
Facebook can send many types of events. You may want to use an if or switch statement to handle each type:
async processFacebookPayload(turnContext, data) {
const facebookPayload = data;
if (facebookPayload) {
if (facebookPayload.postback) {
await this.onFacebookPostback(turnContext, facebookPayload.postback);
return true;
} else if (facebookPayload.optin) {
await this.onFacebookOptin(turnContext, facebookPayload.optin);
return true;
[...]
}
return false;
}
Specifically, Handle a PostBack
The sample just does:
async onFacebookPostback(turnContext, postback) {
console.log('Postback message received.');
await turnContext.sendActivity('Are you sure?');
await this.showChoices(turnContext);
}
As you want to route the question to QnA Maker, you might (using the QnA Maker Sample as guidance):
async onFacebookPostback(turnContext, postback) {
turnContext.activity.text = postback.payload;
const qnaResults = await this.qnaMaker.getAnswers(turnContext);
if (qnaResults[0]) {
await turnContext.sendActivity(qnaResults[0].answer);
} else {
await turnContext.sendActivity('No QnA Maker answers were found.');
}
}
user clicks button > button calls bot with postback event > bot responds with QnA response
? I'm just not clear on what's "detecting the postBack Payload". If the bot already detects it, you don't need to forward anything else to the bot. Might be easiest if you edit your question with the additional details. – mdrichardsonactivity
object and making sure you get the right properties/values. – mdrichardson