使用者與 Google Assistant 互動以查詢目前項目
您的執行要求會收到
action.devices.QUERY
意圖,其中包含
裝置 ID 清單 (由 SYNC
回應提供)。
您的執行要求收到
action.devices.EXECUTE
意圖
當使用者傳送指令給 Assistant 以控制
裝置。
處理 QUERY
個意圖
您的 QUERY
回應包含每種特徵的完整狀態組合
。
{ "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "inputs": [{ "intent": "action.devices.QUERY", "payload": { "devices": [{ "id": "123", "customData": { "fooValue": 74, "barValue": true, "bazValue": "foo" } }, { "id": "456", "customData": { "fooValue": 12, "barValue": false, "bazValue": "bar" } }] } }] }
JSON
{ "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "payload": { "devices": { "123": { "on": true, "online": true }, "456": { "on": true, "online": true, "brightness": 80, "color": { "name": "cerulean", "spectrumRGB": 31655 } } } } }
Node.js
const {smarthome} = require('actions-on-google'); const app = smarthome(); // ... app.onQuery((body, headers) => { // TODO Get device state return { requestId: body.requestId, payload: { devices: { 123: { on: true, online: true }, 456: { on: true, online: true, brightness: 80, color: { name: "cerulean", spectrumRGB: 31655 } } } } }; });
Java
@NotNull @Override public QueryResponse onQuery(@NotNull QueryRequest queryRequest, @Nullable Map<?, ?> map) { QueryResponse.Payload payload = new QueryResponse.Payload(); payload.setDevices( new HashMap<String, Map<String, Object>>() { { put( "123", new HashMap<String, Object>() { { put("on", true); put("online", true); } }); put( "456", new HashMap<String, Object>() { { put("on", true); put("online", true); put("brightness", 80); put( "color", new HashMap<String, Object>() { { put("name", "cerulean"); put("spectrumRGB", 31655); } }); } }); } }); return new QueryResponse(queryRequest.getRequestId(), payload); }
詳情請參閱 QUERY
意圖。
參考文件
處理 EXECUTE
個意圖
與 QUERY
類似,單一意圖可以指定多個裝置 ID。單一
EXECUTE
意圖也可以包含為群組提供的多個不同指令
以及各式各樣的裝置舉例來說,觸發的意圖可能會同時設定亮度和顏色
,或將多個燈具設為不同的顏色。您的
EXECUTE
回應應會在以下時間後傳回裝置的新狀態:
使用 Report State
使用者的裝置變更。舉例來說,因為 EXECUTE
意圖或本機狀態變更 (例如手動切換燈具開關)。
這樣一來,「Google Home Graph」就能與雲端服務保持同步。
{ "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "inputs": [{ "intent": "action.devices.EXECUTE", "payload": { "commands": [{ "devices": [{ "id": "123", "customData": { "fooValue": 74, "barValue": true, "bazValue": "sheepdip" } }, { "id": "456", "customData": { "fooValue": 36, "barValue": false, "bazValue": "moarsheep" } }], "execution": [{ "command": "action.devices.commands.OnOff", "params": { "on": true } }] }] } }] }
JSON
{ "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "payload": { "commands": [ { "ids": [ "123" ], "status": "SUCCESS", "states": { "on": true, "online": true } }, { "ids": [ "456" ], "status": "ERROR", "errorCode": "deviceTurnedOff" } ] } }
Node.js
const {smarthome} = require('actions-on-google'); const app = smarthome(); // ... app.onExecute((body, headers) => { // TODO Send command to device return { requestId: body.requestId, payload: { commands: [{ ids: ["123"], status: "SUCCESS", states: { on: true, online: true } }, { ids: ["456"], status: "ERROR", errorCode: "deviceTurnedOff" }] } }; });
Java
@NotNull @Override public ExecuteResponse onExecute( @NotNull ExecuteRequest executeRequest, @Nullable Map<?, ?> map) { ExecuteResponse.Payload payload = new ExecuteResponse.Payload(); payload.setCommands( new Commands[] { new Commands( new String[] {"123"}, "SUCCESS", new HashMap<String, Object>() { { put("on", true); put("online", true); } }, null, null), new Commands(new String[] {"456"}, "ERROR", null, "deviceTurnedOff", null) }); return new ExecuteResponse(executeRequest.getRequestId(), payload); }
詳情請參閱 EXECUTE
意圖參考說明文件
狀態回應
您的 QUERY
和 EXECUTE
回應中包含一個 status
欄位,用於回報
。每個狀態回應可提供下列其中一種狀態:
值:
SUCCESS
:要求已成功。OFFLINE
:目標裝置處於離線狀態或無法存取。EXCEPTIONS
:要求發生問題或快訊。ERROR
:要求失敗,並顯示對應的errorCode
。