我试图发送一个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制造商对这个问题的回应。
这方面的步骤如下:
发布于 2019-07-01 18:58:30
Facebook事件与Bot框架
当Facebook发送一个事件给你的机器人时,它会发送一个Activity和一个Event ActivityType。对于某些事件,事件数据位于Activity.Value属性中。对于其他事件,如快速回复中的PostBack,活动将是Message,数据将位于Activity.ChannelData中。例如,您的bot可能会收到一个postBack事件,其形式如下:
{
channelId: 'facebook',
[...]
type: 'message',
channelData: {
recipient: {...},
sender: {...},
message: {
[...],
quick_reply: {
[...],
payload: '<your payload>'
}
}
}
}处理Facebook事件
这个答案将在很大程度上影响到Facebook事件示例。我强烈建议大家看一下,以寻求更多的帮助。
捕获消息和事件
首先,您希望使用onMessage()和onEvent()捕获facebook消息和事件
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);
});处理消息/事件
Facebook可以发送多种类型的事件。你可能想要使用if或switch语句处理每种类型
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;
}具体来说,处理一个PostBack
样本只是:
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);
}当您想将问题路由到QnA Maker时,您可以(使用QnA制作者样品作为指导):
async onFacebookPostback(turnContext, postback) {
// qnaMaker.getAnswers doesn't accept string input, so we need to adjust our turnContext
// to match what it expects, which is a string in Activity.Text
turnContext.activity.text = postback.payload;
const qnaResults = await this.qnaMaker.getAnswers(turnContext);
// If an answer was received from QnA Maker, send the answer back to the user.
if (qnaResults[0]) {
await turnContext.sendActivity(qnaResults[0].answer);
// If no answers were returned from QnA Maker, reply with help.
} else {
await turnContext.sendActivity('No QnA Maker answers were found.');
}
}https://stackoverflow.com/questions/56838329
复制相似问题