question

Andrew Prokop avatar image
Andrew Prokop asked ·

Retrieve fax image,How do I retrieve contents of fax via api

Hi all. I am using the javascript SDK to retrieve a fax. I call the following:

var uri = 'https://media.devtest.ringcentral.com/restapi/v1.0/account/296784004/extension/296784004/message-store/9609075004/content/9609075004'; // discovered via get all faxes API

var resp = await platform.get(uri)

I believe I have now received the binary of the fax image. How do I save that to a file?


fax
1 |1500 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image
Phong Vu answered ·

Use this sample code

var async = require('async')
var fs = require('fs')
var https = require('https');

async function read_message_store_fax(){
  try {
    var resp = await platform.get('/restapi/v1.0/account/~/extension/~/message-store', {
          dateFrom: '2021-01-01T00:00:00.000Z',
          direction: ['Inbound'],
          messageType: ['Fax']
        })
    var jsonObj = await resp.json()
    for (var record of jsonObj.records){
      console.log(record)
      if (record.hasOwnProperty('attachments')){
        async.each(record.attachments,
          function(attachment, callback){
              var fileName = attachment.id + "_fax_attachment"
              var fileNameExt = attachment.contentType.split("/")
              fileName += "." + fileNameExt[1]
              download_content(attachment.uri, fileName)
              callback()
            })
      }
    }
  }catch (e){
    console.log(e.message)
  }
}

async function download_content(contentUri, fileName){
    var uri = platform.createUrl(contentUri, {addToken: true});
    download(uri, fileName, function(){
      console.log("Save atttachment to the local machine.")
    })
}

const download = function(uri, dest, cb) {
    var file = fs.createWriteStream(dest);
    var request = https.get(uri, function(response) {
  response.pipe(file);
  file.on('finish', function() {
      file.close(cb);
  });
    });
}
1 |1500 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Andrew Prokop avatar image
Andrew Prokop answered ·

Thank you. I can retrieve all the records and "pdf" files are created. However, each one contains the following:


{

"errorCode" : "AGW-401",

"message" : "Authorization header is not specified",

"errors" : [ {

"errorCode" : "AGW-401",

"message" : "Authorization header is not specified"

} ]

}


I am new to RingCentral programming, but it appears that the token is added to contentUri. Perhaps this isn't working because of my authentication scheme and the way I created my application. I created the application as Other Non-UI (e.g. cronjob, daemon, command line) .

I do this in my code:

var rcsdk = new RC({

server: RINGCENTRAL_SERVER,

clientId: RINGCENTRAL_CLIENTID,

clientSecret: RINGCENTRAL_CLIENTSECRET

});

var platform = rcsdk.platform();


This works to retrieve the records, but not to retrieve the contents.

Any suggestions?



1 comment
1 |1500 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Try this then. Remember to take care of API rate limit!

https://medium.com/ringcentral-developers/ringcentral-api-rate-limit-explained-2280fe53cb16

var async = require('async')
var fs = require('fs')

async function read_message_store_fax(){
  try {
    var resp = await platform.get('/restapi/v1.0/account/~/extension/~/message-store', {
          dateFrom: '2021-01-01T00:00:00.000Z',
          direction: ['Inbound'],
          messageType: ['Fax']
        })
    var jsonObj = await resp.json()
    for (var record of jsonObj.records){
      console.log(record)
      if (record.hasOwnProperty('attachments')){
        async.each(record.attachments,
          async function(attachment){
              var fileName = attachment.id + "_fax_attachment"
              var fileNameExt = attachment.contentType.split("/")
              fileName += "." + fileNameExt[1]
              var res = await platform.get(attachment.uri)
              var buffer = await res.buffer();
              fs.writeFileSync(fileName, buffer)
            })
      }
    }
  }catch (e){
    console.log(e.message)
  }
}
0 Likes 0 ·
Andrew Prokop avatar image
Andrew Prokop answered ·

Thank you! That did the trick.

1 |1500 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys