2
votes

I am using Google Apps Script to call an external API to post both text and an image to a contact in an external system. I have posted the text fine, many times, no problems. I have not worked with sending or even using images in Apps Script before, so I am unsure of how to send the image as a file. I've done quite a bit of research on Stack Overflow and elsewhere, but have not found the answer to this yet.

The API documentation for the external system says that it needs the following:

contactId - Type: String

Message:

text - Type: String... Description: Message text (Media or Text is required).

upload - Type: File... Description: Message image (Media or Text is required). Media must be smaller than 1.5mb. Use a jpg, jpeg, png, or gif.

The "upload", type "File" (a jpg picture/image) is what I cannot figure out how to grab, format, and send. I currently have the image in Google Drive, have shared it for anyone to access via its URL, and it is well under 1.5MB.

Here is most of my test code (marked as JS, but really Google Apps Script), with the identifying info changed, with several different ways I have tried it. At this point, I am just banging my head against the wall! Any help is greatly appreciated!!! Thank you!

function TestAPI() {
  
  var apiKey2 = '9xxxxx-xxxx2-xxxxx-bxxx-3xxxxxxa'; //API Key for the external system
  
  var url4 = 'https://www.externalsystem.com/api/v1/[contactID]/send';
  var pic = DriveApp.getFileById("1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_");  // I Tried this
  //  var pic = driveService.files().get('1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_');  //And tried this
  //  var pic = DriveApp.getFileByID('1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_').getAs(Family.JPG);  //And this
  //  var pic = { "image" : { "source": {"imageUri": "https://drive.google.com/file/d/1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_" } } };  //And this
  //  var pic = { file : DriveApp.getFileById("1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_") };  //And this
  
  var formData = {
    'contactID': '[contactID]',
    'text': "Text here to send to external system through API",   // This works fine every time!
    'upload': pic       // Tried this
//  'upload': DriveApp.getFileByID("1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_").getBlob()  // And tried this
//  'upload': { "image" : { "source": {"imageUri": "https://drive.google.com/file/d/1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_" } } }  // And this
  };
  
  var options4 = {
    "headers":{"x-api-key":apiKey2},
    'method' : 'post',
    'payload': formData
  };
  
  var response4 = UrlFetchApp.fetch(url4, options4);

}

Once again, everything is working fine (text, etc.) except for the image (the "upload") not coming through. I am pretty sure it is because I don't know how to "package" the image from Google Drive through the UrlFetchApp call to the API.

1
Can you provide the URL of document of specification of external API? And also, can you provide the error message? - Tanaike
@TheMaster - I don't believe that that answers my question, as I am getting the image from Google Drive instead of another place, and not uploading to Discord Webhook. However, I'm not very familiar with that, so I could be wrong. If you still think it might answer my question, could you please elaborate? Thank you! - Chris
@Tanaike - The external API specification is www.projectbroadcast.com/apidoc/#api-Contacts-Send_Message. There is no error message. As far as I can tell, the API simply ignores the image upload. I'm sure the reason is because I am not downloading and/or "packaging" it from Google Drive correctly so that I'm not really uploading it through the API at all, or at least not correctly. - Chris
Thank you for replying. In the document, the method of request couldn't be found. So I think that it is required to do several tests. For example, the document says that Message text (Media or Text is required). and Message image (Media or Text is required).. From this, when you include only 'upload': DriveApp.getFileById("###").getBlob() or 'upload': DriveApp.getFileById("###").getBlob().getBytes(), what result will you get? - Tanaike

1 Answers

1
votes

The official document says that Message text (Media or Text is required) and Message image (Media or Text is required). From this, please try to test as following modification.

Modified request body:

var formData = {
  upload: DriveApp.getFileById("###").getBlob()
};
  • I thought that from the official document, when both 'upload' and 'text' are used, only 'text' might be used.
  • And also, from your tested result, it was found that the request body is required to be sent as the form data.

Reference: