I want to make a workflow that whenever someone joins the organization, they are automatically added to an organization-wide room on Google Chat (not Google Group).
In the Google Chat room, if you mention someone in your organization and if they are not present in the room, then they are automatically added to the room.
So, I thought to create a Google App Script which will fetch the users of the organization and then send a message, using webhook, in a room. The problem is that it is not mentioning the user who is not in the room already. Therefore, it is not making my idea work.
Is there any way around to this? Can I use Google Chat API to add users to a room? Please point me to a relevant resource for this.
Please find the code I used below
function listAllUsers() {
var pageToken;
var page;
var userList = []
do {
page = AdminDirectory.Users.list({
domain: 'domain.xyz',
maxResults: 100,
pageToken: pageToken
});
var users = page.users;
if (users) {
for (var i = 0; i < users.length; i++) {
var user = users[i];
sendMessage('<users/'+user.id+'>');
Logger.log('u:%s, %s (%s)', user.id, user.name.fullName, user.primaryEmail);
}
} else {
Logger.log('No users found.');
}
pageToken = page.nextPageToken;
} while (pageToken);
}
function sendMessage(userid){
message = {'text':userid}
UrlFetchApp.fetch('<webhook-url>', {
method: 'post',
headers: {},
contentType: 'application/json',
payload: JSON.stringify(message),
});
}