Skip to main content

I’ve started to write one, but thought I’d ask.  

If it could just read the detailed version of call logs and dump them to CSV.   Bonus points for something that will do SMS. 

 

You can find lots of reusable codes from this project.

To read the call log, check these 2 methods

https://github.com/PacoVu/ringcentral-call-log/blob/master/usershandler.js#L377

https://github.com/PacoVu/ringcentral-call-log/blob/master/usershandler.js#L503

To parse the call records and write to a .CSV file, check this method

https://github.com/PacoVu/ringcentral-call-log/blob/master/usershandler.js#L577

 

It’s a bit complicated with other app parameters but it would give you some ideas how to read and parse  the data and write to a file.


Thank you!  This may come in very helpful.  

Is this too dated?  It doesn’t seem to work. 

I believe you had a hand in it. :) 


Yes, that sample codes are out dated. Here is the revised one

const RingCentral = require('@ringcentral/sdk').SDK
const fs = require('fs')

const RINGCENTRAL_CLIENTID = "Your app client id"
const RINGCENTRAL_CLIENTSECRET = "Your app client secret"
const RINGCENTRAL_SERVER = 'https://platform.ringcentral.com'
const RC_USER_JWT="Your super admin user JWT"


const rcsdk = new RingCentral({
server: RINGCENTRAL_SERVER,
clientId: RINGCENTRAL_CLIENTID,
clientSecret: RINGCENTRAL_CLIENTSECRET
})

var platform = rcsdk.platform();

console.log("Logging in ...")
platform.login({ jwt: RC_USER_JWT })

platform.on(platform.events.loginSuccess, async function(e){
console.log("Login success")
await readCallLog()
});


async function readCallLog(){
console.log("timed")
var date = new Date()
var time = date.getTime()
// 1 week old logs
var lessXXMin = time - (7 * 24 * 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 = ""
try {
var resp = await platform.get('/restapi/v1.0/account/~/call-log', params)
var json = await resp.json()
if (json.records.length > 0){
// Check the API documentation for call log data fields then add whatever you are interested
var cvs = 'id,startTime,duration,type,direction,action,result,to_name,from_name,transport'
for (var record of json.records){
//console.log(JSON.stringify(record))
cvs += "\n"
cvs += record.id + ','
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
}
// 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(e){
console.log(e.message)
}
}

 


Okay, I got the revised code working, however it did not log 7 days back.  It seemed to only log today’s calls.  

I changed some of the code to reflect 30 days ago → 

	async function readCallLog(){
console.log("timed")
var date = new Date()
var time = date.getTime()
// 30 days ago
var thirtyDaysAgo = time - (30 * 24 * 60 * 60 * 1000)
var from = new Date(thirtyDaysAgo)
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)

Same thing.  It works, logs in, outputs to csv, but it only logs today’s calls. 

Output of command node node.js

Logging in ...
Login success
timed
date from: 2024-08-26T18:39:16.978Z
date to: 2024-09-25T18:39:16.978Z
CSV file created successfully.
 

The csv file ONLY contians the calls for 2024-09-25

Am I missing something?  Thanks in advance!!! :)


You have to further develop the code to handle paginations. Set the perPage value to max 250 records per page, then check the next page token to read the next page until no more next page. Please also mind the RingCentral data retention policy and if your account has its own HIPPA policy.

This is an example how to detect and read next page. Remember to fine tune and further develop it.

async function readCallLog(){
console.log("timed")
var date = new Date()
var time = date.getTime()
// 1 week old logs
var lessXXMin = time - (7 * 24 * 60 * 60 * 1000)
var from = new Date(lessXXMin)
var dateFrom = from.toISOString()
var dateTo = date.toISOString()

// See the API reference for more request parameters
var params = {}
paramsm'type'] = 'Voice'
paramsm'view'] = 'Detailed'
paramsm'dateFrom'] = dateFrom.replace('/', ':')
paramsm'dateTo'] = dateTo.replace('/', ':')
paramsm'perPage'] = 250
console.log("date from: " + params.dateFrom)
console.log("date to: " + params.dateTo)
var recordingId = ""
try {
var resp = await platform.get('/restapi/v1.0/account/~/call-log', params)
var jsonObj = await resp.json()
if (jsonObj.records.length > 0){
parseRecords(jsonObj.records)
var navigationObj = jsonObj.navigation
if (navigationObj.hasOwnProperty("nextPage")){
await read_calllog_nextpage(navigationObj.nextPage.uri)
}else{
console.log("readCallLog DONE - no next page => Write to file")
writeToFile()
}
}else {
console.log("No log");
// Set timer to read again after some delay
}
} catch(e){
console.log(e.message)
}
}

