-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathSipClient.ts
More file actions
811 lines (754 loc) · 24.8 KB
/
SipClient.ts
File metadata and controls
811 lines (754 loc) · 24.8 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { Duration } from '@bufbuild/protobuf';
import type {
ListUpdate,
Pagination,
RoomConfiguration,
SIPHeaderOptions,
} from '@livekit/protocol';
import {
CreateSIPDispatchRuleRequest,
CreateSIPInboundTrunkRequest,
CreateSIPOutboundTrunkRequest,
CreateSIPParticipantRequest,
CreateSIPTrunkRequest,
DeleteSIPDispatchRuleRequest,
DeleteSIPTrunkRequest,
ListSIPDispatchRuleRequest,
ListSIPDispatchRuleResponse,
ListSIPInboundTrunkRequest,
ListSIPInboundTrunkResponse,
ListSIPOutboundTrunkRequest,
ListSIPOutboundTrunkResponse,
ListSIPTrunkRequest,
ListSIPTrunkResponse,
SIPDispatchRule,
SIPDispatchRuleDirect,
SIPDispatchRuleIndividual,
SIPDispatchRuleInfo,
SIPInboundTrunkInfo,
SIPMediaEncryption,
SIPOutboundConfig,
SIPOutboundTrunkInfo,
SIPParticipantInfo,
SIPTransport,
SIPTrunkInfo,
TransferSIPParticipantRequest,
UpdateSIPDispatchRuleRequest,
UpdateSIPInboundTrunkRequest,
UpdateSIPOutboundTrunkRequest,
} from '@livekit/protocol';
import type { ClientOptions } from './ClientOptions.js';
import { ServiceBase } from './ServiceBase.js';
import type { Rpc } from './TwirpRPC.js';
import { TwirpRpc, livekitPackage } from './TwirpRPC.js';
const svc = 'SIP';
/**
* @deprecated use CreateSipInboundTrunkOptions or CreateSipOutboundTrunkOptions
*/
export interface CreateSipTrunkOptions {
name?: string;
metadata?: string;
inbound_addresses?: string[];
inbound_numbers?: string[];
inbound_username?: string;
inbound_password?: string;
outbound_address?: string;
outbound_username?: string;
outbound_password?: string;
}
export interface CreateSipInboundTrunkOptions {
metadata?: string;
/** @deprecated - use `allowedAddresses` instead */
allowed_addresses?: string[];
allowedAddresses?: string[];
/** @deprecated - use `allowedNumbers` instead */
allowed_numbers?: string[];
allowedNumbers?: string[];
/** @deprecated - use `authUsername` instead */
auth_username?: string;
authUsername?: string;
/** @deprecated - use `authPassword` instead */
auth_password?: string;
authPassword?: string;
headers?: { [key: string]: string };
headersToAttributes?: { [key: string]: string };
// Map SIP response headers from INVITE to sip.h.* participant attributes automatically.
includeHeaders?: SIPHeaderOptions;
krispEnabled?: boolean;
mediaEncryption?: SIPMediaEncryption;
/** Maximum time for a call to ring in seconds. */
ringingTimeout?: number;
}
export interface CreateSipOutboundTrunkOptions {
metadata?: string;
transport: SIPTransport;
destinationCountry?: string;
/** @deprecated - use `authUsername` instead */
auth_username?: string;
authUsername?: string;
/** @deprecated - use `authPassword` instead */
auth_password?: string;
authPassword?: string;
headers?: { [key: string]: string };
headersToAttributes?: { [key: string]: string };
// Map SIP response headers from INVITE to sip.h.* participant attributes automatically.
includeHeaders?: SIPHeaderOptions;
mediaEncryption?: SIPMediaEncryption;
}
export interface SipDispatchRuleDirect {
type: 'direct';
roomName: string;
pin?: string;
}
export interface SipDispatchRuleIndividual {
type: 'individual';
roomPrefix: string;
pin?: string;
}
export interface CreateSipDispatchRuleOptions {
name?: string;
metadata?: string;
trunkIds?: string[];
hidePhoneNumber?: boolean;
attributes?: { [key: string]: string };
roomPreset?: string;
roomConfig?: RoomConfiguration;
}
export interface CreateSipParticipantOptions {
/** Optional SIP From number to use. If empty, trunk number is used. */
fromNumber?: string;
/** Optional identity of the SIP participant */
participantIdentity?: string;
/** Optional name of the participant */
participantName?: string;
/** Optional display name for the SIP participant */
displayName?: string;
/** Optional metadata to attach to the participant */
participantMetadata?: string;
/** Optional attributes to attach to the participant */
participantAttributes?: { [key: string]: string };
/** Optionally send following DTMF digits (extension codes) when making a call.
* Character 'w' can be used to add a 0.5 sec delay. */
dtmf?: string;
/** @deprecated use `playDialtone` instead */
playRingtone?: boolean;
/** If `true`, the SIP Participant plays a dial tone to the room until the phone is picked up. */
playDialtone?: boolean;
/** These headers are sent as-is and may help identify this call as coming from LiveKit for the other SIP endpoint. */
headers?: { [key: string]: string };
/** Map SIP response headers from INVITE to sip.h.* participant attributes automatically. */
includeHeaders?: SIPHeaderOptions;
hidePhoneNumber?: boolean;
/** Maximum time for the call to ring in seconds. */
ringingTimeout?: number;
/** Maximum call duration in seconds. */
maxCallDuration?: number;
/** If `true`, Krisp noise cancellation will be enabled for the caller. */
krispEnabled?: boolean;
/** If `true`, this will wait until the call is answered before returning. */
waitUntilAnswered?: boolean;
/** Optional request timeout in seconds. default 60 seconds if waitUntilAnswered is true, otherwise 10 seconds */
timeout?: number;
}
export interface ListSipDispatchRuleOptions {
/** Pagination options. */
page?: Pagination;
/** Rule IDs to list. If this option is set, the response will contains rules in the same order. If any of the rules is missing, a nil item in that position will be sent in the response. */
dispatchRuleIds?: string[];
/** Only list rules that contain one of the Trunk IDs, including wildcard rules. */
trunkIds?: string[];
}
export interface ListSipTrunkOptions {
/** Pagination options. */
page?: Pagination;
/** Trunk IDs to list. If this option is set, the response will contains trunks in the same order. If any of the trunks is missing, a nil item in that position will be sent in the response. */
trunkIds?: string[];
/** Only list trunks that contain one of the numbers, including wildcard trunks. */
numbers?: string[];
}
export interface SipDispatchRuleUpdateOptions {
trunkIds?: ListUpdate;
rule?: SIPDispatchRule;
name?: string;
metadata?: string;
attributes?: { [key: string]: string };
}
export interface SipInboundTrunkUpdateOptions {
numbers?: ListUpdate;
allowedAddresses?: ListUpdate;
allowedNumbers?: ListUpdate;
authUsername?: string;
authPassword?: string;
name?: string;
metadata?: string;
mediaEncryption?: SIPMediaEncryption;
}
export interface SipOutboundTrunkUpdateOptions {
numbers?: ListUpdate;
allowedAddresses?: ListUpdate;
allowedNumbers?: ListUpdate;
authUsername?: string;
authPassword?: string;
destinationCountry?: string;
name?: string;
metadata?: string;
mediaEncryption?: SIPMediaEncryption;
}
export interface TransferSipParticipantOptions {
playDialtone?: boolean;
headers?: { [key: string]: string };
/** Maximum time for the transfer destination to answer the call, in seconds. */
ringingTimeout?: number;
}
/**
* Client to access Egress APIs
*/
export class SipClient extends ServiceBase {
private readonly rpc: Rpc;
/**
* @param host - hostname including protocol. i.e. 'https://<project>.livekit.cloud'
* @param apiKey - API Key, can be set in env var LIVEKIT_API_KEY
* @param secret - API Secret, can be set in env var LIVEKIT_API_SECRET
* @param options - client options
*/
constructor(host: string, apiKey?: string, secret?: string, options?: ClientOptions) {
super(apiKey, secret);
const rpcOptions = options?.requestTimeout
? { requestTimeout: options.requestTimeout }
: undefined;
this.rpc = new TwirpRpc(host, livekitPackage, rpcOptions);
}
/**
* @param number - phone number of the trunk
* @param opts - CreateSipTrunkOptions
* @deprecated use `createSipInboundTrunk` or `createSipOutboundTrunk`
*/
async createSipTrunk(number: string, opts?: CreateSipTrunkOptions): Promise<SIPTrunkInfo> {
let inboundAddresses: string[] | undefined;
let inboundNumbers: string[] | undefined;
let inboundUsername: string = '';
let inboundPassword: string = '';
let outboundAddress: string = '';
let outboundUsername: string = '';
let outboundPassword: string = '';
let name: string = '';
let metadata: string = '';
if (opts !== undefined) {
inboundAddresses = opts.inbound_addresses;
inboundNumbers = opts.inbound_numbers;
inboundUsername = opts.inbound_username || '';
inboundPassword = opts.inbound_password || '';
outboundAddress = opts.outbound_address || '';
outboundUsername = opts.outbound_username || '';
outboundPassword = opts.outbound_password || '';
name = opts.name || '';
metadata = opts.metadata || '';
}
const req = new CreateSIPTrunkRequest({
name: name,
metadata: metadata,
inboundAddresses: inboundAddresses,
inboundNumbers: inboundNumbers,
inboundUsername: inboundUsername,
inboundPassword: inboundPassword,
outboundNumber: number,
outboundAddress: outboundAddress,
outboundUsername: outboundUsername,
outboundPassword: outboundPassword,
}).toJson();
const data = await this.rpc.request(
svc,
'CreateSIPTrunk',
req,
await this.authHeader({}, { admin: true }),
);
return SIPTrunkInfo.fromJson(data, { ignoreUnknownFields: true });
}
/**
* Create a new SIP inbound trunk.
*
* @param name - human-readable name of the trunk
* @param numbers - phone numbers of the trunk
* @param opts - CreateSipTrunkOptions
* @returns Created SIP inbound trunk
*/
async createSipInboundTrunk(
name: string,
numbers: string[],
opts?: CreateSipInboundTrunkOptions,
): Promise<SIPInboundTrunkInfo> {
if (opts === undefined) {
opts = {};
}
const req = new CreateSIPInboundTrunkRequest({
trunk: new SIPInboundTrunkInfo({
name: name,
numbers: numbers,
metadata: opts?.metadata,
allowedAddresses: opts.allowedAddresses ?? opts.allowed_addresses,
allowedNumbers: opts.allowedNumbers ?? opts.allowed_numbers,
authUsername: opts.authUsername ?? opts.auth_username,
authPassword: opts.authPassword ?? opts.auth_password,
headers: opts.headers,
headersToAttributes: opts.headersToAttributes,
includeHeaders: opts.includeHeaders,
krispEnabled: opts.krispEnabled,
mediaEncryption: opts.mediaEncryption,
ringingTimeout: opts.ringingTimeout
? new Duration({ seconds: BigInt(opts.ringingTimeout) })
: undefined,
}),
}).toJson();
const data = await this.rpc.request(
svc,
'CreateSIPInboundTrunk',
req,
await this.authHeader({}, { admin: true }),
);
return SIPInboundTrunkInfo.fromJson(data, { ignoreUnknownFields: true });
}
/**
* Create a new SIP outbound trunk.
*
* @param name - human-readable name of the trunk
* @param address - hostname and port of the SIP server to dial
* @param numbers - phone numbers of the trunk
* @param opts - CreateSipTrunkOptions
* @returns Created SIP outbound trunk
*/
async createSipOutboundTrunk(
name: string,
address: string,
numbers: string[],
opts?: CreateSipOutboundTrunkOptions,
): Promise<SIPOutboundTrunkInfo> {
if (opts === undefined) {
opts = {
transport: SIPTransport.SIP_TRANSPORT_AUTO,
};
}
const req = new CreateSIPOutboundTrunkRequest({
trunk: new SIPOutboundTrunkInfo({
name: name,
address: address,
numbers: numbers,
metadata: opts.metadata,
transport: opts.transport,
authUsername: opts.authUsername ?? opts.auth_username,
authPassword: opts.authPassword ?? opts.auth_password,
headers: opts.headers,
headersToAttributes: opts.headersToAttributes,
includeHeaders: opts.includeHeaders,
destinationCountry: opts.destinationCountry,
mediaEncryption: opts.mediaEncryption,
}),
}).toJson();
const data = await this.rpc.request(
svc,
'CreateSIPOutboundTrunk',
req,
await this.authHeader({}, { admin: true }),
);
return SIPOutboundTrunkInfo.fromJson(data, { ignoreUnknownFields: true });
}
/**
* @deprecated use `listSipInboundTrunk` or `listSipOutboundTrunk`
*/
async listSipTrunk(): Promise<Array<SIPTrunkInfo>> {
const req: Partial<ListSIPTrunkRequest> = {};
const data = await this.rpc.request(
svc,
'ListSIPTrunk',
new ListSIPTrunkRequest(req).toJson(),
await this.authHeader({}, { admin: true }),
);
return ListSIPTrunkResponse.fromJson(data, { ignoreUnknownFields: true }).items ?? [];
}
/**
* List SIP inbound trunks with optional filtering.
*
* @param list - Request with optional filtering parameters
* @returns Response containing list of SIP inbound trunks
*/
async listSipInboundTrunk(list: ListSipTrunkOptions = {}): Promise<Array<SIPInboundTrunkInfo>> {
const req = new ListSIPInboundTrunkRequest(list).toJson();
const data = await this.rpc.request(
svc,
'ListSIPInboundTrunk',
req,
await this.authHeader({}, { admin: true }),
);
return ListSIPInboundTrunkResponse.fromJson(data, { ignoreUnknownFields: true }).items ?? [];
}
/**
* List SIP outbound trunks with optional filtering.
*
* @param list - Request with optional filtering parameters
* @returns Response containing list of SIP outbound trunks
*/
async listSipOutboundTrunk(list: ListSipTrunkOptions = {}): Promise<Array<SIPOutboundTrunkInfo>> {
const req = new ListSIPOutboundTrunkRequest(list).toJson();
const data = await this.rpc.request(
svc,
'ListSIPOutboundTrunk',
req,
await this.authHeader({}, { admin: true }),
);
return ListSIPOutboundTrunkResponse.fromJson(data, { ignoreUnknownFields: true }).items ?? [];
}
/**
* Delete a SIP trunk.
*
* @param sipTrunkId - ID of the SIP trunk to delete
* @returns Deleted trunk information
*/
async deleteSipTrunk(sipTrunkId: string): Promise<SIPTrunkInfo> {
const data = await this.rpc.request(
svc,
'DeleteSIPTrunk',
new DeleteSIPTrunkRequest({ sipTrunkId }).toJson(),
await this.authHeader({}, { admin: true }),
);
return SIPTrunkInfo.fromJson(data, { ignoreUnknownFields: true });
}
/**
* Create a new SIP dispatch rule.
*
* @param rule - SIP dispatch rule to create
* @param opts - CreateSipDispatchRuleOptions
* @returns Created SIP dispatch rule
*/
async createSipDispatchRule(
rule: SipDispatchRuleDirect | SipDispatchRuleIndividual,
opts?: CreateSipDispatchRuleOptions,
): Promise<SIPDispatchRuleInfo> {
if (opts === undefined) {
opts = {};
}
let ruleProto: SIPDispatchRule | undefined = undefined;
if (rule.type == 'direct') {
ruleProto = new SIPDispatchRule({
rule: {
case: 'dispatchRuleDirect',
value: new SIPDispatchRuleDirect({
roomName: rule.roomName,
pin: rule.pin || '',
}),
},
});
} else if (rule.type == 'individual') {
ruleProto = new SIPDispatchRule({
rule: {
case: 'dispatchRuleIndividual',
value: new SIPDispatchRuleIndividual({
roomPrefix: rule.roomPrefix,
pin: rule.pin || '',
}),
},
});
}
const req = new CreateSIPDispatchRuleRequest({
rule: ruleProto,
trunkIds: opts.trunkIds,
hidePhoneNumber: opts.hidePhoneNumber,
name: opts.name,
metadata: opts.metadata,
attributes: opts.attributes,
roomPreset: opts.roomPreset,
roomConfig: opts.roomConfig,
}).toJson();
const data = await this.rpc.request(
svc,
'CreateSIPDispatchRule',
req,
await this.authHeader({}, { admin: true }),
);
return SIPDispatchRuleInfo.fromJson(data, { ignoreUnknownFields: true });
}
/**
* Updates an existing SIP dispatch rule by replacing it entirely.
*
* @param sipDispatchRuleId - ID of the SIP dispatch rule to update
* @param rule - new SIP dispatch rule
* @returns Updated SIP dispatch rule
*/
async updateSipDispatchRule(
sipDispatchRuleId: string,
rule: SIPDispatchRuleInfo,
): Promise<SIPDispatchRuleInfo> {
const req = new UpdateSIPDispatchRuleRequest({
sipDispatchRuleId: sipDispatchRuleId,
action: {
case: 'replace',
value: rule,
},
}).toJson();
const data = await this.rpc.request(
svc,
'UpdateSIPDispatchRule',
req,
await this.authHeader({}, { admin: true }),
);
return SIPDispatchRuleInfo.fromJson(data, { ignoreUnknownFields: true });
}
/**
* Updates specific fields of an existing SIP dispatch rule.
* Only provided fields will be updated.
*
* @param sipDispatchRuleId - ID of the SIP dispatch rule to update
* @param fields - Fields of the dispatch rule to update
* @returns Updated SIP dispatch rule
*/
async updateSipDispatchRuleFields(
sipDispatchRuleId: string,
fields: SipDispatchRuleUpdateOptions = {},
): Promise<SIPDispatchRuleInfo> {
const req = new UpdateSIPDispatchRuleRequest({
sipDispatchRuleId: sipDispatchRuleId,
action: {
case: 'update',
value: fields,
},
}).toJson();
const data = await this.rpc.request(
svc,
'UpdateSIPDispatchRule',
req,
await this.authHeader({}, { admin: true }),
);
return SIPDispatchRuleInfo.fromJson(data, { ignoreUnknownFields: true });
}
/**
* Updates an existing SIP inbound trunk by replacing it entirely.
*
* @param sipTrunkId - ID of the SIP inbound trunk to update
* @param trunk - SIP inbound trunk to update with
* @returns Updated SIP inbound trunk
*/
async updateSipInboundTrunk(
sipTrunkId: string,
trunk: SIPInboundTrunkInfo,
): Promise<SIPInboundTrunkInfo> {
const req = new UpdateSIPInboundTrunkRequest({
sipTrunkId,
action: {
case: 'replace',
value: trunk,
},
}).toJson();
const data = await this.rpc.request(
svc,
'UpdateSIPInboundTrunk',
req,
await this.authHeader({}, { admin: true }),
);
return SIPInboundTrunkInfo.fromJson(data, { ignoreUnknownFields: true });
}
/**
* Updates specific fields of an existing SIP inbound trunk.
* Only provided fields will be updated.
*
* @param sipTrunkId - ID of the SIP inbound trunk to update
* @param fields - Fields of the inbound trunk to update
* @returns Updated SIP inbound trunk
*/
async updateSipInboundTrunkFields(
sipTrunkId: string,
fields: SipInboundTrunkUpdateOptions,
): Promise<SIPInboundTrunkInfo> {
const req = new UpdateSIPInboundTrunkRequest({
sipTrunkId,
action: {
case: 'update',
value: fields,
},
}).toJson();
const data = await this.rpc.request(
svc,
'UpdateSIPInboundTrunk',
req,
await this.authHeader({}, { admin: true }),
);
return SIPInboundTrunkInfo.fromJson(data, { ignoreUnknownFields: true });
}
/**
* Updates an existing SIP outbound trunk by replacing it entirely.
*
* @param sipTrunkId - ID of the SIP outbound trunk to update
* @param trunk - SIP outbound trunk to update with
* @returns Updated SIP outbound trunk
*/
async updateSipOutboundTrunk(
sipTrunkId: string,
trunk: SIPOutboundTrunkInfo,
): Promise<SIPOutboundTrunkInfo> {
const req = new UpdateSIPOutboundTrunkRequest({
sipTrunkId,
action: {
case: 'replace',
value: trunk,
},
}).toJson();
const data = await this.rpc.request(
svc,
'UpdateSIPOutboundTrunk',
req,
await this.authHeader({}, { admin: true }),
);
return SIPOutboundTrunkInfo.fromJson(data, { ignoreUnknownFields: true });
}
/**
* Updates specific fields of an existing SIP outbound trunk.
* Only provided fields will be updated.
*
* @param sipTrunkId - ID of the SIP outbound trunk to update
* @param fields - Fields of the outbound trunk to update
* @returns Updated SIP outbound trunk
*/
async updateSipOutboundTrunkFields(
sipTrunkId: string,
fields: SipOutboundTrunkUpdateOptions,
): Promise<SIPOutboundTrunkInfo> {
const req = new UpdateSIPOutboundTrunkRequest({
sipTrunkId,
action: {
case: 'update',
value: fields,
},
}).toJson();
const data = await this.rpc.request(
svc,
'UpdateSIPOutboundTrunk',
req,
await this.authHeader({}, { admin: true }),
);
return SIPOutboundTrunkInfo.fromJson(data, { ignoreUnknownFields: true });
}
/**
* List SIP dispatch rules with optional filtering.
*
* @param list - Request with optional filtering parameters
* @returns Response containing list of SIP dispatch rules
*/
async listSipDispatchRule(
list: ListSipDispatchRuleOptions = {},
): Promise<Array<SIPDispatchRuleInfo>> {
const req = new ListSIPDispatchRuleRequest(list).toJson();
const data = await this.rpc.request(
svc,
'ListSIPDispatchRule',
req,
await this.authHeader({}, { admin: true }),
);
return ListSIPDispatchRuleResponse.fromJson(data, { ignoreUnknownFields: true }).items ?? [];
}
/**
* Delete a SIP dispatch rule.
*
* @param sipDispatchRuleId - ID of the SIP dispatch rule to delete
* @returns Deleted rule information
*/
async deleteSipDispatchRule(sipDispatchRuleId: string): Promise<SIPDispatchRuleInfo> {
const data = await this.rpc.request(
svc,
'DeleteSIPDispatchRule',
new DeleteSIPDispatchRuleRequest({ sipDispatchRuleId }).toJson(),
await this.authHeader({}, { admin: true }),
);
return SIPDispatchRuleInfo.fromJson(data, { ignoreUnknownFields: true });
}
/**
* Create a new SIP participant.
*
* @param sipTrunkId - sip trunk to use for the call
* @param number - number to dial
* @param roomName - room to attach the call to
* @param opts - CreateSipParticipantOptions
* @param outboundTrunkConfig - Optional outbound trunk configuration for sip participant.
* @returns Created SIP participant
*/
async createSipParticipant(
sipTrunkId: string,
number: string,
roomName: string,
opts?: CreateSipParticipantOptions,
outboundTrunkConfig?: SIPOutboundConfig,
): Promise<SIPParticipantInfo> {
if (opts === undefined) {
opts = {};
}
if (opts.timeout === undefined) {
opts.timeout = opts.waitUntilAnswered ? 60 : 10;
}
const req = new CreateSIPParticipantRequest({
sipTrunkId: sipTrunkId,
trunk: outboundTrunkConfig,
sipCallTo: number,
sipNumber: opts.fromNumber,
roomName: roomName,
participantIdentity: opts.participantIdentity || 'sip-participant',
participantName: opts.participantName,
displayName: opts.displayName,
participantMetadata: opts.participantMetadata,
participantAttributes: opts.participantAttributes,
dtmf: opts.dtmf,
playDialtone: opts.playDialtone ?? opts.playRingtone,
headers: opts.headers,
hidePhoneNumber: opts.hidePhoneNumber,
includeHeaders: opts.includeHeaders,
ringingTimeout: opts.ringingTimeout
? new Duration({ seconds: BigInt(opts.ringingTimeout) })
: undefined,
maxCallDuration: opts.maxCallDuration
? new Duration({ seconds: BigInt(opts.maxCallDuration) })
: undefined,
krispEnabled: opts.krispEnabled,
waitUntilAnswered: opts.waitUntilAnswered,
}).toJson();
const data = await this.rpc.request(
svc,
'CreateSIPParticipant',
req,
await this.authHeader({}, { call: true }),
opts.timeout,
);
return SIPParticipantInfo.fromJson(data, { ignoreUnknownFields: true });
}
/**
* Transfer a SIP participant to a different room.
*
* @param roomName - room the SIP participant to transfer is connectd to
* @param participantIdentity - identity of the SIP participant to transfer
* @param transferTo - SIP URL to transfer the participant to
* @param opts - TransferSipParticipantOptions
*/
async transferSipParticipant(
roomName: string,
participantIdentity: string,
transferTo: string,
opts?: TransferSipParticipantOptions,
): Promise<void> {
if (opts === undefined) {
opts = {};
}
const req = new TransferSIPParticipantRequest({
participantIdentity: participantIdentity,
roomName: roomName,
transferTo: transferTo,
playDialtone: opts.playDialtone,
headers: opts.headers,
ringingTimeout: opts.ringingTimeout
? new Duration({ seconds: BigInt(opts.ringingTimeout) })
: undefined,
}).toJson();
await this.rpc.request(
svc,
'TransferSIPParticipant',
req,
await this.authHeader({ roomAdmin: true, room: roomName }, { call: true }),
);
}
}