部署 fulfillment (Dialogflow)

构建网络钩子执行方式后,将其部署到生产环境中,并将其关联到您的 Action。以下步骤介绍了如何设置环境,以便部署到 Cloud Functions for Firebase,以便进行生产托管。不过,您可以选择任何支持 HTTPS 请求和响应的网站托管平台来托管您的执行方式。

如果您已将网络钩子部署到网站托管平台,可以跳至将网络钩子关联到您的 Action。请注意,以下过程的最终步骤因您使用的是 Dialogflow 还是 Actions SDK 而异。

部署到 Cloud Functions for Firebase

  1. 下载并安装 Node.js
  2. 设置并初始化 Firebase CLI。如果以下命令失败并显示 EACCES 错误,您可能需要更改 npm 权限

    npm install -g firebase-tools
    
  3. 使用您的 Google 帐号对 Firebase 工具进行身份验证:

    firebase login
    
  4. 进入您的 Action 项目目录并初始化 Firebase。系统会要求您选择要为 Actions 项目设置的 Firebase CLI 功能。选择 Functions 以及您可能想使用的其他功能(如 Firestore),然后按 Enter 键确认并继续:

    cd <cloud_function_dir>
    firebase init
    
  5. 使用箭头键选择 Firebase 工具并在项目列表中导航,将 Firebase 工具与您的 Actions 项目相关联:

    命令进行此关联。
  6. 选择项目后,Firebase 工具会启动 Functions 设置,询问您要使用哪种语言。使用箭头键进行选择,然后按 Enter 键以继续。

    === Functions Setup
    A functions directory will be created in your project with a Node.js package pre-configured. Functions can be deployed with firebase deploy. ? What language would you like to use to write Cloud Functions? (Use arrow keys) > JavaScript TypeScript
  7. 选择是否要使用 ESLint 捕获可能出现的 bug 并强制执行样式类型 YN

    ? Do you want to use ESLint to catch probable bugs and enforce style? (Y/n)
  8. 通过在提示符中输入 Y 获取项目依赖项:

    ? Do you want to install dependencies with npm now? (Y/n)

    设置完成后,您将看到类似于以下内容的输出:

    ✔  Firebase initialization complete!
  9. 安装 actions-on-google 依赖项:

    cd <cloud_function_dir>/functions
    npm install actions-on-google
    
  10. 获取执行方式依赖项并部署执行方式函数:

    npm install
    firebase deploy --only functions
    

    部署需要几分钟时间。完成后,您将看到类似于以下内容的输出。您将需要在 Dialogflow 中输入函数网址

    ✔  Deploy complete!
    Project Console: https://console.firebase.google.com/project/exampleproject-123/overview Function URL (cloudFunctionName): https://us-central1-exampleproject-123.cloudfunctions.net/cloudFunctionName

将网络钩子关联到您的 Action

如果您使用的是 Dialogflow:在 Dialogflow 控制台中,转到 Fulfillment,将网络钩子按钮切换为已启用,然后将网址字段中的网址替换为您的函数网址

如果您使用的是 Actions SDK:请在 conversations 对象内创建一个对象,以在 Action 软件包中声明您的执行方式:

{
  "actions": [
    {
      "description": "Default Welcome Intent",
      "name": "MAIN",
      "fulfillment": {
        "conversationName": "myFulfillmentFunction"
      },
      "intent": {
        "name": "actions.intent.MAIN",
        "trigger": {
          "queryPatterns": [
            "talk to myFulfillmentFunction"
          ]
        }
      }
    }
  ],
  "conversations": {
    "myFulfillmentFunction": {
      "name": "myFulfillmentFunction",
      "url": "https://us-central1-myprojectname-ab123.cloudfunctions.net/cloudFunctionName"
    }
  },
  "locale": "en"
}