typescript-agi-ipcom 0.0.8-beta.4 → 0.0.8-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Channel.js DELETED
@@ -1,1043 +0,0 @@
1
- "use strict";
2
- // Copyright (c) 2020, Brandon Lehmann
3
- //
4
- // Please see the included LICENSE file for more information.
5
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
- return new (P || (P = Promise))(function (resolve, reject) {
8
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
- step((generator = generator.apply(thisArg, _arguments || [])).next());
12
- });
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.Channel = exports.PlaybackStatus = exports.DialStatus = exports.ChannelState = void 0;
16
- const events_1 = require("events");
17
- const util_1 = require("util");
18
- const ResponseArguments_1 = require("./ResponseArguments");
19
- /** @ignore */
20
- var ContextState;
21
- (function (ContextState) {
22
- ContextState[ContextState["INIT"] = 0] = "INIT";
23
- ContextState[ContextState["WAITING"] = 2] = "WAITING";
24
- })(ContextState || (ContextState = {}));
25
- /**
26
- * Represents the current channel state
27
- */
28
- var ChannelState;
29
- (function (ChannelState) {
30
- ChannelState[ChannelState["DOWN_AVAILABLE"] = 0] = "DOWN_AVAILABLE";
31
- ChannelState[ChannelState["DOWN_RESERVED"] = 1] = "DOWN_RESERVED";
32
- ChannelState[ChannelState["OFF_HOOK"] = 2] = "OFF_HOOK";
33
- ChannelState[ChannelState["DIGITS_DIALED"] = 3] = "DIGITS_DIALED";
34
- ChannelState[ChannelState["RINGING"] = 4] = "RINGING";
35
- ChannelState[ChannelState["REMOTE_RINGING"] = 5] = "REMOTE_RINGING";
36
- ChannelState[ChannelState["UP"] = 6] = "UP";
37
- ChannelState[ChannelState["BUSY"] = 7] = "BUSY";
38
- })(ChannelState = exports.ChannelState || (exports.ChannelState = {}));
39
- /**
40
- * Represents the result of a Dial() attempt
41
- */
42
- var DialStatus;
43
- (function (DialStatus) {
44
- DialStatus[DialStatus["ANSWER"] = 0] = "ANSWER";
45
- DialStatus[DialStatus["BUSY"] = 1] = "BUSY";
46
- DialStatus[DialStatus["NOANSWER"] = 2] = "NOANSWER";
47
- DialStatus[DialStatus["CANCEL"] = 3] = "CANCEL";
48
- DialStatus[DialStatus["CONGESTION"] = 4] = "CONGESTION";
49
- DialStatus[DialStatus["CHANUNAVAIL"] = 5] = "CHANUNAVAIL";
50
- DialStatus[DialStatus["DONTCALL"] = 6] = "DONTCALL";
51
- DialStatus[DialStatus["TORTURE"] = 7] = "TORTURE";
52
- DialStatus[DialStatus["INVALIDARGS"] = 8] = "INVALIDARGS";
53
- })(DialStatus = exports.DialStatus || (exports.DialStatus = {}));
54
- /**
55
- * Represents the playback status
56
- */
57
- var PlaybackStatus;
58
- (function (PlaybackStatus) {
59
- PlaybackStatus[PlaybackStatus["SUCCESS"] = 0] = "SUCCESS";
60
- PlaybackStatus[PlaybackStatus["USER_STOPPED"] = 1] = "USER_STOPPED";
61
- PlaybackStatus[PlaybackStatus["REMOTE_STOPPED"] = 2] = "REMOTE_STOPPED";
62
- PlaybackStatus[PlaybackStatus["ERROR"] = 3] = "ERROR";
63
- })(PlaybackStatus = exports.PlaybackStatus || (exports.PlaybackStatus = {}));
64
- /**
65
- * Represents an AGI Channel
66
- */
67
- class Channel extends events_1.EventEmitter {
68
- /**
69
- * Creates a new instance of a channel object
70
- * @param connection the AGI socket connection
71
- */
72
- constructor(connection) {
73
- super();
74
- this.m_message = '';
75
- this.m_network = '';
76
- this.m_network_script = '';
77
- this.m_request = '';
78
- this.m_channel = '';
79
- this.m_language = '';
80
- this.m_type = '';
81
- this.m_uniqueid = '';
82
- this.m_version = '';
83
- this.m_callerid = '';
84
- this.m_calleridname = '';
85
- this.m_callingpres = '';
86
- this.m_callingani2 = '';
87
- this.m_callington = '';
88
- this.m_callingtns = '';
89
- this.m_dnid = '';
90
- this.m_rdnis = '';
91
- this.m_context = '';
92
- this.m_extension = '';
93
- this.m_priority = '';
94
- this.m_enhanced = '';
95
- this.m_accountcode = '';
96
- this.m_threadid = '';
97
- this.setMaxListeners(10);
98
- this.m_connection = connection;
99
- this.m_state = ContextState.INIT;
100
- this.m_connection.on('data', (data) => this.read(data));
101
- this.m_connection.on('close', () => this.emit('close'));
102
- this.m_connection.on('error', (error) => this.emit('error', error));
103
- this.m_connection.on('timeout', () => this.emit('timeout'));
104
- this.on('hangup', () => this.close());
105
- }
106
- on(event, listener) {
107
- return super.on(event, listener);
108
- }
109
- /**
110
- * Whether this AGI request is over the network
111
- */
112
- get network() {
113
- return (this.m_network.toLowerCase() === 'yes');
114
- }
115
- /**
116
- * The network path included in the AGI request
117
- * ie. agi://127.0.0.1:3000/test
118
- * This value would return 'test'
119
- */
120
- get network_script() {
121
- return this.m_network_script;
122
- }
123
- /**
124
- * The version of Asterisk
125
- */
126
- get version() {
127
- return this.m_version;
128
- }
129
- /**
130
- * The filename of your script
131
- * ie. agi
132
- */
133
- get request() {
134
- return this.m_request;
135
- }
136
- /**
137
- * The originating channel (your phone)
138
- */
139
- get channel() {
140
- return this.m_channel;
141
- }
142
- /**
143
- * The language code (e.g. “en”)
144
- */
145
- get language() {
146
- return this.m_language;
147
- }
148
- /**
149
- * The originating channel type (e.g. “SIP” or “ZAP”)
150
- */
151
- get type() {
152
- return this.m_type;
153
- }
154
- /**
155
- * A unique ID for the call
156
- */
157
- get uniqueid() {
158
- return this.m_uniqueid;
159
- }
160
- /**
161
- * The caller ID number (or “unknown”)
162
- */
163
- get callerid() {
164
- return this.m_callerid;
165
- }
166
- /**
167
- * The caller ID name (or “unknown”)
168
- */
169
- get calleridname() {
170
- return this.m_calleridname;
171
- }
172
- /**
173
- * The presentation for the callerid in a ZAP channel
174
- */
175
- get callingpres() {
176
- return this.m_callingpres;
177
- }
178
- /**
179
- * The number which is defined in ANI2 see Asterisk Detailed Variable List (only for PRI Channels)
180
- */
181
- get callingani2() {
182
- return this.m_callingani2;
183
- }
184
- /**
185
- * The type of number used in PRI Channels see Asterisk Detailed Variable List
186
- */
187
- get callington() {
188
- return this.m_callington;
189
- }
190
- /**
191
- * An optional 4 digit number (Transit Network Selector) used in PRI Channels see Asterisk Detailed Variable List
192
- */
193
- get callingtns() {
194
- return this.m_callingtns;
195
- }
196
- /**
197
- * The dialed number id (or “unknown”)
198
- */
199
- get dnid() {
200
- return this.m_dnid;
201
- }
202
- /**
203
- * The referring DNIS number (or “unknown”)
204
- */
205
- get rdnis() {
206
- return this.m_rdnis;
207
- }
208
- /**
209
- * Origin context in extensions.conf
210
- */
211
- get context() {
212
- return this.m_context;
213
- }
214
- /**
215
- * The called number
216
- */
217
- get extension() {
218
- return this.m_extension;
219
- }
220
- /**
221
- * The priority it was executed as in the dial plan
222
- */
223
- get priority() {
224
- return this.m_priority;
225
- }
226
- /**
227
- * The flag value is 1.0 if started as an EAGI script, 0.0 otherwise
228
- */
229
- get enhanced() {
230
- return this.m_enhanced;
231
- }
232
- /**
233
- * Account code of the origin channel
234
- */
235
- get accountcode() {
236
- return this.m_accountcode;
237
- }
238
- /**
239
- * Thread ID of the AGI script
240
- */
241
- get threadid() {
242
- return this.m_threadid;
243
- }
244
- /**
245
- * Answers channel if not already in answer state.
246
- */
247
- answer() {
248
- return __awaiter(this, void 0, void 0, function* () {
249
- const response = yield this.sendCommand('ANSWER');
250
- if (response.code !== 200 || response.result !== 0) {
251
- throw new Error('Could not answer call');
252
- }
253
- });
254
- }
255
- /**
256
- * Interrupts expected flow of Async AGI commands and returns control to
257
- * previous source (typically, the PBX dialplan).
258
- */
259
- break() {
260
- return __awaiter(this, void 0, void 0, function* () {
261
- const response = yield this.sendCommand('ASYNCAGI BREAK');
262
- if (response.code !== 200 || response.result !== 0) {
263
- throw new Error('Could not interrupt processing');
264
- }
265
- return this.close();
266
- });
267
- }
268
- /**
269
- * Returns status of the connected channel.
270
- * @param channel
271
- */
272
- channelStatus(channel) {
273
- return __awaiter(this, void 0, void 0, function* () {
274
- const response = yield this.sendCommand(util_1.format('CHANNEL STATUS %s', channel || ''));
275
- if (response.code !== 200 || response.result === -1) {
276
- throw new Error('Could not get channel status');
277
- }
278
- return response.result;
279
- });
280
- }
281
- /**
282
- * Sends audio file on channel and allows the listener to control the stream.
283
- * @param filename
284
- * @param escapeDigits
285
- * @param skipms
286
- * @param fastForwardCharacter
287
- * @param rewindCharacter
288
- * @param pauseCharacter
289
- */
290
- controlStreamFile(filename, escapeDigits = '', skipms, fastForwardCharacter, rewindCharacter, pauseCharacter) {
291
- return __awaiter(this, void 0, void 0, function* () {
292
- const response = yield this.sendCommand(util_1.format('CONTROL STREAM FILE %s "%s" %s %s %s %s', filename, escapeDigits, skipms || '', fastForwardCharacter || '', rewindCharacter || '', pauseCharacter || ''));
293
- if (response.code !== 200 || response.result === -1) {
294
- throw new Error('Could not control stream file');
295
- }
296
- const playbackStatus = yield this.getVariable('CPLAYBACKSTATUS ');
297
- const playbackOffset = yield this.getVariable('CPLAYBACKOFFSET ');
298
- let status = PlaybackStatus.ERROR;
299
- switch (playbackStatus.toUpperCase()) {
300
- case 'SUCCESS':
301
- status = PlaybackStatus.SUCCESS;
302
- break;
303
- case 'USERSTOPPED':
304
- status = PlaybackStatus.USER_STOPPED;
305
- break;
306
- case 'REMOTESTOPPED':
307
- status = PlaybackStatus.REMOTE_STOPPED;
308
- break;
309
- }
310
- return {
311
- digit: response.arguments.char('result'),
312
- playbackStatus: status,
313
- playbackOffset: parseInt(playbackOffset, 10),
314
- };
315
- });
316
- }
317
- /**
318
- * Attempts to establish a new outgoing connection on a channel, and then link it to the calling input channel.
319
- * @param target
320
- * @param timeout
321
- * @param params
322
- */
323
- dial(target, timeout = 30, params) {
324
- return __awaiter(this, void 0, void 0, function* () {
325
- yield this.exec('Dial', util_1.format('%s,%s,%s', target, timeout, params || ''));
326
- const dialstatus = yield this.getVariable('DIALSTATUS');
327
- switch (dialstatus.toUpperCase()) {
328
- case 'ANSWER':
329
- return DialStatus.ANSWER;
330
- case 'BUSY':
331
- return DialStatus.BUSY;
332
- case 'NOANSWER':
333
- return DialStatus.NOANSWER;
334
- case 'CANCEL':
335
- return DialStatus.CANCEL;
336
- case 'CONGESTION':
337
- return DialStatus.CONGESTION;
338
- case 'CHANUNAVAIL':
339
- return DialStatus.CHANUNAVAIL;
340
- case 'DONTCALL':
341
- return DialStatus.DONTCALL;
342
- case 'TORTURE':
343
- return DialStatus.TORTURE;
344
- case 'INVALIDARGS':
345
- return DialStatus.INVALIDARGS;
346
- default:
347
- throw new Error('Unknown dial status');
348
- }
349
- });
350
- }
351
- /**
352
- * Deletes an entry in the Asterisk database for a given family and key.
353
- * @param family
354
- * @param key
355
- */
356
- databaseDel(family, key) {
357
- return __awaiter(this, void 0, void 0, function* () {
358
- const response = yield this.sendCommand(util_1.format('DATABASE DEL %s %s', family, key));
359
- if (response.code !== 200 || response.result === 0) {
360
- throw new Error('Could not delete from the database');
361
- }
362
- });
363
- }
364
- /**
365
- * Deletes a family or specific keytree within a family in the Asterisk database.
366
- * @param family
367
- * @param keyTree
368
- */
369
- databaseDelTree(family, keyTree) {
370
- return __awaiter(this, void 0, void 0, function* () {
371
- const response = yield this.sendCommand(util_1.format('DATABASE DELTREE %s %s', family, keyTree || ''));
372
- if (response.code !== 200) {
373
- throw new Error('Could not delete tree from database');
374
- }
375
- return (response.result === 0);
376
- });
377
- }
378
- /**
379
- * Retrieves an entry in the Asterisk database for a given family and key.
380
- * @param family
381
- * @param key
382
- */
383
- databaseGet(family, key) {
384
- return __awaiter(this, void 0, void 0, function* () {
385
- const response = yield this.sendCommand(util_1.format('DATABASE GET %s %s', family, key));
386
- if (response.code !== 200 || response.result === 0) {
387
- throw new Error('Database key not set');
388
- }
389
- return response.arguments.nokey();
390
- });
391
- }
392
- /**
393
- * Adds or updates an entry in the Asterisk database for a given family, key, and value.
394
- * @param family
395
- * @param key
396
- * @param value
397
- */
398
- databasePut(family, key, value) {
399
- return __awaiter(this, void 0, void 0, function* () {
400
- const response = yield this.sendCommand(util_1.format('DATABASE PUT %s %s %s', family, key, value));
401
- if (response.code !== 200 || response.result === 0) {
402
- throw new Error('Database key not set');
403
- }
404
- return response.arguments.string('value');
405
- });
406
- }
407
- /**
408
- * Executes application with given options
409
- * @param application.
410
- * @param args
411
- */
412
- exec(application, ...args) {
413
- return __awaiter(this, void 0, void 0, function* () {
414
- const response = yield this.sendCommand(util_1.format('EXEC %s %s', application, args.join(' ')));
415
- if (response.code !== 200 || response.result === -2) {
416
- throw new Error('Could not execute application');
417
- }
418
- return response.result;
419
- });
420
- }
421
- /**
422
- * Stream the given file, and receive DTMF data.
423
- * @param soundFile
424
- * @param timeout
425
- * @param maxDigits
426
- */
427
- getData(soundFile, timeout = 5, maxDigits) {
428
- return __awaiter(this, void 0, void 0, function* () {
429
- const response = yield this.sendCommand(util_1.format('GET DATA %s %s %s', soundFile, timeout * 1000, maxDigits || ''));
430
- if (response.code !== 200 || response.result === -1) {
431
- throw new Error('Could not get data from channel');
432
- }
433
- return {
434
- digits: response.arguments.string('result'),
435
- timeout: (response.arguments.string('value') === '(timeout)'),
436
- };
437
- });
438
- }
439
- /**
440
- * Evaluates a channel expression
441
- * Understands complex variable names and builtin variables, unlike GET VARIABLE.
442
- * @param key
443
- * @param channel
444
- */
445
- getFullVariable(key, channel) {
446
- return __awaiter(this, void 0, void 0, function* () {
447
- const response = yield this.sendCommand(util_1.format('GET FULL VARIABLE %s %s', key.toUpperCase(), channel || ''));
448
- if (response.code !== 200 || response.result === 0) {
449
- throw new Error('Variable not set');
450
- }
451
- return response.arguments.nokey();
452
- });
453
- }
454
- /**
455
- * Stream file, prompt for DTMF, with timeout.
456
- * Behaves similar to STREAM FILE but used with a timeout option.
457
- * @param soundFile
458
- * @param escapeDigits
459
- * @param timeout
460
- */
461
- getOption(soundFile, escapeDigits = '#', timeout = 5) {
462
- return __awaiter(this, void 0, void 0, function* () {
463
- const response = yield this.sendCommand(util_1.format('GET OPTION %s "%s" %s', soundFile, escapeDigits, timeout * 1000));
464
- if (response.code !== 200 || response.result === -1) {
465
- throw new Error('Could not get option');
466
- }
467
- if (response.arguments.number('endpos') === 0) {
468
- throw new Error('Could Not play file');
469
- }
470
- return {
471
- digit: response.arguments.char('result'),
472
- endpos: response.arguments.number('endpos'),
473
- };
474
- });
475
- }
476
- /**
477
- * Gets a channel variable.
478
- * @param key
479
- */
480
- getVariable(key) {
481
- return __awaiter(this, void 0, void 0, function* () {
482
- const response = yield this.sendCommand(util_1.format('GET VARIABLE %s', key.toUpperCase()));
483
- if (response.code !== 200 || response.result === 0) {
484
- throw new Error('Variable not set');
485
- }
486
- return response.arguments.nokey();
487
- });
488
- }
489
- /**
490
- * Cause the channel to execute the specified dialplan subroutine.
491
- * @param context
492
- * @param extension
493
- * @param priority
494
- * @param argument
495
- */
496
- goSub(context, extension, priority, argument) {
497
- return __awaiter(this, void 0, void 0, function* () {
498
- const response = yield this.sendCommand(util_1.format('GOSUB %s %s %s %s', context, extension, priority, argument || ''));
499
- if (response.code !== 200 || response.result !== 0) {
500
- throw new Error('Could not execute gosub');
501
- }
502
- });
503
- }
504
- /**
505
- * Hangs up the specified channel. If no channel name is given, hangs up the current channel
506
- * @param channel
507
- */
508
- hangup(channel) {
509
- return __awaiter(this, void 0, void 0, function* () {
510
- const response = yield this.sendCommand(util_1.format('HANGUP %s', channel || ''));
511
- if (response.code !== 200 || response.result !== 1) {
512
- throw new Error('Could not hang up call');
513
- }
514
- });
515
- }
516
- /**
517
- * Does nothing
518
- */
519
- noop() {
520
- return __awaiter(this, void 0, void 0, function* () {
521
- const response = yield this.sendCommand('NOOP');
522
- if (response.code !== 200 || response.result !== 0) {
523
- throw new Error('Could not NOOP');
524
- }
525
- });
526
- }
527
- /**
528
- * Receives one character from channels supporting it.
529
- * @param timeout
530
- */
531
- receiveChar(timeout = 5) {
532
- return __awaiter(this, void 0, void 0, function* () {
533
- const response = yield this.sendCommand(util_1.format('RECEIVE CHAR %s', timeout * 1000));
534
- if (response.code !== 200 || response.result === -1) {
535
- throw new Error('Could not get data from channel');
536
- }
537
- return {
538
- char: response.arguments.char('result'),
539
- timeout: response.arguments.boolean('timeout'),
540
- };
541
- });
542
- }
543
- /**
544
- * Receives text from channels supporting it.
545
- * @param timeout
546
- */
547
- receiveText(timeout = 5) {
548
- return __awaiter(this, void 0, void 0, function* () {
549
- const response = yield this.sendCommand(util_1.format('RECEIVE TEXT %s', timeout * 1000));
550
- if (response.code !== 200 || response.result === -1) {
551
- throw new Error('Could not get data from channel');
552
- }
553
- return response.arguments.string('result');
554
- });
555
- }
556
- /**
557
- * Records to a given file.
558
- * @param filename
559
- * @param fileFormat
560
- * @param escapeDigits
561
- * @param timeout
562
- * @param beep
563
- * @param silence
564
- * @param offsetSamples
565
- */
566
- recordFile(filename, fileFormat = 'gsm', escapeDigits = '#', timeout = 10, beep, silence, offsetSamples) {
567
- return __awaiter(this, void 0, void 0, function* () {
568
- const response = yield this.sendCommand(util_1.format('RECORD FILE %s %s "%s" %s %s %s %s', filename, fileFormat, escapeDigits, timeout * 1000, offsetSamples || '', (beep) ? 'BEEP' : '', (silence) ? util_1.format('s=%s', silence) : ''));
569
- if (response.code !== 200 || response.result === -1) {
570
- throw new Error('Could not record file');
571
- }
572
- return {
573
- digit: response.arguments.char('result'),
574
- endpos: response.arguments.number('endpos'),
575
- timeout: response.arguments.boolean('timeout'),
576
- };
577
- });
578
- }
579
- /**
580
- * Says a given character string.
581
- * @param value
582
- * @param escapeDigits
583
- */
584
- sayAlpha(value, escapeDigits = '#') {
585
- return __awaiter(this, void 0, void 0, function* () {
586
- const response = yield this.sendCommand(util_1.format('SAY ALPHA %s "%s"', value, escapeDigits));
587
- if (response.code !== 200 || response.result === -1) {
588
- throw new Error('Could not say alpha');
589
- }
590
- return response.arguments.char('result');
591
- });
592
- }
593
- /**
594
- * Says a given date.
595
- * @param value
596
- * @param escapeDigits
597
- */
598
- sayDate(value, escapeDigits = '#') {
599
- return __awaiter(this, void 0, void 0, function* () {
600
- const response = yield this.sendCommand(util_1.format('SAY DATE %s "%s"', (typeof value === 'number') ? value : Math.floor(value.getTime() / 1000), escapeDigits));
601
- if (response.code !== 200 || response.result === -1) {
602
- throw new Error('Could not say date');
603
- }
604
- return response.arguments.char('result');
605
- });
606
- }
607
- /**
608
- * Says a given time as specified by the format given.
609
- * @param value
610
- * @param escapeDigits
611
- * @param dateFormat
612
- * @param timezone
613
- */
614
- sayDateTime(value, escapeDigits = '#', dateFormat, timezone) {
615
- return __awaiter(this, void 0, void 0, function* () {
616
- const response = yield this.sendCommand(util_1.format('SAY DATETIME %s "%s" %s %s', (typeof value === 'number') ? value : Math.floor(value.getTime() / 1000), escapeDigits, dateFormat || '', timezone || ''));
617
- if (response.code !== 200 || response.result === -1) {
618
- throw new Error('Could not say date time');
619
- }
620
- return response.arguments.char('result');
621
- });
622
- }
623
- /**
624
- * Says a given digit string.
625
- * @param value
626
- * @param escapeDigits
627
- */
628
- sayDigits(value, escapeDigits = '#') {
629
- return __awaiter(this, void 0, void 0, function* () {
630
- const response = yield this.sendCommand(util_1.format('SAY DIGITS %s "%s"', value, escapeDigits));
631
- if (response.code !== 200 || response.result === -1) {
632
- throw new Error('Could not say digits');
633
- }
634
- return response.arguments.char('result');
635
- });
636
- }
637
- /**
638
- * Says a given number.
639
- * @param value
640
- * @param escapeDigits
641
- */
642
- sayNumber(value, escapeDigits = '#') {
643
- return __awaiter(this, void 0, void 0, function* () {
644
- const response = yield this.sendCommand(util_1.format('SAY NUMBER %s "%s"', value, escapeDigits));
645
- if (response.code !== 200 || response.result === -1) {
646
- throw new Error('Could not say number');
647
- }
648
- return response.arguments.char('result');
649
- });
650
- }
651
- /**
652
- * Says a given character string with phonetics.
653
- * @param value
654
- * @param escapeDigits
655
- */
656
- sayPhonetic(value, escapeDigits = '#') {
657
- return __awaiter(this, void 0, void 0, function* () {
658
- const response = yield this.sendCommand(util_1.format('SAY PHONETIC %s "%s"', value, escapeDigits));
659
- if (response.code !== 200 || response.result === -1) {
660
- throw new Error('Could not say phonetic');
661
- }
662
- return response.arguments.char('result');
663
- });
664
- }
665
- /**
666
- * Says a given time.
667
- * @param value
668
- * @param escapeDigits
669
- */
670
- sayTime(value, escapeDigits = '#') {
671
- return __awaiter(this, void 0, void 0, function* () {
672
- const response = yield this.sendCommand(util_1.format('SAY TIME %s "%s"', (typeof value === 'number') ? value : Math.floor(value.getTime() / 1000), escapeDigits));
673
- if (response.code !== 200 || response.result === -1) {
674
- throw new Error('Could not say time');
675
- }
676
- return response.arguments.char('result');
677
- });
678
- }
679
- /**
680
- * Sends images to channels supporting it.
681
- * @param image
682
- */
683
- sendImage(image) {
684
- return __awaiter(this, void 0, void 0, function* () {
685
- const response = yield this.sendCommand(util_1.format('SEND IMAGE %s', image));
686
- if (response.code !== 200 || response.result !== 0) {
687
- throw new Error('Could not send image');
688
- }
689
- });
690
- }
691
- /**
692
- * Sends text to channels supporting it.
693
- * @param text
694
- */
695
- sendText(text) {
696
- return __awaiter(this, void 0, void 0, function* () {
697
- const response = yield this.sendCommand(util_1.format('SEND TEXT "%s"', text));
698
- if (response.code !== 200 || response.result !== 0) {
699
- throw new Error('Could not send text');
700
- }
701
- });
702
- }
703
- /**
704
- * Autohangup channel in some time.
705
- * @param timeout
706
- */
707
- setAutoHangup(timeout = 60) {
708
- return __awaiter(this, void 0, void 0, function* () {
709
- const response = yield this.sendCommand(util_1.format('SET AUTOHANGUP %s', timeout));
710
- if (response.code !== 200 || response.result !== 0) {
711
- throw new Error('Could not set auto hangup');
712
- }
713
- });
714
- }
715
- /**
716
- * Sets callerid for the current channel.
717
- * @param callerNumber
718
- * @param callerName
719
- */
720
- setCallerID(callerNumber, callerName) {
721
- return __awaiter(this, void 0, void 0, function* () {
722
- const callerid = (callerName) ?
723
- util_1.format('"%s"<%s>', callerName, callerNumber) :
724
- callerNumber;
725
- const response = yield this.sendCommand(util_1.format('SET CALLERID %s', callerid));
726
- if (response.code !== 200 || response.result !== 1) {
727
- throw new Error('Could not set caller id');
728
- }
729
- });
730
- }
731
- /**
732
- * Sets channel context.
733
- * @param context
734
- */
735
- setContext(context) {
736
- return __awaiter(this, void 0, void 0, function* () {
737
- const response = yield this.sendCommand(util_1.format('SET CONTEXT %s', context));
738
- if (response.code !== 200 || response.result !== 0) {
739
- throw new Error('Could not set context');
740
- }
741
- });
742
- }
743
- /**
744
- * Changes channel extension.
745
- * @param extension
746
- */
747
- setExtension(extension) {
748
- return __awaiter(this, void 0, void 0, function* () {
749
- const response = yield this.sendCommand(util_1.format('SET EXTENSION %s', extension));
750
- if (response.code !== 200 || response.result !== 0) {
751
- throw new Error('Could not set extension');
752
- }
753
- });
754
- }
755
- /**
756
- * Enable/Disable Music on hold generator
757
- * @param status
758
- * @param musicClass
759
- */
760
- setMusic(status = true, musicClass) {
761
- return __awaiter(this, void 0, void 0, function* () {
762
- const response = yield this.sendCommand(util_1.format('SET MUSIC %s %s', (status) ? 'ON' : 'OFF', musicClass || ''));
763
- if (response.code !== 200 || response.result !== 0) {
764
- throw new Error('Could not set priority');
765
- }
766
- });
767
- }
768
- /**
769
- * Set channel dialplan priority.
770
- * @param priority
771
- */
772
- setPriority(priority) {
773
- return __awaiter(this, void 0, void 0, function* () {
774
- const response = yield this.sendCommand(util_1.format('SET PRIORITY %s', priority));
775
- if (response.code !== 200 || response.result !== 0) {
776
- throw new Error('Could not set priority');
777
- }
778
- });
779
- }
780
- /**
781
- * Set channel dialplan priority.
782
- * @param key
783
- * @param value
784
- */
785
- setVariable(key, value) {
786
- return __awaiter(this, void 0, void 0, function* () {
787
- const response = yield this.sendCommand(util_1.format('SET VARIABLE %s "%s"', key.toUpperCase(), value));
788
- if (response.code !== 200 || response.result !== 1) {
789
- throw new Error('Could not set variable');
790
- }
791
- });
792
- }
793
- speechActivateGrammar(grammar) {
794
- return __awaiter(this, void 0, void 0, function* () {
795
- // TODO: Handle the response
796
- return this.sendCommand(util_1.format('SPEECH ACTIVATE GRAMMAR %s', grammar));
797
- });
798
- }
799
- speechCreate(engine) {
800
- return __awaiter(this, void 0, void 0, function* () {
801
- // TODO: Handle the response
802
- return this.sendCommand(util_1.format('SPEECH CREATE ENGINE %s', engine));
803
- });
804
- }
805
- speechDeactivateGrammar(grammar) {
806
- return __awaiter(this, void 0, void 0, function* () {
807
- // TODO: Handle the response
808
- return this.sendCommand(util_1.format('SPEECH DEACTIVATE GRAMMAR %s', grammar));
809
- });
810
- }
811
- speechDestroy() {
812
- return __awaiter(this, void 0, void 0, function* () {
813
- // TODO: Handle the response
814
- return this.sendCommand('SPEECH DESTROY');
815
- });
816
- }
817
- speechLoadGrammar(grammar, path) {
818
- return __awaiter(this, void 0, void 0, function* () {
819
- // TODO: Handle the response
820
- return this.sendCommand(util_1.format('SPEECH LOAD GRAMMAR %s %s', grammar, path));
821
- });
822
- }
823
- speechRecognize(soundFile, timeout = 5, offset) {
824
- return __awaiter(this, void 0, void 0, function* () {
825
- // TODO: Handle the response
826
- return this.sendCommand(util_1.format('SPEECH RECOGNIZE %s %s %s', soundFile, timeout * 1000, offset));
827
- });
828
- }
829
- speechSet(key, value) {
830
- return __awaiter(this, void 0, void 0, function* () {
831
- // TODO: Handle the response
832
- return this.sendCommand(util_1.format('SPEECH SET %s %s', key, value));
833
- });
834
- }
835
- speedUnloadGrammar(grammar) {
836
- return __awaiter(this, void 0, void 0, function* () {
837
- // TODO: Handle the response
838
- return this.sendCommand(util_1.format('SPEECH UNLOAD GRAMMAR %s', grammar));
839
- });
840
- }
841
- /**
842
- * Sends audio file on channel.
843
- * @param filename
844
- * @param escapeDigits
845
- * @param offset
846
- */
847
- streamFile(filename, escapeDigits = '#', offset) {
848
- return __awaiter(this, void 0, void 0, function* () {
849
- const response = yield this.sendCommand(util_1.format('STREAM FILE %s "%s" %s', filename, escapeDigits, offset || ''));
850
- if (response.code !== 200 || response.result === -1) {
851
- throw new Error('Could not stream file');
852
- }
853
- const status = yield this.getVariable('PLAYBACKSTATUS ');
854
- if (status.toUpperCase() !== 'SUCCESS') {
855
- throw new Error('Could not stream file');
856
- }
857
- return {
858
- digit: response.arguments.char('result'),
859
- endpos: response.arguments.number('endpos'),
860
- };
861
- });
862
- }
863
- /**
864
- * Toggles TDD mode (for the deaf).
865
- * @param status
866
- */
867
- tddMode(status) {
868
- return __awaiter(this, void 0, void 0, function* () {
869
- const response = yield this.sendCommand(util_1.format('TDD MODE %s', (status) ? 'ON' : 'OFF'));
870
- if (response.code !== 200 || response.result !== 1) {
871
- throw new Error('Could not set TDD mode');
872
- }
873
- });
874
- }
875
- /**
876
- * Logs a message to the asterisk verbose log.
877
- * @param message
878
- * @param level
879
- */
880
- verbose(message, level) {
881
- return __awaiter(this, void 0, void 0, function* () {
882
- const response = yield this.sendCommand(util_1.format('VERBOSE "%s" %s', message, level || ''));
883
- if (response.code !== 200 || response.result !== 1) {
884
- throw new Error('Could not send logging message');
885
- }
886
- });
887
- }
888
- /**
889
- * Waits for a digit to be pressed.
890
- * @param timeout
891
- */
892
- waitForDigit(timeout = 5) {
893
- return __awaiter(this, void 0, void 0, function* () {
894
- const response = yield this.sendCommand(util_1.format('WAIT FOR DIGIT %s', timeout * 1000));
895
- if (response.code !== 200 || response.result === -1) {
896
- throw new Error('Could not wait for digit');
897
- }
898
- return response.arguments.char('result');
899
- });
900
- }
901
- /* Internal Methods */
902
- close() {
903
- this.m_connection.destroy();
904
- }
905
- read(data) {
906
- if (data.length === 0) {
907
- return;
908
- }
909
- this.m_message += data.toString();
910
- if (this.m_state === ContextState.INIT) {
911
- if (this.m_message.indexOf('\n\n') === -1) {
912
- return;
913
- }
914
- this.readVariables(this.m_message);
915
- }
916
- else if (this.m_state === ContextState.WAITING) {
917
- if (this.m_message.indexOf('\n') === -1) {
918
- return;
919
- }
920
- this.readResponse(this.m_message);
921
- }
922
- this.m_message = '';
923
- }
924
- readVariables(message) {
925
- const lines = message.split('\n');
926
- lines.map((line) => {
927
- const split = line.split(':');
928
- const name = (split[0] || '').trim();
929
- const value = (split[1] || '').trim();
930
- const id = name.substring(4);
931
- switch (id) {
932
- case 'network':
933
- return this.m_network = value;
934
- case 'network_script':
935
- return this.m_network_script = value;
936
- case 'request':
937
- return this.m_request = value;
938
- case 'channel':
939
- return this.m_channel = value;
940
- case 'language':
941
- return this.m_language = value;
942
- case 'type':
943
- return this.m_type = value;
944
- case 'uniqueid':
945
- return this.m_uniqueid = value;
946
- case 'version':
947
- return this.m_version = value;
948
- case 'callerid':
949
- return this.m_callerid = value;
950
- case 'calleridname':
951
- return this.m_calleridname = value;
952
- case 'callingpres':
953
- return this.m_callingpres = value;
954
- case 'callingani2':
955
- return this.m_callingani2 = value;
956
- case 'callington':
957
- return this.m_callington = value;
958
- case 'callingtns':
959
- return this.m_callingtns = value;
960
- case 'dnid':
961
- return this.m_dnid = value;
962
- case 'rdnis':
963
- return this.m_rdnis = value;
964
- case 'context':
965
- return this.m_context = value;
966
- case 'extension':
967
- return this.m_extension = value;
968
- case 'priority':
969
- return this.m_priority = value;
970
- case 'enhanced':
971
- return this.m_enhanced = value;
972
- case 'accountcode':
973
- return this.m_accountcode = value;
974
- case 'threadid':
975
- return this.m_threadid = value;
976
- }
977
- });
978
- this.m_state = ContextState.WAITING;
979
- this.emit('ready');
980
- }
981
- readResponse(message) {
982
- const lines = message.split('\n');
983
- lines.map((line) => this.readResponseLine(line));
984
- }
985
- readResponseLine(line) {
986
- if (!line) {
987
- return;
988
- }
989
- this.emit('recv', line);
990
- const parsed = line.split(' ');
991
- if (!parsed || parsed[0] === 'HANGUP') {
992
- return this.emit('hangup');
993
- }
994
- const code = parseInt(parsed[0], 10);
995
- parsed.shift();
996
- const args = new ResponseArguments_1.ResponseArguments();
997
- for (const value of parsed) {
998
- if (value.indexOf('=') !== -1) {
999
- const parts = value.split('=', 2);
1000
- const key = parts[0].trim();
1001
- const val = parts[1].trim();
1002
- args.addArgument(key, val);
1003
- }
1004
- else if (value.indexOf('(') !== -1) {
1005
- const name = value.substring(1, value.length - 1);
1006
- args.addArgument(name, true);
1007
- }
1008
- else {
1009
- args.addArgument('value', value);
1010
- }
1011
- }
1012
- const response = {
1013
- code: code,
1014
- result: args.number('result'),
1015
- arguments: args,
1016
- };
1017
- this.emit('response', response);
1018
- }
1019
- send(message) {
1020
- return __awaiter(this, void 0, void 0, function* () {
1021
- return new Promise((resolve, reject) => {
1022
- this.emit('send', message);
1023
- this.m_connection.write(message, (error) => {
1024
- if (error) {
1025
- return reject(error);
1026
- }
1027
- return resolve();
1028
- });
1029
- });
1030
- });
1031
- }
1032
- sendCommand(command) {
1033
- return __awaiter(this, void 0, void 0, function* () {
1034
- return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
1035
- this.once('response', (response) => {
1036
- return resolve(response);
1037
- });
1038
- yield this.send(util_1.format('%s\n', command.trim()));
1039
- }));
1040
- });
1041
- }
1042
- }
1043
- exports.Channel = Channel;