Google apps script webhook的应用程序脚本bot

Google apps script webhook的应用程序脚本bot,google-apps-script,google-hangouts,Google Apps Script,Google Hangouts,我想看看是否有人在谷歌应用程序脚本中创建了一个聊天机器人来处理网络钩子?我已经创建了一个机器人,但我不确定如何将webhook url输入到机器人代码中,以便将消息部署到聊天室。我还没有准确创建您要查找的内容,但是我相信可以找到您要查找的答案。基本上,只要你的机器人进入一个空间,就会有一个事件发生。触发该事件后,您可以将空间id添加到存储在某处的列表中。(电子表格、PropertiesService等) 存储列表后,您可以将应用程序部署为web应用程序。你可以阅读更多关于web应用的信息,但有两

我想看看是否有人在谷歌应用程序脚本中创建了一个聊天机器人来处理网络钩子?我已经创建了一个机器人,但我不确定如何将webhook url输入到机器人代码中,以便将消息部署到聊天室。

我还没有准确创建您要查找的内容,但是我相信可以找到您要查找的答案。基本上,只要你的机器人进入一个空间,就会有一个事件发生。触发该事件后,您可以将空间id添加到存储在某处的列表中。(电子表格、PropertiesService等)

存储列表后,您可以将应用程序部署为web应用程序。你可以阅读更多关于web应用的信息,但有两件事你需要知道,google给你一个url,让你向其发出web请求,以及一对名为doGet(当有人发出get请求时)和doPost(当有人发出post请求时)的事件。您可以创建函数do post,并在将web应用发布到时获取参数

最后,在收到post后,您可以通过对每个ID执行api fetch调用,对google api执行fetch调用,将刚从请求中收到的消息发布到您所在的所有空间

下面将在第一个链接中直接从API发布代码

// Example bot for Hangouts Chat that demonstrates bot-initiated messages
// by spamming the user every minute.
//
// This bot makes use of the Apps Script OAuth2 library at:
//     https://github.com/googlesamples/apps-script-oauth2
//
// Follow the instructions there to add the library to your script.

// When added to a space, we store the space's ID in ScriptProperties.
function onAddToSpace(e) {
  PropertiesService.getScriptProperties()
      .setProperty(e.space.name, '');
  return {
    'text': 'Hi! I\'ll post a message here every minute. ' +
            'Please remove me after testing or I\'ll keep spamming you!'
  };
}

// When removed from a space, we remove the space's ID from ScriptProperties.
function onRemoveFromSpace(e) {
  PropertiesService.getScriptProperties()
      .deleteProperty(e.space.name);
}

// Add a trigger that invokes this function every minute via the 
// "Edit > Current Project's Triggers" menu. When it runs, it will
// post in each space the bot was added to.
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-----\n...\n-----END PRIVATE 
KEY-----\n';
var SERVICE_ACCOUNT_EMAIL = 'service-account@project-id.iam.gserviceaccount.com';

// 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),
  });
}
//展示机器人启动的消息的聊天室机器人示例
//通过每分钟向用户发送垃圾邮件。
//
//此bot使用位于以下位置的应用程序脚本OAuth2库:
//     https://github.com/googlesamples/apps-script-oauth2
//
//按照此处的说明将库添加到脚本中。
//当添加到空间时,我们将空间的ID存储在ScriptProperties中。
ADDTOSPACE(e)上的函数{
PropertiesService.getScriptProperties()
.setProperty(如space.name“”);
返回{
“文本”:“嗨!我每分钟都会在这里发一条消息。”+
“请在测试后删除我,否则我将继续向您发送垃圾邮件!”
};
}
//从空间中删除时,我们将从ScriptProperties中删除空间的ID。
函数onRemoveFromSpace(e){
PropertiesService.getScriptProperties()
.deleteProperty(如空格、名称);
}
//添加一个触发器,通过
//“编辑>当前项目的触发器”菜单。当它运行时,它将
//在机器人添加到的每个空间中发布。
函数onTrigger(){
var spaceIds=PropertiesService.getScriptProperties()
.getKeys();
var message={'text':'Hi!现在是'+(new Date())};
对于(变量i=0;i
谢谢,仅仅为了让webhook在hangout聊天室中工作,似乎有很多事情要做,而使用slack,我只需抓取附加组件就可以了。