order-update.js 发送“{}”作为交易 api actions-on-google 中的响应
order-update.js sent "{}" as response in transaction api actions-on-google
我正在尝试在用户发送订单后发送有关订单状态的更新。我一直在使用 actions on google transaction guide 代码来构建我的系统。但是,一开始我遇到了 "Cannot read property 'JWT' of undefined" 错误,并用 const {google} = require('googleapis');
.
更改了我的代码
现在,当 运行 order-update.js 文件时,我在控制台上收到“{}”作为响应并且无法收到通知。
我正在移动设备上尝试。我不确定是不是因为我使用的是沙盒模式。
const {google} = require('googleapis');
const request = require('request');
const {OrderUpdate} = require('actions-on-google');
const key = require('<my_service_account_path>');
const jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
['https://www.googleapis.com/auth/actions.fulfillment.conversation'],
null
);
jwtClient.authorize((err, tokens) => {
if (err) {
console.log(err);
return;
}
const currentTime = new Date().toISOString();
const actionOrderId = '<my_order_id>';
const orderUpdate = new OrderUpdate({
actionOrderId: actionOrderId,
orderState: {
label: 'Order has been delivered!',
state: 'FULFILLED',
},
updateTime: currentTime,
});
const bearer = 'Bearer ' + tokens.access_token;
const options = {
method: 'POST',
url: 'https://actions.googleapis.com/v2/conversations:send',
headers: {
'Authorization': bearer,
},
body: {
custom_push_message: {
order_update: orderUpdate,
},
// The line below should be removed for non-sandbox transactions.
is_in_sandbox: true,
},
json: true,
};
request.post(options, (err, httpResponse, body) => {
if (err) {
console.log(err);
return;
}
console.log(body);
});
});
在指南中,我找不到我应该得到的任何响应,但我认为“{}”不是答案。
对于推送通知,“200:OK”是预期的响应。
您正在记录正文(或错误),它是一个 JSON 对象。对于成功的响应,一个空的 JSON 正文并不奇怪。错误应包含错误消息。
如果您想验证响应是 HTTP 代码 200,"OK",那么您应该检查 httpResponse.statusCode
。
Some important order updates will result in a push notification being
sent to the user's Assistant-enabled mobile devices.
但并不是说所有都会。目前尚不清楚它认为 "important" 是什么,但是,设置 userNotification
字段 "is a suggestion to notify and is not guaranteed to result in a notification." 您可以添加类似
的字段
const orderUpdate = new OrderUpdate({
actionOrderId: actionOrderId,
orderState: {
label: 'Order has been delivered!',
state: 'FULFILLED',
},
updateTime: currentTime,
userNotification: {
title: "Update on your order",
text: "Your order has been delivered!"
}
});
我正在尝试在用户发送订单后发送有关订单状态的更新。我一直在使用 actions on google transaction guide 代码来构建我的系统。但是,一开始我遇到了 "Cannot read property 'JWT' of undefined" 错误,并用 const {google} = require('googleapis');
.
现在,当 运行 order-update.js 文件时,我在控制台上收到“{}”作为响应并且无法收到通知。 我正在移动设备上尝试。我不确定是不是因为我使用的是沙盒模式。
const {google} = require('googleapis');
const request = require('request');
const {OrderUpdate} = require('actions-on-google');
const key = require('<my_service_account_path>');
const jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
['https://www.googleapis.com/auth/actions.fulfillment.conversation'],
null
);
jwtClient.authorize((err, tokens) => {
if (err) {
console.log(err);
return;
}
const currentTime = new Date().toISOString();
const actionOrderId = '<my_order_id>';
const orderUpdate = new OrderUpdate({
actionOrderId: actionOrderId,
orderState: {
label: 'Order has been delivered!',
state: 'FULFILLED',
},
updateTime: currentTime,
});
const bearer = 'Bearer ' + tokens.access_token;
const options = {
method: 'POST',
url: 'https://actions.googleapis.com/v2/conversations:send',
headers: {
'Authorization': bearer,
},
body: {
custom_push_message: {
order_update: orderUpdate,
},
// The line below should be removed for non-sandbox transactions.
is_in_sandbox: true,
},
json: true,
};
request.post(options, (err, httpResponse, body) => {
if (err) {
console.log(err);
return;
}
console.log(body);
});
});
在指南中,我找不到我应该得到的任何响应,但我认为“{}”不是答案。 对于推送通知,“200:OK”是预期的响应。
您正在记录正文(或错误),它是一个 JSON 对象。对于成功的响应,一个空的 JSON 正文并不奇怪。错误应包含错误消息。
如果您想验证响应是 HTTP 代码 200,"OK",那么您应该检查 httpResponse.statusCode
。
Some important order updates will result in a push notification being sent to the user's Assistant-enabled mobile devices.
但并不是说所有都会。目前尚不清楚它认为 "important" 是什么,但是,设置 userNotification
字段 "is a suggestion to notify and is not guaranteed to result in a notification." 您可以添加类似
const orderUpdate = new OrderUpdate({
actionOrderId: actionOrderId,
orderState: {
label: 'Order has been delivered!',
state: 'FULFILLED',
},
updateTime: currentTime,
userNotification: {
title: "Update on your order",
text: "Your order has been delivered!"
}
});