Absolute novice needing help with an API. Help?

  • 1
  • Question
  • Updated 2 months ago
I am needing to have RC export a detailed call log for our business every hour or so (more frequently if possible) into either excel or Google Sheets and I would like this to be as automated a process as possible.  I have very little coding experience but I think this might be the best way to get this done with about breaking the bank on zapier.  Would someone be able to help get us set-up with something like this or at least walk me through how I could do it?
Photo of Matthew Nisbet

Matthew Nisbet

  • 92 Points 75 badge 2x thumb

Posted 3 months ago

  • 1
Photo of Phong Vu

Phong Vu, Devangelist

  • 3,192 Points 3k badge 2x thumb
Hi Matthew,

What you need is really doable with the call log API. If you are familiar with Node JS and can run it locally on your machine, this little code can help you to see how it works. You just need to login the developer portal and create an app with the read call log permission. App platform type will be Server only (no UI). Copy your app Client ID and Client Secret to use in 

var RC = require('ringcentral')
var rcsdk = new RC({
  server:"https://platform.devtest.ringcentral.com",
  appKey: YOU_APP_CLIENT_ID,
  appSecret:YOUR_APP_CLIENT_SECRET
})
var platform = rcsdk.platform()
login()
function login() {
  platform.login({
    username:process.env.USERNAME,
    password:process.env.PASSWORD
  })
  .then(function(resp){
    console.log("readCallLog by timer")
    readCallLog()
    setInterval(function(){
        readCallLog()
      }, 60 * 60 * 1000); // repeat reading call-log every 60 mins
  })
  .catch(function(e){
    console.log(e)
    throw e
  })
}
function readCallLog(){
  console.log("timed")
  var date = new Date()
  var time = date.getTime()
  // 60-min period
  var lessXXMin = time - (60 * 60 * 1000)
  var from = new Date(lessXXMin)
  var dateFrom = from.toISOString()
  var dateTo = date.toISOString()
  var params = {}
  // See the API reference for more request parameters
  params['type'] = 'Voice'
  params['view'] = 'Detailed'
  params['dateFrom'] = dateFrom.replace('/', ':')
  params['dateTo'] = dateTo.replace('/', ':')
  console.log("date from: " + params.dateFrom)
  console.log("date to: " + params.dateTo)
  var recordingId = ""
  platform.get('/account/~/extension/~/call-log', params)
  .then(function(resp){
    var json = resp.json()
    if (json.records.length > 0){
      var fs = require('fs')
      // Check the API documentation for call log data fields then add whatever you are interested
      var cvs = 'uri,startTime,duration,type,direction,action,result,to_name,from_name,transport'
      for (var record of json.records){
        //console.log(JSON.stringify(record))
        cvs += "\r\n"
        cvs += record.uri + ','
        cvs += record.startTime + ','
        cvs += record.duration + ','
        cvs += record.type + ','
        cvs += record.direction + ','
        cvs += record.action + ','
        cvs += record.result + ','
        if (record.to.hasOwnProperty('name'))
          cvs += record.to.name + ','
        else
          cvs += 'null,'
        if (record.hasOwnProperty('from')){
          if (record.from.hasOwnProperty('name'))
            cvs += record.from.name + ','
          else
            cvs += 'null,'
        }else
          cvs += 'null,'
        cvs += record.transport
        if (record.recording != undefined) {
          console.log("contentUri: " + record.recording.contentUri)
          recordingId = record.recording.id
          console.log(record.recording.contentUri)
          platform.get(record.recording.contentUri)
          .then(function(res) {
            console.log("ok")
            return res.response().buffer();
          })
          .then(function(buffer) {
            console.log("buffer")
            fs.writeFileSync(recordingId + '.mp3', buffer);
          })
          .catch(function(e){
            console.log(e)
            throw e
          })
        }
      }
      // add to your CRM  or other db
      // for demo, I write to a file
      fs.writeFile('call_log_'+dateTo+'.csv', cvs, function(err) {
        if(err)
          console.log(err);
        else
          console.log("call log is saved.");
      })
    }else {
      console.log("No log");
    }
  })
  .catch(function(e){
    throw e
  })
}
Let me know if you want in other programming languages.

+ Phong Vu 
Photo of Matthew Nisbet

Matthew Nisbet

  • 92 Points 75 badge 2x thumb
Thank you so much.  This will be extremely helpful I believe.  I have started the creation of the application.  I am trying to get it to hook up to the existing accounts of our business and I am honestly a bit lost there. 

Honestly, with enough time and effort put in, I am sure that I could get this in the long run, especially since you have done all the heavy lifting already it looks like but is this something we would potentially be able to hire you to provide a finished product for us or are there folks with RC that offer that service?


Photo of Phong Vu

Phong Vu, Devangelist

  • 3,010 Points 3k badge 2x thumb
Hi Matthew,

Let me see how we can help you further. For now, what kind of problem are you facing with trying the code. After you created an application and get the credentials, I believe that it is still in the sandbox environment. If you want to hookup to the existing accounts of your business, all you need to do is to submit the app for graduation to get the app approved for your production account. Then you will get the production Client ID and Client secret. Use them with this code.

var rcsdk = new RC({
  server:"https://platform.ringcentral.com",
  appKey: YOUR_APP_PRODUCTION_CLIENT_ID,
  appSecret:YOUR_APP_PRODUCTION_CLIENT_SECRET
})

Let me know your app name or your account email address so I can check your app.

+ Phong
Photo of Matthew Nisbet

Matthew Nisbet

  • 92 Points 75 badge 2x thumb
Phong, Ill be honest in that I am way over my head in doing this.  Are there any existing apps that you are familiar that would do this?  We have 20ish people calling on RC and I was having Zapier pull over call data for each individual person but that was getting crazy expensive with them so we need a cheaper option to get those call logs into either Excel or sheets.

Do you have any recommendations?
Photo of Phong Vu

Phong Vu, Devangelist

  • 3,010 Points 3k badge 2x thumb
Hi Matthew,

What you need is quite doable with a small program. I can help to build the app for you. But you need to give me detailed specs of what metadata do you want to fetch and save to a .csv periodically. If you want to proceed, please send a request to devsupport@ringcentral.com and mention about this.

+ Phong