Pat
Pat

Reputation: 101

How can a webhook identify USER_IDs?

I'm developing a webhook, and I want it to be able to @mention specific users. The simple message format specifies using the format of <users/123456789012345> to @mention a specific users. Since this is not a bot, it is not processing an incoming message and cannot pull the from the sender field, as suggested in the note on the developer site linked above.

How can I get the USER_ID of a user on my domain?

Upvotes: 10

Views: 9929

Answers (4)

Saucey
Saucey

Reputation: 31

Okay, so in order to get the UserID without having to do all of the things that you're trying to do here you need to use the Admin SDK API in google app script. Basically what you'll want to do is use your google app script as an intermediary for what you're trying to do. So you'll post something to google app script via their web exec functions on a deployed app and then have that app communicate to the google chat API via something like this:

var googlewebhookurl = 'https://chat.googleapis.com/v1/spaces/ASDFLKAJHEPQIHEWFQOEWNQWEFOINQ';
var options = {
    'method': 'post',
    'contentType': 'application/json',
    'payload' : JSON.stringify({ text: "<users/000000000001> I see you!" })
  }
UrlFetchApp.fetch(googlewebhookurl, options);

To start, you'll need to add the Admin SDK API to the services in your google app script services area. So click the plus next to "Services" and then select the Admin SDK API and click add to add it to the services, it winds up showing up in the list as "AdminDirectory" once it has been added to the services.

This is an image showing what it looks like once you've added it to the services.

Here is a link to the documentation for the Admin SDK API getting user information: https://developers.google.com/apps-script/advanced/admin-sdk-directory#get_user

You should be able to copy and paste that example function to get the information you're looking for regarding the user. I'm pasting the example code below in case something happens to this link:

/**
* Get a user by their email address and logs all of their data as a JSON string.
*/
function getUser() {
  var userEmail = '[email protected]';
  var user = AdminDirectory.Users.get(userEmail);
  Logger.log('User data:\n %s', JSON.stringify(user, null, 2));
}

In order to get the user id, take the user variable that comes back and access user.id and voila! You have the ID you're looking for. From there just plaster it into a text message in google chat and you should be in business. I haven't gotten it to work with cards at all yet. I'm not seeing any documentation saying that it's supported in cards at all. For more information regarding chat messages and cards take a look at these:

https://developers.google.com/chat/api/guides/message-formats/basic

https://developers.google.com/chat/api/guides/message-formats/cards

Upvotes: 1

Pablo Salazar
Pablo Salazar

Reputation: 957

I use this for find the USER_ID but if you have another way let me know

  1. Right click on the user and select inspect enter image description here

  2. Search for data-member-id this is your USER_ID

enter image description here

Upvotes: 8

square_eyes
square_eyes

Reputation: 1281

I am in the same boat as you. Trying to use a Webhook to mention a user.

If you inspect the HTML on https://chat.google.com, using web/dev tools, you can actually see user ID under tag data-member-id="user/bot/123456789012345"

Using the above I tried to form a webhook that mentioned the user (in my case another bot).

Unfortunately it didn't work. Passing in the string <users/123456789012345> please test came through to chat as a string literal.

It's strange because the html links and markup DO get interpreted. As mentioned here. Edit: so does <users/all>.

I'm leaving this here since your question asks for how to elicit the ID. While it may not be pretty, or much automatable, it helped me get the user ID at least.

Edit 2: It works, just not to mention my other bot for some reason. But using this method I was able to mention a human user :)

Upvotes: 10

Kelvin Youk
Kelvin Youk

Reputation: 255

A webhook will not be able to pull the USER_ID of a user. As a workaround for this, you can create a service account and a bot that has access to the room and use the REST API spaces.members.list() and spaces.members.get() method.

Note: The bot will need to be added to the room.

Upvotes: 1

Related Questions