-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebphone.js
252 lines (221 loc) · 6.75 KB
/
webphone.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
var {xhrsend, parseJSON, Pubsub, randomString} = require('./common.js')
const WebRTCConn = require('./webrtc.js')
var Realtime = require('./realtime.js')
function WebPhone(access_token, realtime) {
var pubsub = new Pubsub()
var $audio = null
// chuẩn bị thẻ audio để nghe được
function createAudioTag() {
if ($audio) return
if (!document.createElement) return // mobile
$audio = document.createElement('audio')
$audio.id = 'web_call_audio'
$audio.style = 'display: none'
$audio.autoplay = 'autoplay'
document.body.appendChild($audio)
}
createAudioTag()
access_token = access_token || ''
if (!access_token) throw 'invalid_access_token'
var accid = access_token.split('_')[0]
if (!accid.startsWith('ac')) throw 'invalid_access_token'
var agid = access_token.split('_')[1]
if (!agid.startsWith('ag')) throw 'invalid_access_token'
if (!realtime)
realtime = new Realtime('https://api.subiz.com.vn/rt/0/', {
getAccessToken: function () {
return new Promise(function (rs) {
rs(access_token)
})
},
})
var webrtcconn = new WebRTCConn({
accid: accid,
agid: agid,
access_token: access_token,
realtime,
env: window,
onEvent: function (ev) {
// publish call status event
if (!(ev.type || '').startsWith('call')) return
if (!ev.data || !ev.data.call_info) return
let callid = ev.data.call_info.call_id
if (!callid) return
pubsub.emit('event', ev)
},
onTrack: function (event) {
if (event.track.kind != 'audio') return
$audio.srcObject = event.streams[0]
},
})
var _numbers = []
var _fetchNumbers = function () {
xhrsend(
'get',
`https://api.subiz.com.vn/4.0/integrations?x-access-token=${access_token}`,
undefined,
function (body, code) {
setTimeout(_fetchNumbers, 120000)
if (code != 200) return
var integrations = parseJSON(body)
if (!integrations || !integrations.length) return
var numbers = []
for (var i = 0; i < integrations.length; i++) {
var integration = integrations[i] || {}
if (integration.connector_type == 'call' && integration.state == 'activated') {
var number = integration.id.split('.')[1]
numbers.push(number)
}
}
_numbers = numbers
},
)
}
_fetchNumbers()
this.onEvent = function (cb) {
return pubsub.on('event', cb)
}
this.sendDtmf = function (key, callid) {
return webrtcconn.sendDtmf(key, callid)
}
this.transferCall = function (number, callid) {
return webrtcconn.transferCall(number, callid)
}
this.hangupCall = function (callid) {
if (!callid) callid = current_call_id
return webrtcconn.hangupCall(callid)
}
var me = this
this.isMicAllowed = function () {
return me.checkMic().then(function (out) {
return out.result
})
}
var micStream = undefined
this.getMicroStream = function () {
return micStream
}
this.checkMic = function () {
const timeout = new Promise(function (rs, rj) {
return setTimeout(rs, 500, 'Not_authorized')
})
var microPermission = getMicroPermissions()
return {
timeout: Promise.race([timeout, microPermission]),
result: getMicroPermissions(),
}
}
var getMicroPermissions = function () {
return new Promise(function (rs) {
navigator.mediaDevices
.getUserMedia({audio: true, video: false})
.then(function (stream) {
micStream = stream
rs(micStream)
})
.catch(function (err) {
console.log('REJECT VIDEO PERMISSSIONN', err)
micStream = undefined
rs(undefined)
})
})
}
this.answerWebCall = function (callid) {
return new Promise(function (rs) {
getMicroPermissions().then(function (stream) {
current_call_id = callid
webrtcconn.answerCall(callid, stream).then(function (out) {
if (out.error) return rs({error})
rs(body)
})
})
})
}
this.getCall = function (callid) {
return webrtcconn.matchCall(callid)
}
var base = '0123456789abcdef'
// makeCall(number, [] || undefined || '', streamPm) // fallback all number
// makeCall(number, ['a','b','c'], streamPm) // fallback specific number
// makeCall(number, function() {}, streamPm) //
this.makeCall = function (number, fromnumbers, streamPm, _a, _b, _state) {
if (!_state) _state = {}
if (!fromnumbers || (Array.isArray(fromnumbers) && fromnumbers.length == 0)) {
fromnumbers = _numbers
}
if (typeof fromnumbers === 'string' || fromnumbers instanceof String || typeof value === 'number') {
fromnumbers = [fromnumbers + '']
}
// '11edc52b-2918-4d71-9058-f7285e29d894'
var callid = `${randomString(8, base)}-${randomString(4, base)}-${randomString(4, base)}`
callid += `-${randomString(4, base)}-${randomString(12, base)}`
current_call_id = callid
if (!streamPm) streamPm = getMicroPermissions()
let callnumber = chooseCallNumber(fromnumbers, _state.called)
if (!callnumber)
return (
_state.last_result || {
start: Date.now(),
ended: Date.now(),
direction: 'outbound',
account_id: accid,
to_number: number,
error: 'out_of_number',
id: callid,
hangup_code: 'failure',
status: 'ended',
}
)
return webrtcconn.makeCall(number, callnumber, streamPm, callid).then((out) => {
if (out.error) return out
if (out.hangup_code == 'cancel') return out
if (out.answered > 0) return out
if (out.hangup_code == 'terminated' || out.hangup_code == 'failure' || out.hangup_code == 'congestion') {
if (!_state.called) _state.called = {}
_state.called[callnumber] = true
_state.last_result = out
return this.makeCall(number, fromnumbers, streamPm, _a, _b, _state)
}
return out
})
}
var current_call_id = ''
this.getCurrentCallId = function () {
return current_call_id
}
this.getCurrentCall = function () {
if (current_call_id) {
var call = webrtcconn.matchCall(current_call_id)
if (call && call.status != 'ended') return call
}
var myOutgoingCall = Object.values(webrtcconn.matchCall()).find(function (call) {
var isOutboundCall = call.direction == 'outbound' || call.direction == 'outgoing'
return (call.status == 'dialing' || call.status == 'active') && isOutboundCall
})
if (myOutgoingCall) {
current_call_id = myOutgoingCall.call_id || ''
return myOutgoingCall
}
var dialingIncomingCall = Object.values(webrtcconn.matchCall()).find(function (call) {
return call.status == 'dialing' && (call.direction == 'incoming' || call.direction == 'inbound')
})
if (dialingIncomingCall) {
current_call_id = dialingIncomingCall.call_id || ''
return dialingIncomingCall
}
current_call_id = ''
return undefined
}
}
window.SbzWebPhone1 = WebPhone
module.exports = WebPhone
function chooseCallNumber(numbers, called) {
if (!called) called = {}
if (!numbers || !numbers.length) return ''
for (var i = 0; i < numbers.length; i++) {
var number = numbers[i]
if (called[number]) continue
return number
}
return ''
}