Search code examples
google-apps-scriptchatbotgoogle-workspacegoogle-hangouts

Google Chat bot - Send private message WITHOUT event


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.

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.

So instead of this :

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

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 !

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.


Solution

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

    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.