-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
146 lines (124 loc) · 4.23 KB
/
example.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
const namiLib = require('nami')
const NamiSaga = require('../src/NamiSaga')
const { take, cancel, call, fork, race } = require('redux-saga/effects')
class NamiSagaExample extends NamiSaga {
/**
* Open connection
*
* @returns {Promise}
*/
open () {
return this.runSaga(this._sagaOpen.bind(this))
}
/**
* Make a call and get the results (startTime, answered, pickupTime, hangupTime)
* Rejects if there's any error
*
* @param {Action} originateAction
* @returns {Promise}
*/
originate (originateAction) {
return this.runSaga(this._sagaWatchConnection.bind(this), // watch for connection error
this._sagaOriginate.bind(this), originateAction) // send originate action and watch for call progress
}
/**
* Opens connection. Throws if connection or authorization fails.
*/
* _sagaOpen () {
yield fork([this, super.open])
let events = yield race([
take('namiConnected'),
take('namiConnectionClose'),
take('namiInvalidPeer'),
take('namiLoginIncorrect')
])
if (events[0]) { // success (namiConnected)
return events[0]
} else { // error
throw events.find(e => !!e)
}
}
/**
* Run saga and make sure there's no connection error during saga's execution
*/
* _sagaWatchConnection (saga, ...args) {
let [connectionClosed, sagaResult] = yield race([
take('namiConnectionClose'),
call(saga, ...args)
])
if (connectionClosed) throw new Error('Connection closed')
return sagaResult
}
/**
* Send `Originate` action and listen to events to get the call result
* (answered, startTime, pickupTime, hangupTime)
*
* @param {Action} originateAction
* @returns {Object}
*/
* _sagaOriginate (originateAction) {
let callResult = {
startTime: new Date(),
answered: false
}
// Send `Originate` action
originateAction.variable = `call=${originateAction.ActionID}`
let originateResponse = yield call([this, this.send], originateAction)
if (originateResponse.response !== 'Success') throw new Error('Originate failed')
let callId = originateAction.ActionID.toString()
// Catch `VarSet` event to obtain channel ID associated with the call
let varSet = yield take((a) => a.type === 'namiEventVarSet' && a.event.variable === 'call' && a.event.value === callId)
let channelId = varSet.event.channel
// Catch `OriginateResponse` event
// (`fork` because `OriginateResponse` event may not occur in some circumstances)
let originateResponseTask = yield fork(function * () {
let originateResponse = yield take((a) => a.type === 'namiEventOriginateResponse' &&
(a.event.channel === channelId || a.event.actionid === callId))
if (originateResponse.event.response === 'Success') {
callResult.answered = true
callResult.pickupTime = new Date()
}
})
// Catch `Hangup` event, call is over
yield take(a => a.type === 'namiEventHangup' && a.event.channel === channelId)
callResult.hangupTime = new Date()
// Cancel `originateResponseTask` (in case `OriginateResponse` event has not occurred)
yield cancel(originateResponseTask)
return callResult
}
}
// --------------------------------------------------------------------------------------------
// -- Usage example ---------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------
let nami = new NamiSagaExample({ /* config */ })
nami.open()
.then(() => {
console.log('Connected')
})
.then(() => {
console.log('Making a call...')
// Call and play an audio file
let originateAction = Object.assign(new namiLib.Actions.Originate(), {
channel: 'SIP/xxxxxxx/xxxxxxxx',
application: 'Playback',
data: 'ru/vm-options',
async: 'true'
})
return nami.originate(originateAction)
})
.then((callResult) => {
console.log({callResult})
/* {
startTime: 2017-12-18T04:27:16.340Z,
answered: true,
pickupTime: 2017-12-18T04:27:25.384Z,
hangupTime: 2017-12-18T04:27:27.449Z
} */
})
.then(() => {
console.log('Disconnecting...')
nami.close()
})
.catch(e => {
console.log('Error', e)
})