belladonna
belladonna

Reputation: 31

How to get user profile picture from Google Chat Bot using Google App Script?

I'm a bit new to Google App Script and I can't find how to do it. I am creating a Google Chat bot using Google App Script and I want to get the profile picture of the user who messaged the bot, preferably the link instead of the image itself since I will be passing the image to a website.

Edit: I do know that Google has an API to get the user photo like this, if I'm an admin - which I'm not - or like this but I don't know how to use it in App Script or if it's even possible to do so. From what I understand I need to have the user's ID which I have no idea how to get. I have also looked at this question however the links that could be helpful for me doesn't work anymore. If it helps I also know their email address, I just need to get the account ID from there. I don't mind using other APIs that require OAuth to do this. If it helps I also know their email address, I just need to get the account ID from there.

Upvotes: 1

Views: 1881

Answers (2)

Iamblichus
Iamblichus

Reputation: 19309

Answer:

This information cannot be retrieved with Chat API.

Reference:

Chat API User resource doesn't include any information about the profile picture:

{
  "name": string,
  "displayName": string,
  "domainId": string,
  "type": enum (Type),
  "isAnonymous": boolean
}

File a feature request:

If you think this feature can be useful, I'd suggest requesting it in Issue Tracker using this template.

Upvotes: 0

belladonna
belladonna

Reputation: 31

In case anyone would need the answer in the future what I did was add "https://www.googleapis.com/auth/userinfo.profile" to my scopes and added this to my code

var accessToken = ScriptApp.getOAuthToken();
var url = 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' + accessToken + '';
var response = UrlFetchApp.fetch(url);
response =  JSON.parse(response.getContentText());
var image = response['picture'];

The image then returns a link to the public profile picture.

Upvotes: 1

Related Questions