在瀑布对话期间避免Luis的干扰(BotFramework v4)

时间:2018-10-12 23:26:19

标签: node.js botframework luis

如何防止Luis在Bot Framework v4中触发意图?例如,当问用户问题/提示时,例如“您叫什么名字?”或“您的邮政编码是什么?”

在v3中,可以通过以下方式完成:

var recognizer = new builder.LuisRecognizer(url) 
    .onEnabled(function (context, callback) {
        // LUIS is only ON when there are no tasks pending(e.g. Prompt text) 
        var enabled = context.dialogStack().length === 0; 
        callback(null, enabled); 
    });

link

在v4中,这是我的识别器:

this.luisRecognizer = new LuisRecognizer({
            applicationId: luisConfig.appId,
            endpoint: luisConfig.getEndpoint(),
            endpointKey: luisConfig.authoringKey
        });

我想我需要将其创建为中间件,以检查是否存在对话框状态,然后禁用/重新启用Luis ...?

1 个答案:

答案 0 :(得分:0)

在主对话框(用于识别LUIS意图的对话框)中,检查您的上下文以查看是否存在任何活动对话框:

async onTurn(context) {
    // Create dialog context.
    const dc = await this.dialogs.createContext(context);
    // By checking the incoming Activity type, the bot only calls LUIS in appropriate cases.
    if (context.activity.type === ActivityTypes.Message) {
        // Normalizes all user's text inputs
        const utterance = context.activity.text.trim().toLowerCase();

        // handle conversation interrupts first
        const interrupted = await this.isTurnInterrupted(dc, utterance);
        if (!interrupted) {
            // Continue the current dialog
            const dialogResult = await dc.continueDialog();

            // If no one has responded,
            if (!dc.context.responded) {
                // Examine results from active dialog
                switch (dialogResult.status) {
                    case DialogTurnStatus.empty:
                        // Call to LUIS recognizer to get intent + entities
                        const results = await this.luisRecognizer.recognize(dc.context);
                        // Since the LuisRecognizer was configured to include the raw results, get the `topScoringIntent` as specified by LUIS.
                        const topIntent = results.luisResult.topScoringIntent;

                        switch (topIntent.intent) {
                            case SMALLTALK_INTENT:
                                return await dc.beginDialog(SmallTalkDialog.Name);
                            ...
                        }
                    case DialogTurnStatus.waiting:
                        // The active dialog is waiting for a response from the user, so do nothing
                        break;
                    case DialogTurnStatus.complete:
                        await dc.endDialog();
                        break;
                    default:
                        await dc.cancelAllDialogs();
                        break;
                }
            }
        }
    } else if (context.activity.type === ActivityTypes.ConversationUpdate &&
        context.activity.recipient.id !== context.activity.membersAdded[0].id) {
        // If the Activity is a ConversationUpdate, send a greeting message to the user.
        await context.sendActivity('¡Hola! ¿En qué puedo ayudarte? ');
    } else if (context.activity.type !== ActivityTypes.ConversationUpdate) {
        // Respond to all other Activity types.
        //await context.sendActivity(`[${ context.activity.type }]-type activity detected.`);
        //await this.authDialog.onTurn(context);
    }
    // Save state changes
    await this.conversationState.saveChanges(context);
  }

希望这会有所帮助。