generated from GenesysCloudBlueprints/blueprint-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dtmf-service.ts
58 lines (51 loc) · 1.88 KB
/
dtmf-service.ts
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
import EventEmitter from 'events';
/*
* This class provides DTMF support for the incoming DTMF digits from the Client.
* The following events are expected from the session:
*
* Name; error
* Parameters: Error message string or error object.
*
* Name: final-digits
* Parameters: A string representing all of the captured DTMF digits.
*
* The current usage of this class requires that a new instance be created for
* each set of DTMF digits received. Once a terminating digit is received, a
* new instance must be created.
*/
export class DTMFService {
private emitter = new EventEmitter();
private state = 'None';
private digits = '';
on(event: string, listener: (...args: any[]) => void): DTMFService {
this.emitter?.addListener(event, listener);
return this;
}
getState(): string {
return this.state;
}
/*
* For this implementation, we are just going to continue to capture digits until we
* received a terminating digit (specifically, #). A more robust implementation will
* need to be able to handle the case where a terminating digit is not received - for
* that case, an inter-digit timeout should be implemented.
*/
processDigit(digit: string): DTMFService {
if (this.state === 'Complete') {
this.emitter.emit('error', 'DTMF digits already received.');
return this;
}
this.state = 'Processing';
// If we get a terminating digit, mark this instance as complete, send out the event,
// and reset the digits to help prevent possible leaking of digits if this instance is
// attempted to be reused.
if (digit === '#') {
this.state = 'Complete';
this.emitter.emit('final-digits', this.digits);
this.digits = '';
return this;
}
this.digits += digit;
return this;
}
}