Google Home对接

相关连接汇总

谷歌开发者网站:https://developers.google.com/ Cloud Functions:https://cloud.google.com/functions/ Functions示例代码: Google Actions:https://developers.google.com/actions/

配置Actions on Google

云端配置工程

创建工程

  • 前往谷歌开发者网站(https://developers.google.com/)注册后登录开发者平台
  • 选择Actions on Google

  • 进入Actions Console

  • 点击”Add/import project“新增一个Action

  • 按照界面输入Actions Project基本信息,确定则创建成功

填写基本信息

  • 点击Overview,看到Actions的概要信息

  • 点击App information,配置Actions相关信息,最下方保存

添加OAuth

  • 点击Account Linking,配置Google Actions用户账号和Ablecloud账号授权,选择Authorization Code类型

  • 填入Client information

Authorization Url:

https://oauthtest.ablecloud.cn/authorize (测试环境) 
https://oauth.ablecloud.cn/authorize (国内环境)
https://usoauth.ablecloud.cn/authorize (北美环境)
https://euoauth.ablecloud.cn/authorize (欧洲环境)

Access Token Url:

https://oauthtest.ablecloud.cn/token (测试环境) 
https://oauth.ablecloud.cn/token (国内环境)
https://usoauth.ablecloud.cn/token (北美环境)
https://euoauth.ablecloud.cn/token (欧洲环境)

获取Ablecloud上的Client ID和Client Secret

  • 登录Ablecloud管理平台

  • 选择 Matrix平台相应环境(开发环境,生产环境)

  • 选择顶部选项卡“应用组件”->左侧列表项“智能接入”,Google Home页面即有所需信息

  • 填入Client ID和Client Secret,保存即可

本地配置开发环境

查看上传指令

  • 点击ADD ACTIONS,看到多个action bundle的配置说明

  • 点击Actions SDK选项卡的BUILD按钮,记住action bundle的配置指令

配置本地Actions工程

  • 本地安装环境

gactions命令行工具:https://developers.google.com/actions/tools/gactions-cli

  • 新建开发目录,如(~/work-demo/AbleTemplate)
mkdir -p ~/work-demo/AbleTemplate
  • 初始化本地工程
gactions init
  • 编辑本地工程action.json

signInRequired用于Account Linking提示

{
  "actions": [
    {
        ...
      "intent": {
        "name": "actions.intent.MAIN",
        ...
      },
      "signInRequired": true
    },
    {
      ...
      "intent": {
        "name": "ablecloud.intent.Open",
        ...
      }
    },
    {
      ...
      "intent": {
        "name": "ablecloud.intent.Close",
        ...
      }
    }
  ],
  "conversations": {
    "AbleTemplate": {
      "name": "AbleTemplate",
      "url": "<INSERT YOUR FULLFILLMENT URL HERE>",
      "fulfillmentApiVersion": 2
    }
  }
}

fullfillment:conversations的url称为fullfillment,即实现了业务逻辑处理的API入口,这里我们使用Google的Cloud Funtions服务来实现

fulfillmentApiVersion:必须为2

配置Cloud Functions for Firebase

根据Acionts on Google文档章节fulfillment-hosting,每个Actions on Google工程默认创建一个Cloud Functions for Firebase工程

配置Funtions本地工程

  • 本地安装环境

node npm firebase命令行工具

npm install -g firebase-tools
  • firebase工具Google开发者账号授权
firebase login
  • 新建开发目录,如(~/work-demo/AbleTemplate/)
mkdir -p ~/work-demo/AbleTemplate
cd ~/work-demo/AbleTemplate
  • 初始化本地工程
firebase init
  • Project Feature选中“Functions”
 ◯ Database: Deploy Firebase Realtime Database Rules
 ◯ Firestore: Deploy rules and create indexes for Firestore
❯◉ Functions: Configure and deploy Cloud Functions
 ◯ Hosting: Configure and deploy Firebase Hosting sites
 ◯ Storage: Deploy Cloud Storage security rules
  • Firebase project选择与Actions对应的Project ID
  [don't setup a default project]
❯ AbleTemplate (abletemplate-c861b)
  [create a new project]

本地编写业务逻辑

  • 安装依赖
npm install --save actions-on-google
  • 修改index.json

...

const AbleTemplate = functions.https.onRequest((req, res) => {
  const app = new ActionsSdkApp({request: req, response: res});

  console.log(`Request headers: ${JSON.stringify(req.headers)}`);
  console.log(`Request body: ${JSON.stringify(req.body)}`);

  // Create functions to handle requests here
  let actionMap = new Map();
  actionMap.set(app.StandardIntents.MAIN, mainIntent);
  actionMap.set(app.StandardIntents.SIGN_IN, handleSignin);
  actionMap.set(app.StandardIntents.TEXT, handleText);
  actionMap.set("ablecloud.intent.Open", handleOpen);
  actionMap.set("ablecloud.intent.Close", handleClose);

  app.handleRequest(actionMap);
});

module.exports = {
  AbleTemplate
};
  • 部署到云端
cd <PROJECT_DIR>/functions
firebase deploy --only functions

配置Functions云端工程

  • 前往Firebase开发者网站(https://firebase.google.com/)注册后登录开发者平台

  • 进入Firebase Console

  • 点击左下角Upgrade,付费方式从Spark改为Blaze

根据pricing,Spark免费方案无法访问非Google的URL,故使用有免费额度的Blaze付费方案

部署/测试Actions on Google

  • 完善Actions

将配置好的Functions服务URL写入action.json

{
  ...
  "conversations": {
    "AbleTemplate": {
      ...
      "url": "https://us-central1-abletemplate-c861b.cloudfunctions.net/AbleTemplate",
      "fulfillmentApiVersion": 2
    }
  }
}
  • 部署Actions
gactions update --action_package <PACKAGE_NAME> --project <PROJECT_ID>

示例工程 PACKAGE_NAME 为“action.json”, PROJECT_ID 为“abletemplate-c861b”