在模拟器中输入 What's your name?。From fulfillment: My name is Dialogflow! 响应从新处理程序发送。
访问参数值
在前面的步骤中,您创建了 set-language 意图来标识人类和编程语言。该意图使用 language 和 language-programming 参数。在此部分中,您可以在 fulfillment 处理程序中访问这些参数的值。
如需添加此处理程序,请执行以下操作:
为 set-language 意图启用 fulfillment。
点击左侧边栏菜单中的 Fulfillment。
类似于上述步骤,添加以下处理程序和 intentMap 调用:
functionlanguageHandler(agent){constlanguage=agent.parameters.language;constprogrammingLanguage=agent.parameters['language-programming'];if(language){agent.add(`Fromfulfillment:Wow!Ididn't know you knew ${language}`);}elseif(programmingLanguage){agent.add(`Fromfulfillment:${programmingLanguage}iscool`);}else{agent.add(`Fromfulfillment:Whatlanguagedoyouknow?`);}}
intentMap.set('set-language', languageHandler);
点击部署。
该处理程序会检索参数 language 和 language-programming,并将其存储在变量中。它从 agent.parameters 特性中检索这些值,该特性是一个对象,包含最终用户在其请求中指定的所有参数名称和值。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[[["\u003cp\u003eThis guide provides instructions on integrating Dialogflow agents with external services using fulfillment, enabling actions based on user input and dynamic responses.\u003c/p\u003e\n"],["\u003cp\u003eThe inline editor in the Dialogflow Console allows for creating and deploying fulfillment code to Cloud Functions, suitable for simple testing and prototyping.\u003c/p\u003e\n"],["\u003cp\u003eTo use fulfillment, you must enable it for each specific intent, which then overrides the static responses configured in the intent settings.\u003c/p\u003e\n"],["\u003cp\u003eCustom fulfillment handlers can be created by defining functions that process user input and generating dynamic responses, and associating them with specific intents via an intent map.\u003c/p\u003e\n"],["\u003cp\u003eFulfillment handlers can access parameter values extracted from user input through the \u003ccode\u003eagent.parameters\u003c/code\u003e attribute, enabling personalized responses based on the user's specified details.\u003c/p\u003e\n"]]],[],null,["# Quickstart: Fulfillment\n\nThis guide shows you how to integrate your agent with your services using\n[fulfillment](/dialogflow/docs/fulfillment-overview).\nIntegrating your service allows you to take actions\nbased on end-user expressions and send dynamic responses back to the end-user.\n| **Note:** If you are using Google Assistant, check options on the [Google Assistant](/dialogflow/es/docs/integrations/aog) page.\n\nBefore you begin\n----------------\n\nIf you don't plan on using fulfillment, you can skip this quickstart.\n\nThese steps use\n[Cloud Functions](/functions/docs)\nand assume basic Node.js knowledge.\nIf you want to create fulfillment using your own server or another language,\nsee the more detailed [fulfillment documentation](/dialogflow/docs/fulfillment-overview).\n\nYou should do the following before reading this guide:\n\n1. Read [Dialogflow basics](/dialogflow/docs/basics).\n2. Perform [setup steps](/dialogflow/docs/quick/setup).\n3. Perform steps in the [Build an agent](/dialogflow/docs/quick/build-agent) quickstart guide. Steps below continue working on the same agent. If you no longer have that agent, you can download [`build-agent-quickstart.zip`](/static/dialogflow/es/docs/data/build-agent-quickstart.zip) and [import the file](/dialogflow/docs/agents-settings#export).\n\nThe inline editor\n-----------------\n\n\nThe Dialogflow Console has a built-in code editor, called the\n\n[inline editor](/dialogflow/docs/fulfillment-inline-editor)\n\nthat you can use to create fulfillment code and deploy the code to\n[Cloud Functions](/functions/docs).\nCloud Functions has associated charges,\nbut the service is available for no charge up to a\n[significant number of monthly invocations](/functions/pricing). Note that you still\nmust register and provide a valid billing account. Cancel anytime.\n\n\nWhen you initially enable the inline editor,\nNode.js fulfillment code is pre-populated with default handlers for\n[default intents](/dialogflow/docs/intents-default)\nthat are included for all agents.\nThe code also has commented instructions\nfor adding handlers for developer-defined intents.\n\n\nThe inline editor is intended for simple fulfillment testing and prototyping.\nOnce you are ready to build a production application,\nyou should create a\n[webhook service](/dialogflow/docs/fulfillment-webhook).\n\nEnable and deploy fulfillment with the Inline Editor\n----------------------------------------------------\n\nTo enable and deploy the default fulfillment code using the Inline Editor:\n\n1. Click **Fulfillment** in the left sidebar menu.\n2. Toggle the **Inline Editor** to **Enabled**.\n3. If you did not [enable billing in setup steps](/dialogflow/docs/quick/setup#billing), you are prompted to enable billing now. Cloud Functions has associated charges, but the service is available for no charge up to a [significant number of monthly invocations](/functions/pricing). Note that you still must register and provide a billing account. Cancel anytime.\n4. Click **Deploy** at the bottom of the form, and wait until dialogs indicate that it has been deployed.\n\nFor each intent that requires fulfillment,\nyou must enable fulfillment for the intent.\nTo enable fulfillment for the Default Welcome Intent:\n\n1. Click **Intents** in the left sidebar menu.\n2. Click **Default Welcome Intent**.\n3. Scroll down to the **Fulfillment** section\n and toggle **Enable webhook call for this intent** on.\n\n | **Note:** Enabling fulfillment overrides responses set in the **Responses** section of the intent. If fulfillment fails, the entries in the **Responses** section will be used.\n4.\n Click the **Save** button and wait until the **Agent Training**\n dialog indicates that training is complete.\n\nNow you can test your fulfillment in the simulator.\nEnter `Hi` in the simulator, and it responds with `Welcome to my agent!`.\nThis response is sent from the fulfillment webhook you just deployed.\nIn particular, the response came from this code: \n\n```\nfunction welcome(agent) {\n agent.add(`Welcome to my agent!`);\n}\n```\n\nCreate a custom fulfillment handler\n-----------------------------------\n\nThe steps above use a handler supplied by the default inline editor code.\nTo create a custom handler:\n\n1. Enable fulfillment for the `get-agent-name` intent you created in previous steps.\n2. Click **Fulfillment** in the left sidebar menu, and examine the code in the inline editor.\n3. Find this line:\n\n exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) =\u003e {\n\n The `onRequest` function handles all requests from Dialogflow.\n Within the body of this function, specific handler functions are defined.\n These handler functions are invoked when associated intents are matched.\n For example, the function used in the previous section is `function welcome(agent)`.\n4. Below the existing handlers, add this function for the `get-agent-name` intent:\n\n function getAgentNameHandler(agent) {\n agent.add('From fulfillment: My name is Dialogflow!');\n }\n\n5. Below the handler function definitions, there are `intentMap.set()` calls.\n These calls associate specific handlers with intents by name.\n For example, `intentMap.set('Default Welcome Intent', welcome)`\n associates the `welcome` handler with the intent named `Default Welcome Intent`.\n\n6. Below the existing `intentMap.set` calls,\n add this line for the `get-agent-name` intent:\n\n intentMap.set('get-agent-name', getAgentNameHandler);\n\n7. Click **Deploy** at the bottom of the form.\n\n8. Enter `What's your name?` in the simulator.\n The `From fulfillment: My name is Dialogflow!` response\n is sent from your new handler.\n\nAccess parameter values\n-----------------------\n\nIn previous steps, you created a `set-language` intent\nto identify human and programming languages.\nThe intent uses the `language` and `language-programming` parameters.\nIn this section, you access values for these parameters\nin your fulfillment handler.\n\nTo add the handler:\n\n1. Enable fulfillment for the `set-language` intent.\n2. Click **Fulfillment** in the left sidebar menu.\n3. Similar to steps above, add the following handler and `intentMap` call:\n\n function languageHandler(agent) {\n const language = agent.parameters.language;\n const programmingLanguage = agent.parameters['language-programming'];\n if (language) {\n agent.add(`From fulfillment: Wow! I didn't know you knew ${language}`);\n } else if (programmingLanguage) {\n agent.add(`From fulfillment: ${programmingLanguage} is cool`);\n } else {\n agent.add(`From fulfillment: What language do you know?`);\n }\n }\n\n intentMap.set('set-language', languageHandler);\n\n4. Click **Deploy**.\n\nThis handler retrieves the parameters `language` and `language-programming`\nand stores them in variables.\nIt retrieves these values from the `agent.parameters` attribute,\nwhich is an object that contains all of the parameter names and values\nthat end-users specify in their requests.\n\nTo try it, enter `I know how to speak German` in the simulator."]]