【问题标题】:dialogflow trigger fallback from web fulfilment对话流触发 Web 实现的回退
【发布时间】:2019-08-02 02:35:57
【问题描述】:

我正在使用 dialogflow 和 actions-on-google 构建应用程序。到目前为止,它运行良好,但在我的网络履行中尝试触发后备时我遇到了问题。

这是我处理后备意图的代码:

const NO_INPUT_INTENT = 'actions_intent_NO_INPUT';
const DEFAULT_FALLBACK_INTENT = 'Default Fallback Intent';

app.intent(DEFAULT_FALLBACK_INTENT, handleError);
app.intent(NO_INPUT_INTENT, handleError);
app.fallback(handleError); // If no intent is specified

function handleError(conv) {
    let data = conv.data;
    let questions = data.questions;
    let answers = [];

    let state = getState(conv);

    switch (state) {
        case 'answers':
            let unansweredQuestions = questions.filter((item) => !item.active);

            if (!unansweredQuestions.length) {
                conv.close(`It doesn't look like I can help so let's stop here. Bye for now.`);
            } else {
                let question = unansweredQuestions[0];
                answers = question.answers.map((answer) => answer.text);

                conv.contexts.set(question.speechContext, 5);

                getErrorMessage(conv, {
                    first: 'Sorry, I did not get that. ' + question.speechText,
                    second:
                        `I still don't understand your response. You can try something like ` +
                        answers[0] +
                        `, ` +
                        answers[1] +
                        ` or ` +
                        answers[2] +
                        `.`,
                    third: `Please try one of the following: ` + answers.join(', ') + '.',
                    last: `It doesn't look like I can help so let's stop here. Bye for now.`
                });
            }
            break;
        case 'questions':
            answers = questions
                .filter((question) => question.speechContext && question.speechText)
                .map((question) => question.text);

            conv.contexts.set('questions', 5);

            getErrorMessage(conv, {
                first: 'Sorry, I did not get that. What is important to you?',
                second:
                    'Sorry, try saying something like ' + answers[0] + ', ' + answers[1] + ' or ' + answers[2] + '.',
                third: `Please try one of the following: ` + answers.join(', ') + '.',
                last: `It doesn't look like I can help so let's stop here. Bye for now.`
            });
            break;
        case 'scenario':
            let scenario = data.scenario;
            answers = scenario.answers.map((answer) => answer.text);

            conv.contexts.set('categoryintent-followup', 2);

            getErrorMessage(conv, {
                first: `Sorry, what are you most likely to use it for?'`,
                second:
                    'Sorry, try saying something like ' + answers[0] + ', ' + answers[1] + ' or ' + answers[2] + '.',
                third: `Please try one of the following: ` + answers.join(', ') + '.',
                last: `It doesn't look like I can help so let's stop here. Bye for now.`
            });
            break;
        default:
            conv.contexts.set('category', 5);

            getErrorMessage(conv, {
                first: 'Sorry, what type of product are you looking for?',
                second: 'We can help you choose a camera, a laptop, a TV, a tablet, a smartphone or headphones.',
                third: `Sorry that isn't something we can help you find at the moment. We can help you pick a new camera, laptop, TV, tablet, smartphone or headphones.`,
                last: `It doesn't look like I can help so let's stop here. Bye for now.`
            });
            break;
    }
}

let repromptCount = 0;

function getErrorMessage(conv, text) {
    switch (repromptCount) {
        case 0:
            conv.ask(text.first);
            break;
        case 1:
            conv.ask(text.second);
            break;
        case 2:
            conv.ask(text.third);
            break;
        default:
            conv.close(text.last);
            break;
    }
    repromptCount++;
}

当通过对话流调用任一后备意图时,这非常有用。问题是我有这段代码:

function getQuestions(conv, params) {
    let scenario = params.scenario;
    let data = conv.data;

    let answers = data.scenario.answers.filter((answer) => {
        if (answer.text === scenario) {
            return answer;
        }
    });

    if (answers.length !== 1) {
        // TODO: trigger fallback intent
        return;
    }

    let formulas = (conv.data.formulas = answers[0].formulas);
    let categoryId = data.categoryId;

    let options = {
        method: 'PUT',
        url: apiUrl + 'questions/filter',
        body: {
            categoryId: categoryId,
            organisationId: organisation,
            formulas: formulas
        },
        json: true
    };

    return request(options)
        .then((response) => {
            data.questions = response;
            data.scenario = answers[0];

            conv.ask('What is important to you?');
            createSuggestions(conv, response, 'text');
            repromptCount = 0; // Reset our fallback reprompts
        })
        .catch((error) => {
            //conv.ask(JSON.stringify(error));
            conv.close('We encountered an error. Please report this and try again later.');
        });
}

在这里,如果您查看以 if (answers.length !== 1) 开头的行,我希望这会触发回退意图。

这可能吗?如果可以,怎么做?

【问题讨论】:

    标签: dialogflow-es actions-on-google


    【解决方案1】:

    从您的网络履行中,您不能触发任何其他意图。至此,Dialogflow 已匹配您的意图,并正在寻找针对此意图的响应。您可能希望直接调用 handleError 函数或为您的条件重新实现回退处理程序。

    【讨论】:

      【解决方案2】:

      是的,这是可能的。

      您需要在对话框流控制台的后备意图中定义一个事件。
      然后只需在您的条件if (answers.length !== 1) 下在您的 webhook 中调用该事件。

      这里描述https://cloud.google.com/dialogflow/es/docs/events-custom#webhook

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2015-11-17
        • 1970-01-01
        • 2015-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-17
        • 2020-05-06
        相关资源
        最近更新 更多