-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
195 lines (181 loc) · 5.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var RC = require('ringcentral')
var fs = require('fs')
require('dotenv').load()
var async = require("async");
var rcsdk = null
if (process.env.MODE == "production"){
rcsdk = new RC({
server:RC.server.production,
appKey: process.env.CLIENT_ID_PROD,
appSecret:process.env.CLIENT_SECRET_PROD
})
}else{
rcsdk = new RC({
server:RC.server.sandbox,
appKey: process.env.CLIENT_ID_SB,
appSecret:process.env.CLIENT_SECRET_SB
})
}
var platform = rcsdk.platform()
var subscription = rcsdk.createSubscription()
subscription.on(subscription.events.notification, presenceEvent)
var usersList = []
login()
function login(){
var un = ""
var pwd = ""
if (process.env.MODE == "production"){
un= process.env.USERNAME_PROD,
pwd= process.env.PASSWORD_PROD
}else{
un= process.env.USERNAME_SB,
pwd= process.env.PASSWORD_SB
}
platform.login({
username:un,
password:pwd
})
.then(function(resp){
checkExistingSubscription()
})
.catch(function(e){
console.log(e)
throw e
})
}
function checkExistingSubscription(){
fs.readFile('subscriptionId.txt', 'utf8', function (err, id) {
if (err) {
subscribeForNotification()
}else{
removeRegisteredSubscription(id)
}
});
}
function removeRegisteredSubscription(id) {
platform.delete('/subscription/' + id)
.then(function (response) {
console.log("deleted: " + id)
subscribeForNotification()
})
.catch(function(e) {
console.error(e.toString());
subscribeForNotification()
});
}
function subscribeForNotification(){
var eventFilter = ['/restapi/v1.0/account/~/presence']
subscription.setEventFilters(eventFilter)
.register()
.then(function(resp){
console.log('Ready for getting account presense events')
var json = resp.json();
fs.writeFile("subscriptionId.txt", json.id, function(err) {
if(err)
console.log(err);
else
console.log("SubscriptionId " + json.id + " is saved.");
});
})
.catch(function(e){
throw e
})
}
function presenceEvent(msg){
var user = {}
user['extensionId'] = msg.body.extensionId
user['telephonyStatus'] = msg.body.telephonyStatus
user['startTime'] = ""
checkTelephonyStatusChange(user)
}
function checkTelephonyStatusChange(user){
var newUser = true
for (var i=0; i<usersList.length; i++){
if (usersList[i].extensionId == user.extensionId){
console.log("OLD -> NEW: " + usersList[i].telephonyStatus + " -> " + user.telephonyStatus)
newUser = false
if (usersList[i].telephonyStatus == "NoCall" && user.telephonyStatus == "Ringing"){
usersList[i].telephonyStatus = user.telephonyStatus
usersList[i].startTime = createStartTime()
console.log("ExtensionId " + usersList[i].extensionId + " has an incoming call")
break
}
if (usersList[i].telephonyStatus == "Ringing" && user.telephonyStatus == "CallConnected"){
usersList[i].telephonyStatus = user.telephonyStatus
console.log("ExtensionId " + usersList[i].extensionId + " has a accepted a call")
break
}
if (usersList[i].telephonyStatus == "Ringing" && user.telephonyStatus == "NoCall"){
usersList[i].telephonyStatus = user.telephonyStatus
console.log("ExtensionId " + usersList[i].extensionId + " has a missed call")
break
}
if (usersList[i].telephonyStatus == "CallConnected" && user.telephonyStatus == "NoCall"){
usersList[i].telephonyStatus = user.telephonyStatus
var date = new Date()
var stopTime = date.toISOString()
stopTime = stopTime.replace('/', ':')
console.log("ExtensionId " + usersList[i].extensionId + " has a terminated call")
// wait for 20 secs then check for call recordings
setTimeout(function(){
readExtensionCallLogs(usersList[i].extensionId, usersList[i].startTime, stopTime)
}, 20000)
break
}
}
}
if (newUser){
console.log("NEW USER: " + " -> " + user.telephonyStatus)
if (user.telephonyStatus == "Ringing"){
user.startTime = createStartTime()
console.log("ExtensionId " + user.extensionId + " has an incoming call")
}
usersList.push(user)
}
}
function createStartTime(){
var date = new Date()
var time = date.getTime()
// make 10 secs to offset some delay in response
var lessXXSeconds = time - 10000
var from = new Date(lessXXSeconds)
var dateFrom = from.toISOString()
return dateFrom.replace('/', ':')
}
function readExtensionCallLogs(extensionId, startTime, stopTime){
var endpoint = '/account/~/extension/'+ extensionId +'/call-log'
var params = {}
params['dateFrom'] = startTime
params['dateTo'] = stopTime
params['recordingType'] = 'All'
platform.get(endpoint, params)
.then(function(resp){
async.each(resp.json().records,
function(record, callback){
console.log("THIS CALL HAS A RECORDING: " + record.recording.contentUri)
saveAudioFile(record)
},
function(err){
console.log("No call with call recording within this period of time.")
}
);
})
.catch(function(e){
var err = e.toString();
console.log(err)
})
}
function saveAudioFile(record){
platform.get(record.recording.contentUri)
.then(function(res) {
return res.response().buffer();
})
.then(function(buffer) {
var destFile = './recordings/' + record.recording.id + '.mp3'
fs.writeFileSync(destFile, buffer);
console.log("CALL RECORDING SAVED AT: " + destFile)
})
.catch(function(e){
console.log(e)
})
}