How to send images as attachments in Google Chat

60 Views Asked by At

I'm getting an encoded image from overlayImages() and I want to send it as an attachment:

fun createMessageAsCardWithImage(arr: ByteArray): Message {
     val imageResult = overlayImages(arr)

     return Message().setText("Optional text message with attachment")
         .setAttachment(
             listOf(
                 Attachment()
                     .setContentType("image/jpg")
                     .setContentName("avatar.jpg")
                     .setThumbnailUri("data:image/jpg;base64$imageResult")
             )
         )
}

This method doesn't work, the text of the attachment is sent to the chat, but not the image itself. Which Attachment() method should I use for this, or what other methods are there?

I tried sending the image using setImage():

fun createMessageAsCardWithImage(arr: ByteArray): Message {
     val imageResult = overlayImages(arr)

     return Message().setCardsV2(
         arrayListOf(
             CardWithId().setCard(
                 GoogleAppsCardV1Card().setSections(
                     arrayListOf(
                         GoogleAppsCardV1Section().setWidgets(
                             arrayListOf(
                                 GoogleAppsCardV1Widget().setImage(
                                     GoogleAppsCardV1Image().setImageUrl("data:image/jpg;base64$imageResult")
                                 )
                             )
                         )
                     )
                 )
             )
         )
     )
}

Some images are sent this way, and some cause an error:

**Exception in thread "DefaultDispatcher-worker-7" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request**
POST https://chat.googleapis.com/v1/spaces/AAAAlDCKqPA/messages
{
   "code": 400,
   "errors": [
     {
       "domain": "global",
       "message": "Request contains an invalid argument.",
       "reason": "badRequest"
     }
   ],
   "message": "Request contains an invalid argument.",
   "status": "INVALID_ARGUMENT"
}

What should I do to send an image?

0

There are 0 best solutions below