Skip to main content

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?


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);
});
});
}

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?




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)
}
}

Thank you! That did the trick.


Reply