处理来自 facebook 的 link/referral 次中断(Bot Framework v4)

Handlling link/referral interruptions from facebook (Bot Framework v4)

考虑这种情况:

  1. 因此,当用户单击带有推荐参数的 m.me link 时,它会重定向到 Messenger 机器人,显示一些欢迎消息,然后通过 ConfirmPrompt 询问用户是否要继续.
  2. 用户没有点击 yes/no 但是 重新点击带有引用参数的 link 会导致错误,因为(我不不知道如何捕获此错误),错误在 onTurn 级别。

哪位好心人可以解释一下这种 activity 发生了什么以及如何正确处理它(通过重新启动我的整个对话框以获取引用参数)

谢谢! PS。我为我的机器人使用 NodeJs SDK,(机器人框架 V4)

已编辑:(感谢您很好地询问代码示例以充分理解场景,我还将添加一些屏幕截图以可视化问题)

因此这段代码位于 ReferralDialog 中专门要求用户确认的瀑布阶段之一。

await step.context.sendActivity("Doesn’t that sound easy?");
await step.context.sendActivity({ type: 'typing'});
return await step.prompt(CONFIRM_PROMPT, 'Now, would you like to apply?', ['yes', 'no']);

瀑布的下一阶段包含以下片段代码:

if(step.result)
        {
            await step.context.sendActivity("And to protect your privacy from prying eyes, I will bring you outside of messenger to a secure East West owned chat environment");


           return await step.endDialog();
        }
        else
        {
            await step.context.sendActivity("Thank you ! If you change your mind, please click the link your referrer sent you again to re-enter the program.");
            return await step.endDialog();
        }

在主对话框中(片段): //通过fb但与ref(已经互动)

else if(channelData &&  channelData.referral && channelData.referral.ref && stepContext.context.activity.channelId === 'facebook')
    {
return await stepContext.beginDialog(REFERRAL_DIALOG, userData);}

所以假设是:如果用户说点击 yes/no,就会有一条消息:(但一些疯狂的用户再次点击引荐 link)

所以我们现在有这个错误:

Error Message

第二张和第三张图片用于机器人的响应: Messenger prompt Messenger error

错误信息,发生在index.js有这样的代码: (它是从框架中预设的,我从来没有 change/do 那个代码上的东西)

// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
    // This check writes out errors to console log .vs. app insights.
    // NOTE: In production environment, you should consider logging this to Azure
    //       application insights.
    console.error(`\n [onTurnError] unhandled error: ${ error }`);

    // Send a trace activity, which will be displayed in Bot Framework Emulator
    await context.sendTraceActivity(
        'OnTurnError Trace',
        `${ error }`,
        'https://www.botframework.com/schemas/error',
        'TurnError'
    );

    // Send a message to the user
    let onTurnErrorMessage = 'The bot encounted an error or bug.';
    await context.sendActivity(onTurnErrorMessage, onTurnErrorMessage, InputHints.ExpectingInput);
    onTurnErrorMessage = 'To continue to run this bot, please fix the bot source code.';
    await context.sendActivity(onTurnErrorMessage, onTurnErrorMessage, InputHints.ExpectingInput);
    await context.sendActivity(error);
    // Clear out state
    await conversationState.delete(context);
};

我的机器人的唯一 toLowerCase 代码可以在 CancelAndHelpDialog 中找到(我也没有定义但添加了一些东西:

class CancelAndHelpDialog extends ComponentDialog {
    async onContinueDialog(innerDc) {
        const result = await this.interrupt(innerDc);
        if (result) {

            console.log("Bot was interrupted");
            return result;
        }
        return await super.onContinueDialog(innerDc);
    }

    async interrupt(innerDc) {
        console.log("Interrupt");
        let interruptData=innerDc.context.activity;
        if (interruptData && interruptData.text) {
            console.log("uhhhhhhhhhmmm" + typeof innerDc.context.activity.text);
                const text = innerDc.context.activity.text.toLowerCase();

                switch (text) {
                case 'help':
                case '?': {
                    const helpMessageText = 'Call our customer service 8888-1700 for further assistance.';
                    await innerDc.context.sendActivity(helpMessageText, helpMessageText, InputHints.ExpectingInput);
                    return { status: DialogTurnStatus.waiting };
                }
                case 'cancel':
                case 'quit': {
                    const cancelMessageText = 'Cancelling...';
                    await innerDc.context.sendActivity(cancelMessageText, cancelMessageText, InputHints.IgnoringInput);
                    return await innerDc.cancelAllDialogs();
                }
            }
        }
    }
}

我在 CancelAndHelpDialog 中添加的所有 console.log() 都不会显示,这真的很奇怪,更重要的是(重申一下)除了那个对话框我没有任何其他 toLowerCase 代码。

谢谢!

编辑: 由于问题是它对用户来说是一个错误,我找到了一种方法来更改 onContinueDialog 并重新提示用户重新单击 link。呵呵

async onContinueDialog(innerDc) {
        console.log("This is at oncontinue Dialog");
        console.log("\n\r This is the channel Data" + JSON.stringify(innerDc.context.activity.channelData));

        let interruptData=innerDc.context.activity;
        if(interruptData.channelData.referral && interruptData.channelData.referral.ref)
        {
            await innerDc.context.sendActivity("Sorry I didn't catch it. Kindly click the link again.");   
            return await innerDc.cancelAllDialogs();
        }
        else
        {
            const result = await this.interrupt(innerDc);
            console.log("The result: " + JSON.stringify(result));
            if (result) {

                console.log("Bot was interrupted");
                return result;
            }
            return await super.onContinueDialog(innerDc);
        }
    }

以这种方式,用户被迫重新单击 link 并重新启动所有对话框。

之前我的机器人使用的是 Bot Framework 4.7.2,其中包含 Facebook 频道的错误。升级到 4.8.0 将摆脱错误“TypeError: Cannot read 属性 'toLowerCase' of undefined”。