question

yg-tech7387 avatar image
yg-tech7387 asked Phong Vu commented

How to pull call logs with an API

I need to get RC call logs in a csv format every 15 minutes or less to my CRM can someone help with this?

getting started
1 |3000

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
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
1 |3000

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

yg-tech7387 avatar image
yg-tech7387 answered
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.
1 |3000

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
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
1 |3000

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

yg-tech7387 avatar image
yg-tech7387 answered
Thank you so much for this you're Awesome.

1 |3000

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

steve-ermish avatar image
steve-ermish answered Phong Vu commented
Hello, this is very helpful.  The code appears to run by displaying the date to console, however a CSV file never saves.
32 comments
1 |3000

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

steve-ermish avatar image steve-ermish commented ·
I was able to get it to display the time without the / using this code.  It's probably more complicated than it needs to be, but it's working.  Thanks again for your help with this!

var today = new Date();
var date = today.getFullYear()+''+(today.getMonth()+1)+''+today.getDate()
var time = today.getHours() + '' + today.getMinutes() + '' + today.getSeconds()
var dateTime = date+' '+time

fs.writeFile('RingCentralCallLog'+dateTime+'.csv', cvs, function(err) {

1 Like 1 ·
steve-ermish avatar image steve-ermish commented ·
Thank you, this got it working!

if (record.hasOwnProperty('to')){
if (record.to.hasOwnProperty('phoneNumber'))
cvs += record.to.phoneNumber + ','
}

if (record.hasOwnProperty('from')){
if (record.from.hasOwnProperty('phoneNumber'))
cvs += record.from.phoneNumber + ','
}

if (record.hasOwnProperty('from')){
if (record.from.hasOwnProperty('name'))
cvs += record.from.name + ','
}

if (record.hasOwnProperty('from')){
if (record.to.hasOwnProperty('name'))
cvs += record.to.name + ','
}

This is just the standard /restapi/v1.0/account/~/call-log using the 'detailed' parameter.
It appears not all the data is available for every call.  Also, randomly(1 in 1000) the record.to.name includes a comma in the response which is why I put it at the end because it adds another column.

I've posted the working javascript and Postman collection here
https://github.com/steveermish/RingCentral-REST-API
1 Like 1 ·
Phong Vu avatar image Phong Vu ♦♦ commented ·
It looks like you don't have any record and that's why it does not enter the if (json.records.length > 0) loop. Otherwise, there might be some restriction in your system that prevent the fs.writeFile function to create a new file. Please double check and let me know. Phong
0 Likes 0 ·
steve-ermish avatar image steve-ermish commented ·
I had to reinstall the fs module and now it's working.  For some reason the file is saving with a / in the filename and is invalid.
For example, call_log_2017-10-12T13/18/19.018Z.csv.  Why would the time not use the same - as the date?

0 Likes 0 ·
Phong Vu avatar image Phong Vu ♦♦ commented ·
Hi Steve,
The file name I chose was just for convenient. And it works on MacOS. You can simply modify it e.g. var filename = dateTo.replace('/', '-'). The most important is the date and time format for the request dateFrom and dateTo parameters. They must be in ISO 8601 format e.g. 2017-10-12T18:07:52.534Z. That's why I replace the / with : 

params['dateFrom'] = dateFrom.replace('/', ':')
params['dateTo'] = dateTo.replace('/', ':')

0 Likes 0 ·
Show more comments
yg-tech7387 avatar image
yg-tech7387 answered
Thank you all for the help we are working to implement this into our CRM. Thank you again for your help.
1 |3000

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

don-jackson7565 avatar image
don-jackson7565 answered Phong Vu commented
  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?
1 comment
1 |3000

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 ♦♦ commented ·
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
0 Likes 0 ·

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