dialogflow触发Web履行的后备

时间:2019-03-11 17:50:16

标签: dialogflow actions-on-google

我正在使用dialogflow和Google Actions构建一个应用程序。到目前为止,它运作良好,但是尝试在网络实现中触发后备时遇到了问题。

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

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++;
}

当通过dialogflow调用任何一个后备意图时,哪个效果很好。问题是我有这段代码:

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)开始的那一行,我希望以此触发后备意图。

这可能吗?如果是这样,怎么办?

2 个答案:

答案 0 :(得分:0)

从您的网络实现中,您无法触发任何其他意图。至此,Dialogflow已符合您的意图,并正在寻找针对此意图的响应。您可能想直接调用handleError函数,或者根据情况重新实现后备处理程序。

答案 1 :(得分:0)

是的。

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

希望有帮助。