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.