question

matthew-nisbet11707 avatar image
matthew-nisbet11707 asked Phong Vu commented

Absolute novice needing help with an API. Help?

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?
topic-default
1 |1000

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

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

matthew-nisbet11707 avatar image
matthew-nisbet11707 answered Phong Vu commented
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?


3 comments
1 |1000

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 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
0 Likes 0 ·
matthew-nisbet11707 avatar image matthew-nisbet11707 commented ·
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?
0 Likes 0 ·
Phong Vu avatar image Phong Vu ♦♦ commented ·
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
0 Likes 0 ·

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.