Google聊天机器人-发送不带事件的私人消息 [英] Google Chat bot - Send private message WITHOUT event

查看:105
本文介绍了Google聊天机器人-发送不带事件的私人消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在PM中工作的机器人,我可以与它对话并使其毫无问题地完成我的任务,但是我没有找到如何使它向特定的其他人发送消息的机器人.

I have a bot that is working in PM, I can talk with it and make it do my tasks without any problems but I don't find how to make it send a message to a specific other person.

我希望它可以将私人消息发送到特定的用户列表,而无需这些用户的任何交互.唯一的交互是来自我的订单,要求它向其他人发送消息.

I want it to send private messages to a specific list of users without any interactions from thoses users. The only interaction is the order coming from me, asking it to send messages to others.

我发现了很多有关机器人响应并通过Webhook进行响应的文档和帖子,但是关于机器人直接向某人发送PM的信息却没有.

I found a lot of documentations and posts about bot responding to messages with webhook but nothing about the bot sending directly a PM to someone.

所以代替这个:

function onMessage(event) {
    return {"text": "MSG = " + message };
}

我正在寻找可以指定用户ID 用户名的东西:

I'm looking for something where I could specify user ID or user name :

function sendMessage(ID/name) {
    return {"text": "MSG = " + message, "ID": ID}; //not accurate example
}
sendMessage("User_ID");

如果您对执行此操作有任何想法或信息,将不胜感激!

If you have any ideas or informations on the way to do it, this would be very appreciated !

更新:尚无法与某人发起DM对话,但可以通过检查bot所在的空间来向所有bot联系人发送消息(因此,无需每个人都向bot发送消息.消息是触发它所必需的.)

UPDATE : It's not yet possible to INITIATE a DM conversation with someone but it is possible to send messages to all the bot contacts by checking the spaces the bot is in (so ther is no need for each person to send a message to the bot, only one message is necessary to trigger it).

以下是我如何使用它的示例:

Here is an example on how I used it :

//Configure the chatbot service
function get_chatbot_service() {
  return OAuth2.createService(BOT_NAME)
  .setTokenUrl('https://accounts.google.com/o/oauth2/token') // Set the endpoint URL.
  .setPrivateKey(PRIVATE_KEY)                                // Set the private key.
  .setIssuer(CLIENT_EMAIL)                                   // Set the issuer.
  .setPropertyStore(PropertiesService.getScriptProperties()) // Set the property store where authorized tokens should be persisted.
  .setScope('https://www.googleapis.com/auth/chat.bot');     // Set the scope.
}

//Return all the spaces (DM and rooms) the bot belong to
function get_spaces() {
  var service  = get_chatbot_service();
  var url      = 'https://chat.googleapis.com/v1/spaces';
  var response = UrlFetchApp.fetch(url, { headers: { Authorization: 'Bearer ' + service.getAccessToken() }});
  var rep      = JSON.parse(response.getContentText());
  return (rep.spaces)
}

//Get the informations and send the message to every contacts the bot have been added to
function send_message() {
    var service = get_chatbot_service();
    var spaces  = get_spaces();
    var msg     = "Test message";
    
    for (var i = 0; i < spaces.length; i++) {
        var space  = spaces[i];
        if (space.type == "DM") {    //Check if we are in DM
            var url = 'https://chat.googleapis.com/v1/'+ space.name +'/messages'; //Specify the URL with space name
            var options = {
                method : 'POST',
                contentType: 'application/json',
                headers: { Authorization: 'Bearer ' + service.getAccessToken() },
                payload : JSON.stringify({ text: msg })     //Add your message
            }
            UrlFetchApp.fetch(url, options);   //Send the message
        }
    }
}

也许这会帮助某人一天.

Maybe this will help someone one day.

推荐答案

当前没有任何方法可以使漫游器开始对话.

Currently there is no possible way to make the bot start a conversation.

我在 Google的公开问题跟踪器上发现了此问题.您只需要去那里,然后单击标题旁边的星号,即可获取有关该问题的最新信息,并为该问题提供更多的可见性.

I have found this issue on Google's Public Issue Tracker. You just have to go there and click on the star next to the title so you get updates on the issue and you give the issue more visibility.

这篇关于Google聊天机器人-发送不带事件的私人消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