async function read_calllog_nextpage(url){
console.log("read_calllog_nextpage", url)
var resp = await platform.get(url)
var jsonObj = await resp.json()

if (jsonObj.records.length > 0){
parseRecords(jsonObj.records)

var navigationObj = jsonObj.navigation
if (navigationObj.hasOwnProperty("nextPage")){
console.log("Read next page after 2 seconds")
setTimeout(function(){
read_calllog_nextpage(navigationObj.nextPage.uri)
}, 2000)
}else{
console.log("read_calllog_nextpage DONE - no next page => Write to file")
// add to your CRM or other db
// for demo, I write to a file
writeToFile()
}
}
}

var csvContent = 'id,startTime,duration,type,direction,action,result,to_name,from_name,transport'

function parseRecords(records){
// Check the API documentation for call log data fields then add whatever you are interested
for (var record of records){
csvContent += "\n"
csvContent += record.id + ','
csvContent += record.startTime + ','
csvContent += record.duration + ','
csvContent += record.type + ','
csvContent += record.direction + ','
csvContent += record.action + ','
csvContent += record.result + ','
if (record.to.hasOwnProperty('name'))
csvContent += record.to.name + ','
else
csvContent += 'null,'
if (record.hasOwnProperty('from')){
if (record.from.hasOwnProperty('name'))
csvContent += record.from.name + ','
else
csvContent += 'null,'
}else
csvContent += 'null,'
csvContent += record.transport
}
}

function writeToFile(){
fs.writeFileSync('call_log.csv', csvContent)
}

 


Yes!  Okay the paginations helped this!  I really appreciate it.  All seems to be working thus far!   Thank you!


Hey do you have a similar SMS log node JS code available?  I really appreciate it!


Hey do you have a similar SMS log node JS code available?  I really appreciate it!

For SMS log, the logic to read the message store, iterate through all pages, parse the data in the records array are pretty much similar to the ones for call logs.

However, the difference between the call log data access control and the message store access control is that for call log, you can authenticate your app with a super admin user and you can call the list company call records API to get the entire company call logs or you can call the list user records API to get the call log of a particular extension. While for the message store, there is no company level API to list the entire company message logs. The super admin user can read the message store of other user extension but you have to call the list messages API for every user in the company in order to export the entire company message logs. If you want to export the message logs for the entire company and the number of user extensions in your company is large (let’s say above 300), you should consider using the message store export API instead. You can check out this article to learn more about the export API.

Remember that the message store include SMS, Voicemail and Fax messages as well, so use the API query to filter out the message type you don’t want to export.


Okay, for clarity, I don’t want to see the messages.  Just want to log file that you can get (as a super admin) from the portal.  

Just the basic info :

Direction

Type

Message Type

From

To

Date / Time

 

The actual messages my guys will show me from their desktop.  It would be way too much info. :) 

Just need the SMS log data as I would get it from the admin portal. 


Okay, for clarity, I don’t want to see the messages.  Just want to log file that you can get (as a super admin) from the portal.  

Just the basic info :

Direction

Type

Message Type

From

To

Date / Time

 

The actual messages my guys will show me from their desktop.  It would be way too much info. :) 

Just need the SMS log data as I would get it from the admin portal. 

Well, the API returns everything and if you don’t need something, just ignore that data field.


Reply