2

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.

Chris
  • 473
  • 5
  • 18
  • 1
    Does this answer your question? [How to upload an image to Discord using Google Apps Script and a Discord Webhook?](https://stackoverflow.com/questions/52509999/how-to-upload-an-image-to-discord-using-google-apps-script-and-a-discord-webhook) – TheMaster Feb 22 '20 at 06:06
  • Can you provide the URL of document of specification of `external API`? And also, can you provide the error message? – Tanaike Feb 22 '20 at 06:57
  • @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 Feb 22 '20 at 07:27
  • @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 Feb 22 '20 at 07:31
  • 1
    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 Feb 22 '20 at 07:52
  • @Tanaike - OMG! Your first suggestion worked! I thought I tried that for sure. You can see above in the code portion of my original post the following: // 'upload': DriveApp.getFileByID("1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_").getBlob() // And tried this I THOUGHT I tried that, and I know that everything I tried did not work, as the image has never showed up yet along side the text, until I tried it after you asked what it would do if I tried it. Thank you so much!!! – Chris Feb 22 '20 at 08:12
  • Thank you for replying. I'm glad your issue was resolved. When your issue was resolved, can you post it as an answer? By this, it will be useful for other users who have the same issue. – Tanaike Feb 22 '20 at 08:21
  • @Tanaike I will do so. As this is my first time on Stack Overflow, is there a way for me to give you the credit for the answer? – Chris Feb 22 '20 at 08:23
  • @Tanaike, yes I can accept it if you post it as an answer. – Chris Feb 22 '20 at 08:26
  • I posted it just now. Could you please confirm it? – Tanaike Feb 22 '20 at 08:31

1 Answers1

1

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165