建立 smart home 動作後,下一步就是在 的執行要求,用於處理智慧型住宅意圖並傳回 Google Assistant 識別。
識別使用者
「Assistant」會向您的
smart home 動作的執行要求和提供的存取權杖
透過 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
想用來達成您的
要求提供與指定使用者相關聯的裝置清單
一些技術。您的執行要求必須傳回以下國家/地區內每位使用者的專屬 ID:
SYNC
回應的 agentUserId
欄位。此 ID 不得變更
值。我們不建議您
根據使用者可變更的設定提供電子郵件地址或其他屬性
變更。
SYNC
回應的 devices
欄位包含
使用者已授權 Assistant 存取,
類型和特徵
支援,以及設定特徵行為時所需的屬性
該裝置。
連結帳戶時或使用者會觸發 SYNC
意圖
手動重新同步裝置。如果使用者的裝置清單
特徵或屬性值改變時,請使用
要求同步處理,以觸發新的
SYNC
意圖,並向 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
意圖。
參考文件