创建 smart home 操作后,下一步是在 您的执行方式来处理智能家居 intent 并返回 Google Assistant 可识别。
识别用户
Assistant向您的
smart home 操作(具有提供的访问令牌)的执行方式
由 OAuth 2.0 服务器在 Authorization
标头中添加。
POST /fulfillment HTTP/1.1 Host: smarthome.example.com Content-Type: application/json Authorization: Bearer ACCESS_TOKEN
在响应任何请求之前,你的执行方式逻辑应验证此令牌凭据是否有效并确定关联的用户账号。如果
访问令牌无效,你的执行方式应返回
HTTP 401 Unauthorized
错误。
列出设备及其功能
Assistant 会发送
action.devices.SYNC
intent 对您的
用于请求与指定用户关联的设备列表的执行方式,以及
功能。您的执行方式必须针对以下国家/地区中的每位用户返回唯一 ID
SYNC
响应的 agentUserId
字段。此 ID 必须是不可变的
值来代表您的云服务的用户。不建议
根据用户可以执行的设置提供电子邮件地址或其他属性
更改。
你的 SYNC
响应的 devices
字段包含
用户已授权Assistant访问,则
类型和特征
以及配置 trait 行为所需的属性,
特定设备
SYNC
intent 会在账号关联期间或在用户手动重新同步其设备时触发。如果用户的设备列表,支持
特征或属性值发生变化,请使用
请求同步以触发新的
SYNC
intent 并向 Google 报告更新。
{ "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "inputs": [{ "intent": "action.devices.SYNC" }] }
JSON
{ "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "payload": { "agentUserId": "1836.15267389", "devices": [ { "id": "123", "type": "action.devices.types.OUTLET", "traits": [ "action.devices.traits.OnOff" ], "name": { "defaultNames": [ "My Outlet 1234" ], "name": "Night light", "nicknames": [ "wall plug" ] }, "willReportState": false, "roomHint": "kitchen", "deviceInfo": { "manufacturer": "lights-out-inc", "model": "hs1234", "hwVersion": "3.2", "swVersion": "11.4" }, "otherDeviceIds": [ { "deviceId": "local-device-id" } ], "customData": { "fooValue": 74, "barValue": true, "bazValue": "foo" } }, { "id": "456", "type": "action.devices.types.LIGHT", "traits": [ "action.devices.traits.OnOff", "action.devices.traits.Brightness", "action.devices.traits.ColorSetting" ], "name": { "defaultNames": [ "lights out inc. bulb A19 color hyperglow" ], "name": "lamp1", "nicknames": [ "reading lamp" ] }, "willReportState": false, "roomHint": "office", "attributes": { "colorModel": "rgb", "colorTemperatureRange": { "temperatureMinK": 2000, "temperatureMaxK": 9000 }, "commandOnlyColorSetting": false }, "deviceInfo": { "manufacturer": "lights out inc.", "model": "hg11", "hwVersion": "1.2", "swVersion": "5.4" }, "customData": { "fooValue": 12, "barValue": false, "bazValue": "bar" } } ] } }
Node.js
const {smarthome} = require('actions-on-google'); const app = smarthome(); // ... app.onSync((body, headers) => { // TODO Get devices for user return { requestId: body.requestId, payload: { agentUserId: "1836.15267389", devices: [{ id: "123", type: "action.devices.types.OUTLET", traits: [ "action.devices.traits.OnOff" ], name: { defaultNames: ["My Outlet 1234"], name: "Night light", nicknames: ["wall plug"] }, willReportState: false, roomHint: "kitchen", deviceInfo: { manufacturer: "lights-out-inc", model: "hs1234", hwVersion: "3.2", swVersion: "11.4" }, otherDeviceIds: [{ deviceId: "local-device-id" }], customData: { fooValue: 74, barValue: true, bazValue: "foo" } }, { id: "456", type: "action.devices.types.LIGHT", traits: [ "action.devices.traits.OnOff", "action.devices.traits.Brightness", "action.devices.traits.ColorSetting" ], name: { defaultNames: ["lights out inc. bulb A19 color hyperglow"], name: "lamp1", nicknames: ["reading lamp"] }, willReportState: false, roomHint: "office", attributes: { colorModel: 'rgb', colorTemperatureRange: { temperatureMinK: 2000, temperatureMaxK: 9000 }, commandOnlyColorSetting: false }, deviceInfo: { manufacturer: "lights out inc.", model: "hg11", hwVersion: "1.2", swVersion: "5.4" }, customData: { fooValue: 12, barValue: false, bazValue: "bar" } }] } }; });
Java
@NotNull @Override public SyncResponse onSync(@NotNull SyncRequest syncRequest, @Nullable Map<?, ?> map) { Payload payload = new Payload(); payload.setAgentUserId("1836.15267389"); payload.setDevices( new Device[] { new Device.Builder() .setId("123") .setType("action.devices.types.OUTLET") .addTrait("action.devices.traits.OnOff") .setName( Collections.singletonList("My Outlet 1234"), "Night light", Collections.singletonList("Wall plug")) .setWillReportState(true) .setDeviceInfo("lights-out-inc", "hs1234", "3.2", "11.4") .setCustomData( new JSONObject() .put("fooValue", 74) .put("barValue", true) .put("bazValue", "foo")) .build(), new Device.Builder() .setId("456") .setType("action.devices.types.LIGHT") .addTrait("action.devices.traits.OnOff") .addTrait("action.devices.traits.Brightness") .addTrait("action.devices.traits.ColorTemperature") .addTrait("action.devices.traits.ColorSpectrum") .setName( Collections.singletonList("Lights Out Inc. bulb A19 color hyperglow"), "Lamp", Collections.singletonList("Reading lamp")) .setWillReportState(true) .setDeviceInfo("Lights Out Inc.", "hg11", "1.2", "5.4") .setCustomData( new JSONObject() .put("fooValue", 12) .put("barValue", false) .put("bazValue", "bar")) .build(), }); return new SyncResponse(syncRequest.getRequestId(), payload); }
如需了解详情,请参阅 SYNC
intent
参考文档。