-1

我正在尝试为每天运行一个功能并发送消息的环聊聊天机器人工作。我从 Google Developers 网站上的模板开始,但在获取 URL 时收到 404 截断服务器响应错误。

错误:

Truncated server response: <h1>Not Found</h1> <h2>Error 404</h2>

这是代码:

function onTrigger() {
  var spaceIds = PropertiesService.getScriptProperties()
      .getKeys();
  var message = { 'text': 'Hi! It\'s now ' + (new Date()) };
  for (var i = 0; i < spaceIds.length; ++i) {
    postMessage(spaceIds[i], message);
  }
}

var SCOPE = 'https://www.googleapis.com/auth/chat.bot';
// The values below are copied from the JSON file downloaded upon
// service account creation.
var SERVICE_ACCOUNT_PRIVATE_KEY = "-----BEGIN PRIVATE KEY---- bla bla bla---END PRIVATE KEY-----\n";
var SERVICE_ACCOUNT_EMAIL = "blablabla";

//key and email deleted here for safety reasons.

// Posts a message into the given space ID via the API, using
// service account authentication.
function postMessage(spaceId, message) {

  var service = OAuth2.createService('chat')
      .setTokenUrl('https://accounts.google.com/o/oauth2/token')
      .setPrivateKey(SERVICE_ACCOUNT_PRIVATE_KEY)
      .setClientId(SERVICE_ACCOUNT_EMAIL)
      .setPropertyStore(PropertiesService.getUserProperties())
      .setScope(SCOPE);
  if (!service.hasAccess()) {
    Logger.log('Authentication error: %s', service.getLastError());
    return;
  }
  var url = 'https://chat.googleapis.com/v1/' + spaceId + '/messages';
  UrlFetchApp.fetch(url, {
    method: 'post',
    headers: { 'Authorization': 'Bearer ' + service.getAccessToken() },
    contentType: 'application/json',
    payload: JSON.stringify(message),
  });event.type == "ADDED_TO_SPACE"
}

任何帮助将不胜感激。

提前致谢。马科斯

4

1 回答 1

1

回答:

在空间中创建消息的 URL 需要以空间 ID的形式spaces/*提供。*

使固定:

您需要根据文档更改您的 URL:

var url = 'https://chat.googleapis.com/v1/space/' + spaceId + '/messages';

参考:

于 2020-01-20T11:32:36.710 回答