-
Notifications
You must be signed in to change notification settings - Fork 180
/
Input.ts
106 lines (92 loc) · 2.31 KB
/
Input.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
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
import { NCCOActions } from '../../enums';
import { DTMFSettings } from '../../types';
import { InputAction } from '../../types';
import { SpeechSettings } from '../../types';
import { Serializable } from '../../ncco';
/**
* Represents an Input action in the Nexmo Call Control Object (NCCO) for gathering user input.
*/
export class Input implements InputAction, Serializable {
/**
* The action type, which is always 'input'.
*/
action: NCCOActions.INPUT = NCCOActions.INPUT;
/**
* An array of input types ('dtmf' and/or 'speech').
*/
type: string[] = [];
/**
* DTMF input settings.
*/
dtmf?: DTMFSettings;
/**
* Speech input settings.
*/
speech?: SpeechSettings;
/**
* An array of URLs to send events to asynchronously.
*/
eventUrl?: string[] = [];
/**
* The HTTP method used to send events (e.g., 'POST' or 'GET').
*/
eventMethod?: string;
/**
* Create a new Input instance.
*
* @param {DTMFSettings} dtmf - DTMF input settings.
* @param {SpeechSettings} speech - Speech input settings.
* @param {string} eventUrl - URL to send events to asynchronously.
* @param {string} eventMethod - The HTTP method used to send events.
*/
constructor(
dtmf?: DTMFSettings,
speech?: SpeechSettings,
eventUrl?: string,
eventMethod?: string,
) {
if (dtmf) {
this.type.push('dtmf');
this.dtmf = dtmf;
}
if (speech) {
this.type.push('speech');
this.speech = speech;
}
if (eventUrl) {
this.eventUrl = [ eventUrl ];
}
if (eventMethod) {
this.eventMethod = eventMethod;
}
if (this.type.length === 0) {
throw new TypeError(
'Input action must have at least either DTMF or Speech settings',
);
}
}
/**
* Serialize the Input action to a Nexmo Call Control Object (NCCO) format.
*
* @return {InputAction} - The serialized Input action.
*/
serializeToNCCO(): InputAction {
const data: InputAction = {
action: NCCOActions.INPUT,
type: this.type,
};
if (this.dtmf) {
data.dtmf = this.dtmf;
}
if (this.speech) {
data.speech = this.speech;
}
if (this.eventUrl) {
data.eventUrl = this.eventUrl;
}
if (this.eventMethod) {
data.eventMethod = this.eventMethod;
}
return data;
}
}