order
我正在尝试在用户发送订单后发送有关订单状态的更新。我一直在使用actions on google transaction guide代码来构建我的系统。但是,我首先得到“无法读取属性'JWT'的未定义”错误,并使用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,
['.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: ':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,“确定”,那么你应该检查httpResponse.statusCode
。
documentation说
一些重要的订单更新将导致将推送通知发送到用户的启用助理的移动设备。
但并不是说一切都会。目前尚不清楚它认为“重要”是什么,但是,设置userNotification
字段“是建议通知,并不保证会产生通知。”您可以添加类似的字段
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!"
}
});