我正在尝试向我的机器人发送一个postBack文本消息,但是我不知道正确的语法。
这是我的代码:
if (postback.payload == "WHAT_IS_MENTAL_HEALTH") {
await turnContext.sendActivity("TO-DO: Forward on 'What Is Mental Health?' to Bot Handler");
ActionTypes.postBack("What Is Mental Health?");
}
我正在尝试转发“什么是心理健康?”一文。到我的机器人,这样它就会撤回对该问题的QnA Maker响应。
为此,步骤如下:
答案 0 :(得分:1)
Facebook将事件发送给您的机器人时,它会发送一个HasSupportFragmentInjector
Activity
的ActivityType。对于某些事件,事件数据位于Activity.Value
属性中。对于其他事件,例如来自快速回复的回发,活动将为Event
,数据将位于Message
中。例如,您的机器人可能会通过以下活动收到一个postBack事件:
Activity.ChannelData
这个答案将大大地Facebook Events Sample。我强烈建议您寻求帮助。
首先,您要使用onMessage()
和onEvent()
捕获Facebook消息和事件:
{
channelId: 'facebook',
[...]
type: 'message',
channelData: {
recipient: {...},
sender: {...},
message: {
[...],
quick_reply: {
[...],
payload: '<your payload>'
}
}
}
}
Facebook可以发送许多类型的事件。您可能要use an if or switch statement to handle each type:
this.onMessage(async (turnContext) => {
console.log('Processing a Message Activity.');
// Show choices if the Facebook Payload from ChannelData is not handled
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.');
// Analyze Facebook payload from EventActivity.Value
await this.processFacebookPayload(turnContext, turnContext.activity.value);
});
该示例只是这样做:
async processFacebookPayload(turnContext, data) {
// At this point we know we are on Facebook channel, and can consume the Facebook custom payload present in channelData.
const facebookPayload = data;
if (facebookPayload) {
if (facebookPayload.postback) {
// Postback
await this.onFacebookPostback(turnContext, facebookPayload.postback);
return true;
} else if (facebookPayload.optin) {
// Optin
await this.onFacebookOptin(turnContext, facebookPayload.optin);
return true;
[...]
}
return false;
}
当您要将问题路由到QnA Maker时,可以(使用QnA Maker Sample作为指导):
async onFacebookPostback(turnContext, postback) {
console.log('Postback message received.');
// TODO: Your postBack handling logic here...
// Answer the postback and show choices
await turnContext.sendActivity('Are you sure?');
await this.showChoices(turnContext);
}