【问题标题】:order-update.js sent "{}" as response in transaction api actions-on-googleorder-update.js 在事务 api actions-on-google 中发送“{}”作为响应
【发布时间】:2019-07-08 10:32:04
【问题描述】:

我正在尝试在用户发送订单后发送有关订单状态的更新。我一直在使用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,
  ['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”是预期的响应。

【问题讨论】:

    标签: node.js firebase google-api dialogflow-es actions-on-google


    【解决方案1】:

    您正在记录正文(或错误),它是一个 JSON 对象。对于成功的响应,空的 JSON 正文并不奇怪。错误应包含错误消息。

    如果你想验证响应是 HTTP 代码 200,“OK”,那么你应该检查httpResponse.statusCode

    documentation 状态

    一些重要的订单更新将导致推送通知 发送到用户启用 Google 助理的移动设备。

    但并不是说一切都会。目前尚不清楚它认为什么是“重要的”,但是,设置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!"
        }
      });
    

    【讨论】:

    • 谢谢,一切顺利。尽管如此,我还是无法收到通知,但我可以看到订单状态的变化。
    猜你喜欢
    • 2017-10-28
    • 2019-03-11
    • 2018-06-07
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多