2

I have read all the documentation that Google offers in relation to the Google Chat Rest API which is here and here but it is not clear on how to send an image stored in Google Drive in a message. Google has no examples of this on GitHub either, only cards and the card example is based on a public image, not one in Drive.

The code, which does send the message, but without the image, is below:

from flask import Flask, render_template, request, json
from apiclient import discovery
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials

scopes = ['https://www.googleapis.com/auth/chat.bot']

key = 'file.json'

app = Flask(__name__)

@app.route('/', methods=['POST'])
def home_post():
  event = request.get_json()
  email = event['user']['email']
  message = event['message']['text']
  space = event['space']['name']

  creds = ServiceAccountCredentials.from_json_keyfile_name(key, scopes)
  CHAT = discovery.build('chat', 'v1', http=creds.authorize(Http()))

  respe = {
    "text": "from jjime",
    "attachment": [{
    "contentName": "driveimgtest.png",
    "contentType": "image/png",
    "driveDataRef": {
      "driveFileId": "ID OF IMAGE IN DRIVE"
      },
    "source":"DRIVE_FILE",
    }]
  }

  CHAT.spaces().messages().create(parent=space,body=respe).execute()
  return json.jsonify({})

@app.route('/', methods=['GET'])
def home_get():
  return render_template('home.html')

if __name__ == '__main__':
  app.run(debug=True)

So I get back "from jjme" in Google Chat, and I get no errors in the terminal, but no image is attached. The image is in Google Drive and it is set to "anyone with the link can view". The contentName is also set to the exact name of the image in Google Drive. In the documentation, it does not clarify what arguments are necessary, so I really don't know what I am missing. Any help would be appreciated.

Jason Jurotich
  • 441
  • 4
  • 24

1 Answers1

1
  • As per the documentation you linked, there is only a get method - to retrieve attachments, but not to send them.
  • As far as bots go, they can either send simple text messages or card messages. Bots cannot send images without a card.
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • 1
    If that is the case, I will do tests in relation to this, which Google gave as an example here (https://github.com/googleworkspace/hangouts-chat-samples/blob/master/python/card-bot/main.py). After that I can select the answer as correct. I still find it strange that Google does not even give me an error in trying this (getting an image from Drive). The most logical is that Google would have indicated you can't do this by an error, but none come out. – Jason Jurotich Aug 12 '21 at 12:51