简体   繁体   English

如何从 google-chat webhook 获取响应数据?

[英]How to get response data from google-chat webhook?

I'm following the following example:我正在关注以下示例:

const fetch = require('node-fetch');

const webhookURL = '<INCOMING-WEBHOOK-URL>';

const data = JSON.stringify({
  'text': 'Hello from a Node script!',
});

fetch(webhookURL, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json; charset=UTF-8',
  },
  body: data,
}).then((response) => {
  console.log(response);
});

For all intents and purposes, my code is essentially the same.出于所有意图和目的,我的代码本质上是相同的。

According to API response body should be of type Message but instead I'm getting the following:根据 API 响应主体应该是 Message 类型,但我得到以下内容:

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    body: Gunzip {
      _writeState: [Uint32Array],
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 5,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      bytesWritten: 0,
      _handle: [Zlib],
      _outBuffer: <Buffer 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 00 48 02 15 01 00 00 00 05 00 04 00 04 00 04 00 38 2f 01 15 01 00 00 00 30 46 01 15 01 00 00 00 1e 1e ... 16334 more bytes>,
      _outOffset: 0,
      _chunkSize: 16384,
      _defaultFlushFlag: 2,
      _finishFlushFlag: 2,
      _defaultFullFlushFlag: 3,
      _info: undefined,
      _maxOutputLength: 4294967295,
      _level: -1,
      _strategy: 0,
      [Symbol(kCapture)]: false,
      [Symbol(kTransformState)]: [Object],
      [Symbol(kError)]: null
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: <my webhook url>,
    status: 200,
    statusText: 'OK',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0
  }
}

I'm not sure how to process this data to read it as the desired Message type, as referenced in the API docs.我不确定如何处理此数据以将其读取为所需的消息类型,如 API 文档中所述。 Any help would be greatly appreciated!任何帮助将不胜感激!

For reference: https://developers.google.com/chat/reference/rest/v1/spaces/webhooks供参考: https://developers.google.com/chat/reference/rest/v1/spaces/webhooks

Solution was remarkably simple.解决方案非常简单。 I forgot that in order to read the contents of fetch as specified here I need to call我忘记了为了读取这里指定的fetch的内容,我需要调用

const resJson = await response.json()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM