How to pull call logs with an API

  • 2
  • Question
  • Updated 1 year ago
  • Answered
I need to get RC call logs in a csv format every 15 minutes or less to my CRM can someone help with this?
Photo of YG Tech

YG Tech

  • 170 Points 100 badge 2x thumb

Posted 1 year ago

  • 2
Photo of Phong Vu

Phong Vu, Devangelist

  • 3,212 Points 3k badge 2x thumb
Hi,

Are you looking for a contractor to help with the work or are you asking for sample code showing how to implement that? If you need sample code, let me know what programming language do you expect.

+ Phong
Photo of YG Tech

YG Tech

  • 170 Points 100 badge 2x thumb
I was just asking for sample code; really any language would be fine. But if you could get this up and working for me very quickly we might be able to pay for it.
Photo of Phong Vu

Phong Vu, Devangelist

  • 3,212 Points 3k badge 2x thumb
Hi,

Here is the working code in Node JS. 

You need to install the RingCentral SDK for Node JS
npm install ringcentral --save

and install the dotenv so you can use the .env to keep your app and login credentials

Code:
var RC = require('ringcentral')
require('dotenv').load()

var rcsdk = new RC({
  server:process.env.SERVER,
  appKey: process.env.APP_KEY,
  appSecret:process.env.APP_SECRET
})

var platform = rcsdk.platform()

login()
function login() {
  platform.login({
    username:process.env.USERNAME,
    password:process.env.PASSWORD
  })
  .then(function(resp){
      readCallLog()
      setInterval(function(){
        readCallLog()
      }, 60 * 15 * 1000); // repeat reading call-log every 15 mins
  })
  .catch(function(e){
    throw e
  })
}

function readCallLog(){
  var date = new Date()
  var time = date.getTime()
  // 15-min period
  var less15Min = time - (60 * 15 * 1000)
  var from = new Date(less15Min)
  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(params.dateFrom)
  console.log(params.dateTo)
  platform.get('/account/~/extension/~/call-log', params)
  .then(function(resp){
    var json = resp.json()
    if (json.records.length > 0){
      // 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){
        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.name != undefined)
          cvs += record.to.name + ','
        else
          cvs += 'null,'
          if (record.from.name != undefined)
            cvs += record.from.name + ','
          else
            cvs += 'null,'
        cvs += record.transport
      }
      var fs = require('fs')
      // add to your CRM db
      // for demo, I write the data to a file
      fs.writeFile('call_log_'+dateTo+'.csv', cvs, function(err) {
        if(err)
          console.log(err);
        else
          console.log("call log is saved.");
      })
    }
  })
  .catch(function(e){
    throw e
  })
}

I am here to help you. So no payment :)

Bests,
Phong
Photo of YG Tech

YG Tech

  • 170 Points 100 badge 2x thumb
Thank you so much for this you're Awesome.
Photo of Steve Ermish

Steve Ermish

  • 282 Points 250 badge 2x thumb
Hello, this is very helpful.  The code appears to run by displaying the date to console, however a CSV file never saves.
Photo of Steve Ermish

Steve Ermish

  • 282 Points 250 badge 2x thumb
Thanks, yes I mean the header.  When using var cvs='' it just creates a blank first row.  I need the header to be removed completely.  Is that possible?
(Edited)
Photo of Steve Ermish

Steve Ermish

  • 282 Points 250 badge 2x thumb
Hi Phong, do you know how to just remove the header row completely?
Photo of Phong Vu

Phong Vu, Devangelist

  • 1,936 Points 1k badge 2x thumb
I showed you above, just specify an empty string var cvs=''; before appending data row to it. Also move the cvs += "\r\n" to the end of each loop and don't add it to the last row.
Photo of Steve Ermish

Steve Ermish

  • 282 Points 250 badge 2x thumb
Ok thank you, once I moved the \r\n to the end, it removed the first row.  After pulling logs for a few days, I noticed the CSV column data is misaligned 1 to 2 of every 1000 rows.  Any ideas what could cause that?
Photo of Phong Vu

Phong Vu, Devangelist

  • 1,936 Points 1k badge 2x thumb
You have to check what field is missing. Double check all your if () blocks must have an else, and make sure you add a "," to separate every field, be it having a value or not. Also double check your code where you escape commas "," in the value.
Photo of YG Tech

YG Tech

  • 170 Points 100 badge 2x thumb
Thank you all for the help we are working to implement this into our CRM. Thank you again for your help.
Photo of Don Jackson

Don Jackson

  • 160 Points 100 badge 2x thumb
 Phong Vu, this is working great, but it only pulls down 100 records maximum. I increase the time to more than 15 minutes and I sometimes have more than 100 calls in the time period. How do set the number of records to pull?
Photo of Phong Vu

Phong Vu, Devangelist

  • 2,596 Points 2k badge 2x thumb
Hi Don,

You can set the perPage value to > 100. The default value is 100.
https://developer.ringcentral.com/api-docs/latest/index.html#!#RefUserCallLog.html

Cheers,
Phong