首页 > 解决方案 > 错误:WebhookClient.handleRequest 处没有请求意图的处理程序

问题描述

调用云函数的默认意图会出错

Error: No handler for requested intent
at WebhookClient.handleRequest (/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:287:29)
at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/user_code/index.js:73:11)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)
at /var/tmp/worker/worker.js:783:7
at /var/tmp/worker/worker.js:766:11
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)

正如我在诊断信息日志中的 webresponse 所示。

             {
              "responseId": "86043a10-8bc2-4ee7-8e8b-1e997289ad7c",
              "queryResult": {
                "queryText": "hi",
                "action": "input.welcome",
                "parameters": {},
                "allRequiredParamsPresent": true,
                "fulfillmentText": "Hi. Am Uma. Kindly let me know your experience facing an issue.",
                "fulfillmentMessages": [
                  {
                    "text": {
                      "text": [
                        "Hi. Am Uma and welcome to support. Kindly let me know your experience facing an issue."
                      ]
                    }
                  }
                ],
                "outputContexts": [
                  {
                    "name": "projects/handymanticketagent/agent/sessions/e416a522-da87-ebd1-348e-9fdea1efbf65/contexts/defaultwelcomeintent-followup",
                    "lifespanCount": 2
                  }
                ],
                "intent": {
                  "name": "projects/handymanticketagent/agent/intents/c58f706f-6cb6-499d-9ce2-459e8054ddc1",
                  "displayName": "Default Welcome Intent"
                },
                "intentDetectionConfidence": 1,
                "diagnosticInfo": {
                  "webhook_latency_ms": 10001
                },
                "languageCode": "en"
              },
              "webhookStatus": {
                "code": 4,
                "message": "Webhook call failed. Error: Request timeout."
              }
            }

根据此处的堆栈溢出答案,添加了一个映射到函数的意图,但仍然出现错误并且可以进一步发展。云功能控制台在哪里以及如何说我的请求缺少处理程序?

在此处输入图像描述

在此处输入图像描述

更新:正如@prisoner 所说,包括我的云功能代码。

            'use strict';

            const functions = require('firebase-functions');
            const admin = require('firebase-admin');
            const { WebhookClient } = require('dialogflow-fulfillment');

            process.env.DEBUG = 'dialogflow:*'; // enables lib debugging statements
            admin.initializeApp(functions.config().firebase);
            const db = admin.firestore();

            exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
                console.log(request.body.queryResult.fulfillmentText);
                console.log(request);
                console.log(response);
                const agent = new WebhookClient({ request, response });
                console.log(agent);
                function writeToDb(agent) {
                    // Get parameter from Dialogflow with the string to add to the database
                    const databaseEntry = agent.parameters.databaseEntry;
                    console.log(databaseEntry);
                    // Get the database collection 'dialogflow' and document 'agent' and store
                    // the document  {entry: "<value of database entry>"} in the 'agent' document
                    const dialogflowAgentRef = db.collection('dialogflow').doc('agent');
                    console.log(dialogflowAgentRef);
                    return db.runTransaction(t => {

                        t.set(dialogflowAgentRef, { entry: databaseEntry });
                        console.log(Promise.resolve('Write complete'));
                        return Promise.resolve('Write complete');
                    }).then(doc => {
                        agent.add('Wrote "${databaseEntry}" to the Firestore database.');
                        return null;
                        }).catch(err => {
                            if (err) {
                                console.log(err.stack);
                            }
                        console.log('Error writing to Firestore: ${err}');
                        agent.add('Failed to write "${databaseEntry}" to the Firestore database.');

                    });
                }

                function readFromDb(agent) {
                    console.log(agent);
                    // Get the database collection 'dialogflow' and document 'agent'
                    const dialogflowAgentDoc = db.collection('dialogflow').doc('agent');
                    console.log(dialogflowAgentDoc);
                    // Get the value of 'entry' in the document and send it to the user
                    return dialogflowAgentDoc.get()
                        .then(doc => {
                            if (!doc.exists) {
                                agent.add('No data found in the database!');
                            } else {
                                agent.add(doc.data().entry);
                            }
                            return Promise.resolve('Read complete');
                        }).catch(() => {
                            agent.add('Error reading entry from the Firestore database.');
                            agent.add('Please add a entry to the database first by saying, "Write <your phrase> to the database"');
                        });
                }
                function defaultwelcomeintent_function(agent) {
                    console.log(agent);
                } 
                // Map from Dialogflow intent names to functions to be run when the intent is matched
                let intentMap = new Map();
                intentMap.set('defaultwelcomeintent-followup', defaultwelcomeintent_function);
                intentMap.set('ReadFromFirestore', readFromDb);
                intentMap.set('WriteToFirestore', writeToDb);
                console.log(intentMap);
                agent.handleRequest(intentMap);
            });

标签: google-cloud-functionsdialogflow-esactions-on-google

解决方案


诊断信息表明该履行的意图的显示名称是“默认欢迎意图”:

"intent": {
    "name": "projects/handymanticketagent/agent/intents/c58f706f-6cb6-499d-9ce2-459e8054ddc1",
    "displayName": "Default Welcome Intent"
},

所以你需要像这样为它创建一个映射: intentMap.set('Default Welcome Intent', defaultwelcomeintent_function);

defaultwelcomeintent_function您在云函数中定义的处理程序在哪里。


推荐阅读