@fonoster/voice 0.5.5 → 0.6.1

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.
Files changed (109) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +3 -0
  3. package/dist/VoiceResponse.d.ts +218 -0
  4. package/dist/VoiceResponse.js +352 -0
  5. package/dist/VoiceServer.d.ts +6 -0
  6. package/dist/VoiceServer.js +109 -0
  7. package/dist/createSession.d.ts +4 -0
  8. package/dist/createSession.js +44 -0
  9. package/dist/defaultServerConfig.d.ts +3 -0
  10. package/dist/defaultServerConfig.js +10 -0
  11. package/dist/demo.d.ts +1 -0
  12. package/dist/demo.js +56 -0
  13. package/dist/index.d.ts +6 -6
  14. package/dist/index.js +42 -7
  15. package/dist/serviceDefinition.d.ts +2 -0
  16. package/dist/serviceDefinition.js +29 -0
  17. package/dist/types.d.ts +8 -20
  18. package/dist/verbs/Answer.d.ts +6 -0
  19. package/dist/{mute/mute.js → verbs/Answer.js} +8 -7
  20. package/dist/verbs/Dial.d.ts +15 -0
  21. package/dist/verbs/Dial.js +52 -0
  22. package/dist/verbs/Gather.d.ts +7 -0
  23. package/dist/verbs/Gather.js +39 -0
  24. package/dist/verbs/Hangup.d.ts +6 -0
  25. package/dist/{unmute/unmute.js → verbs/Hangup.js} +8 -7
  26. package/dist/verbs/Mute.d.ts +7 -0
  27. package/dist/verbs/Mute.js +36 -0
  28. package/dist/verbs/Play.d.ts +7 -0
  29. package/dist/verbs/Play.js +14 -0
  30. package/dist/verbs/PlayDtmf.d.ts +7 -0
  31. package/dist/verbs/PlayDtmf.js +13 -0
  32. package/dist/verbs/PlaybackControl.d.ts +7 -0
  33. package/dist/{dial/status_stream.js → verbs/PlaybackControl.js} +11 -17
  34. package/dist/verbs/Record.d.ts +7 -0
  35. package/dist/verbs/Record.js +20 -0
  36. package/dist/verbs/Say.d.ts +7 -0
  37. package/dist/verbs/Say.js +14 -0
  38. package/dist/verbs/Stream.d.ts +21 -0
  39. package/dist/verbs/Stream.js +76 -0
  40. package/dist/verbs/Unmute.d.ts +4 -0
  41. package/dist/verbs/Unmute.js +26 -0
  42. package/dist/verbs/Verb.d.ts +10 -0
  43. package/dist/verbs/Verb.js +70 -0
  44. package/dist/verbs/index.d.ts +14 -0
  45. package/dist/verbs/index.js +48 -0
  46. package/dist/verbs/validateRequest.d.ts +2 -0
  47. package/dist/verbs/validateRequest.js +11 -0
  48. package/package.json +22 -21
  49. package/dist/answer/answer.d.ts +0 -4
  50. package/dist/answer/answer.js +0 -55
  51. package/dist/asserts.d.ts +0 -4
  52. package/dist/asserts.js +0 -47
  53. package/dist/dial/dial.d.ts +0 -6
  54. package/dist/dial/dial.js +0 -77
  55. package/dist/dial/status_stream.d.ts +0 -9
  56. package/dist/dial/types.d.ts +0 -6
  57. package/dist/dial/types.js +0 -2
  58. package/dist/dtmf/asserts.d.ts +0 -2
  59. package/dist/dtmf/asserts.js +0 -9
  60. package/dist/dtmf/dtmf.d.ts +0 -5
  61. package/dist/dtmf/dtmf.js +0 -61
  62. package/dist/dtmf/types.d.ts +0 -3
  63. package/dist/dtmf/types.js +0 -2
  64. package/dist/gather/asserts.d.ts +0 -2
  65. package/dist/gather/asserts.js +0 -9
  66. package/dist/gather/gather.d.ts +0 -10
  67. package/dist/gather/gather.js +0 -50
  68. package/dist/gather/source_dtmf.d.ts +0 -3
  69. package/dist/gather/source_dtmf.js +0 -46
  70. package/dist/gather/source_speech.d.ts +0 -5
  71. package/dist/gather/source_speech.js +0 -65
  72. package/dist/gather/types.d.ts +0 -6
  73. package/dist/gather/types.js +0 -2
  74. package/dist/hangup/hangup.d.ts +0 -4
  75. package/dist/hangup/hangup.js +0 -57
  76. package/dist/mute/mute.d.ts +0 -6
  77. package/dist/mute/types.d.ts +0 -3
  78. package/dist/mute/types.js +0 -2
  79. package/dist/play/play.d.ts +0 -6
  80. package/dist/play/play.js +0 -57
  81. package/dist/play/types.d.ts +0 -5
  82. package/dist/play/types.js +0 -2
  83. package/dist/playback/playback.d.ts +0 -12
  84. package/dist/playback/playback.js +0 -67
  85. package/dist/record/record.d.ts +0 -6
  86. package/dist/record/record.js +0 -68
  87. package/dist/record/types.d.ts +0 -13
  88. package/dist/record/types.js +0 -2
  89. package/dist/say/types.d.ts +0 -5
  90. package/dist/say/types.js +0 -2
  91. package/dist/server.d.ts +0 -15
  92. package/dist/server.js +0 -90
  93. package/dist/sgather/gather.d.ts +0 -10
  94. package/dist/sgather/gather.js +0 -42
  95. package/dist/sgather/source_speech.d.ts +0 -7
  96. package/dist/sgather/source_speech.js +0 -42
  97. package/dist/sgather/stream_data.d.ts +0 -14
  98. package/dist/sgather/stream_data.js +0 -69
  99. package/dist/sgather/types.d.ts +0 -7
  100. package/dist/sgather/types.js +0 -2
  101. package/dist/unmute/types.d.ts +0 -3
  102. package/dist/unmute/types.js +0 -2
  103. package/dist/unmute/unmute.d.ts +0 -6
  104. package/dist/utils.d.ts +0 -12
  105. package/dist/utils.js +0 -57
  106. package/dist/verb.d.ts +0 -9
  107. package/dist/verb.js +0 -50
  108. package/dist/voice.d.ts +0 -284
  109. package/dist/voice.js +0 -367
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 Fonoster Inc
3
+ Copyright (c) 2024 Fonoster Inc
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ <a href="https://gitpod.io/#https://github.com/fonoster/fonoster"> <img src="https://img.shields.io/badge/Contribute%20with-Gitpod-908a85?logo=gitpod" alt="Contribute with Gitpod" />
2
+
3
+ This module is part of the [Fonoster](https://fonoster.com) project. By itself, it does not do much. It is intended to be used as a dependency for other modules. For more information about the project, please visit [https://github.com/fonoster/fonoster](https://github.com/fonoster/fonoster).
@@ -0,0 +1,218 @@
1
+ import { DialOptions, GatherOptions, GatherResponse, MuteOptions, PlayOptions, PlayResponse, PlaybackControlAction, RecordOptions, RecordResponse, SayOptions, SayResponse, StreamOptions, VerbResponse, VoiceRequest, VoiceSessionStreamServer } from "@fonoster/common";
2
+ import { DialStatusStream, Stream } from "./verbs";
3
+ /**
4
+ * @classdesc Use the VoiceResponse object, to construct advance Interactive
5
+ * Voice Response (IVR) applications.
6
+ *
7
+ * @extends Verb
8
+ * @example
9
+ *
10
+ * import { VoiceServer } from "@fonoster/voice";
11
+ *
12
+ * async function handler (request, response) {
13
+ * await response.answer();
14
+ * await response.play("sound:hello-world");
15
+ * }
16
+ *
17
+ * const voiceServer = new VoiceServer({base: '/voiceapp'})
18
+ * voiceServer.listen(handler, { port: 3000 })
19
+ */
20
+ declare class VoiceResponse {
21
+ voice: VoiceSessionStreamServer;
22
+ request: VoiceRequest;
23
+ /**
24
+ * Constructs a new VoiceResponse object.
25
+ *
26
+ * @param {VoiceRequest} request - Options to indicate the objects endpoint
27
+ * @param {VoiceSessionStream} voice - The voice session stream
28
+ * @see module:core:APIClient
29
+ */
30
+ constructor(request: VoiceRequest, voice: VoiceSessionStreamServer);
31
+ /**
32
+ * Answer the call. Before running any other verb you
33
+ * must run the anwer command.
34
+ * @example
35
+ *
36
+ * async function handler (request, response) {
37
+ * await response.answer();
38
+ * }
39
+ */
40
+ answer(): Promise<VerbResponse>;
41
+ /**
42
+ * Hangup the call.
43
+ * @example
44
+ *
45
+ * async function handler (request, response) {
46
+ * await response.hangup();
47
+ * }
48
+ */
49
+ hangup(): Promise<VerbResponse>;
50
+ /**
51
+ * Play an audio in the call.
52
+ *
53
+ * @param {string} url - The URL of the media to play
54
+ * @param {PlayOptions} options - Options to control the playback
55
+ * @param {string} options.playbackRef - Playback identifier to use in Playback operations
56
+ * @see Playback
57
+ * @example
58
+ *
59
+ * async function handler (request, response) {
60
+ * await response.answer();
61
+ * await response.play("https://soundsserver:9000/sounds/hello-world.wav");
62
+ * }
63
+ */
64
+ play(url: string, options?: PlayOptions): Promise<PlayResponse>;
65
+ /**
66
+ * Play a series of DTMF digits in a call.
67
+ *
68
+ * @param {string} digits -The DTMF digits to play (0-9, #, or *)
69
+ * @example
70
+ *
71
+ * async function handler (request, response) {
72
+ * await response.answer();
73
+ * await response.playDtmf("1234");
74
+ * }
75
+ */
76
+ playDtmf(digits: string): Promise<void>;
77
+ /**
78
+ * Control the playback of the currently playing media.
79
+ *
80
+ * @param {string} playbackRef - The playback identifier
81
+ * @param {PlaybackControlAction} action - The action to perform (STOP, RESTART, PAUSE, UNPAUSE, FORWARD)
82
+ * @see play
83
+ * @example
84
+ *
85
+ * async function handler (request, response) {
86
+ * await response.answer();
87
+ * await response.play("https://s3.fonoster.io/uuid/hello-world.wav", { playbackRef: "playback-01" });
88
+ *
89
+ * // Pause the media
90
+ * await response.playbackControl("playback-01", PlaybackControlAction.PAUSE);
91
+ * }
92
+ */
93
+ playbackControl(playbackRef: string, action: PlaybackControlAction): Promise<VerbResponse>;
94
+ /**
95
+ * Waits for data entry from the user's keypad or from a speech provider.
96
+ *
97
+ * @param {GatherOptions} options - Options to select the maximum number of digits, final character, and timeout
98
+ * @param {number} options.maxDigits - Maximum number of digits to collect. Defaults to 1
99
+ * @param {number} options.timeout - Milliseconds to wait before timeout. Defaults to 4000. Use zero for no timeout.
100
+ * @param {string} options.finishOnKey - Optional last character to wait for. Defaults to '#'. It will not be included in the returned digits
101
+ * @param {GatherSource} options.source - Where to listen as input source. This option accepts `DTMF` and `SPEECH`. A speech provider must be configure
102
+ * when including the `SPEECH` source. You might inclue both with `SPEECH_AND_DTMF`. Defaults to `SPEECH_AND_DTMF`
103
+ * @note When including `SPEECH` the default timeout is 10000 (10s).
104
+ * @example
105
+ *
106
+ * async function handler (request, response) {
107
+ * await response.answer();
108
+ * const speech = await response.gather({ source: GatherSource.SPEECH, numDigits: 3 });
109
+ * console.log("speech: " + speech);
110
+ * await response.hangup();
111
+ * }
112
+ */
113
+ gather(options?: GatherOptions): Promise<GatherResponse>;
114
+ /**
115
+ * Send a text for a TTS engine to convert to speech.
116
+ *
117
+ * @param {string} text - The text to convert to speech
118
+ * @param {SayOptions} options - Options to control the TTS engine
119
+ * @param {string} options.playbackRef - Playback identifier to use in Playback operations
120
+ * @param {TTSOptions} options.ttsOptions - Options to control the TTS engine (specific to the TTS engine)
121
+ * @see Say
122
+ * @example
123
+ *
124
+ * async function handler (request, response) {
125
+ * await response.answer();
126
+ * const playbackRef = await response.say("Hello World");
127
+ *
128
+ * // Like the play verb, you can control the playback
129
+ * await response.playbackControl(playbackRef, PlaybackControlAction.STOP);
130
+ * }
131
+ */
132
+ say(text: string, options?: SayOptions): Promise<SayResponse>;
133
+ /**
134
+ * Record the audio of the call.
135
+ *
136
+ * @param {RecordOptions} options - Options to control the record operation
137
+ * @param {number} options.maxDuration - The maximum duration of the recording in seconds. Default is 60
138
+ * @param {number} options.maxSilence - The maximum duration of silence in seconds. Default is 5
139
+ * @param {boolean} options.beep - Play a beep before recording. Default is true
140
+ * @param {string} options.finishOnKey - Stop recording when a DTMF digit is received. Default is '#'
141
+ * @return {RecordResponse} The record response
142
+ * @example
143
+ *
144
+ * async function handler (request, response) {
145
+ * await response.answer();
146
+ * const record = await response.record();
147
+ * console.log("Recording: %s", record.name);
148
+ * }
149
+ */
150
+ record(options?: RecordOptions): Promise<RecordResponse>;
151
+ /**
152
+ * Dials a destination and returns a stream of status.
153
+ *
154
+ * @param {string} destination - The destination to dial
155
+ * @param {DialOptions} options - Options to control the dial operation
156
+ * @param {number} options.timeout - The timeout in seconds. Default is 60
157
+ * @param {RecordDirection} options.recordDirection - The direction to record the call (IN, OUT, BOTH). Default is BOTH
158
+ * @return {Promise<DialStatusStream>} The dial status stream
159
+ */
160
+ dial(destination: string, options?: DialOptions): Promise<DialStatusStream>;
161
+ /**
162
+ * Starts a bidirectional audio stream between the call and the application.
163
+ *
164
+ * @param {StreamOptions} options - Options to control the stream operation
165
+ * @param {StreamDirection} options.direction - The direction to stream the audio (IN, OUT, BOTH). Default is BOTH
166
+ * @param {StreamAudioFormat} options.format - The audio format to stream (WAV). Default is WAV
167
+ * @param {boolean} options.enableVad - Enable voice activity detection. Default is false
168
+ * @return {Promise<Stream>} The stream object
169
+ * @example
170
+ *
171
+ * async function handler (request, response) {
172
+ * await response.answer();
173
+ *
174
+ * const stream = await response.stream({
175
+ * direction: StreamDirection.BOTH,
176
+ * format: StreamAudioFormat.WAV,
177
+ * enableVad: true
178
+ * });
179
+ *
180
+ * stream.onPayload((payload) => {
181
+ * // Use the payload
182
+ * });
183
+ *
184
+ * // Or write to the stream
185
+ * // stream.write({ type: StreamMessageType.AUDIO_OUT, payload: "\x00\x01\x02" });
186
+ * }
187
+ */
188
+ stream(options: StreamOptions): Promise<Stream>;
189
+ /**
190
+ * Mutes a call.
191
+ *
192
+ * @param {MuteOptions} options - Options to control the mute operation
193
+ * @param {MuteDirection} options.direction - The direction to mute the channel (IN, OUT, BOTH). Default is BOTH
194
+ * @see unmute
195
+ * @example
196
+ *
197
+ * async function handler (request, response) {
198
+ * await response.answer();
199
+ * await response.mute(); // Will mute both directions
200
+ * }
201
+ */
202
+ mute(options?: MuteOptions): Promise<void>;
203
+ /**
204
+ * Unmutes a call.
205
+ *
206
+ * @param {MuteOptions} options - Options to control the unmute operation
207
+ * @param {MuteDirection} options.direction - The direction to unmute the call (IN, OUT, BOTH). Default is BOTH
208
+ * @see mute
209
+ * @example
210
+ *
211
+ * async function handler (request, response) {
212
+ * await response.answer();
213
+ * await response.unmute(); // Will unmute both directions
214
+ * }
215
+ */
216
+ unmute(options?: MuteOptions): Promise<void>;
217
+ }
218
+ export { VoiceResponse };
@@ -0,0 +1,352 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.VoiceResponse = void 0;
13
+ /*
14
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
15
+ * http://github.com/fonoster/fonoster
16
+ *
17
+ * This file is part of Fonoster
18
+ *
19
+ * Licensed under the MIT License (the "License");
20
+ * you may not use this file except in compliance with
21
+ * the License. You may obtain a copy of the License at
22
+ *
23
+ * https://opensource.org/licenses/MIT
24
+ *
25
+ * Unless required by applicable law or agreed to in writing, software
26
+ * distributed under the License is distributed on an "AS IS" BASIS,
27
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28
+ * See the License for the specific language governing permissions and
29
+ * limitations under the License.
30
+ */
31
+ const common_1 = require("@fonoster/common");
32
+ const pb_util_1 = require("pb-util");
33
+ const verbs_1 = require("./verbs");
34
+ /**
35
+ * @classdesc Use the VoiceResponse object, to construct advance Interactive
36
+ * Voice Response (IVR) applications.
37
+ *
38
+ * @extends Verb
39
+ * @example
40
+ *
41
+ * import { VoiceServer } from "@fonoster/voice";
42
+ *
43
+ * async function handler (request, response) {
44
+ * await response.answer();
45
+ * await response.play("sound:hello-world");
46
+ * }
47
+ *
48
+ * const voiceServer = new VoiceServer({base: '/voiceapp'})
49
+ * voiceServer.listen(handler, { port: 3000 })
50
+ */
51
+ class VoiceResponse {
52
+ /**
53
+ * Constructs a new VoiceResponse object.
54
+ *
55
+ * @param {VoiceRequest} request - Options to indicate the objects endpoint
56
+ * @param {VoiceSessionStream} voice - The voice session stream
57
+ * @see module:core:APIClient
58
+ */
59
+ constructor(request, voice) {
60
+ this.voice = voice;
61
+ this.request = request;
62
+ }
63
+ /**
64
+ * Answer the call. Before running any other verb you
65
+ * must run the anwer command.
66
+ * @example
67
+ *
68
+ * async function handler (request, response) {
69
+ * await response.answer();
70
+ * }
71
+ */
72
+ answer() {
73
+ return __awaiter(this, void 0, void 0, function* () {
74
+ yield new verbs_1.Answer(this.request, this.voice).run();
75
+ return { sessionRef: this.request.sessionRef };
76
+ });
77
+ }
78
+ /**
79
+ * Hangup the call.
80
+ * @example
81
+ *
82
+ * async function handler (request, response) {
83
+ * await response.hangup();
84
+ * }
85
+ */
86
+ hangup() {
87
+ return __awaiter(this, void 0, void 0, function* () {
88
+ yield new verbs_1.Hangup(this.request, this.voice).run();
89
+ return { sessionRef: this.request.sessionRef };
90
+ });
91
+ }
92
+ /**
93
+ * Play an audio in the call.
94
+ *
95
+ * @param {string} url - The URL of the media to play
96
+ * @param {PlayOptions} options - Options to control the playback
97
+ * @param {string} options.playbackRef - Playback identifier to use in Playback operations
98
+ * @see Playback
99
+ * @example
100
+ *
101
+ * async function handler (request, response) {
102
+ * await response.answer();
103
+ * await response.play("https://soundsserver:9000/sounds/hello-world.wav");
104
+ * }
105
+ */
106
+ play(url, options) {
107
+ return __awaiter(this, void 0, void 0, function* () {
108
+ const response = yield new verbs_1.Play(this.request, this.voice).run(Object.assign(Object.assign({}, options), { sessionRef: this.request.sessionRef, url }));
109
+ return response.playResponse;
110
+ });
111
+ }
112
+ /**
113
+ * Play a series of DTMF digits in a call.
114
+ *
115
+ * @param {string} digits -The DTMF digits to play (0-9, #, or *)
116
+ * @example
117
+ *
118
+ * async function handler (request, response) {
119
+ * await response.answer();
120
+ * await response.playDtmf("1234");
121
+ * }
122
+ */
123
+ playDtmf(digits) {
124
+ return __awaiter(this, void 0, void 0, function* () {
125
+ yield new verbs_1.PlayDtmf(this.request, this.voice).run({
126
+ sessionRef: this.request.sessionRef,
127
+ digits
128
+ });
129
+ });
130
+ }
131
+ /**
132
+ * Control the playback of the currently playing media.
133
+ *
134
+ * @param {string} playbackRef - The playback identifier
135
+ * @param {PlaybackControlAction} action - The action to perform (STOP, RESTART, PAUSE, UNPAUSE, FORWARD)
136
+ * @see play
137
+ * @example
138
+ *
139
+ * async function handler (request, response) {
140
+ * await response.answer();
141
+ * await response.play("https://s3.fonoster.io/uuid/hello-world.wav", { playbackRef: "playback-01" });
142
+ *
143
+ * // Pause the media
144
+ * await response.playbackControl("playback-01", PlaybackControlAction.PAUSE);
145
+ * }
146
+ */
147
+ playbackControl(playbackRef, action) {
148
+ return __awaiter(this, void 0, void 0, function* () {
149
+ yield new verbs_1.PlaybackControl(this.request, this.voice).run({
150
+ sessionRef: this.request.sessionRef,
151
+ playbackRef,
152
+ action
153
+ });
154
+ return { sessionRef: this.request.sessionRef };
155
+ });
156
+ }
157
+ /**
158
+ * Waits for data entry from the user's keypad or from a speech provider.
159
+ *
160
+ * @param {GatherOptions} options - Options to select the maximum number of digits, final character, and timeout
161
+ * @param {number} options.maxDigits - Maximum number of digits to collect. Defaults to 1
162
+ * @param {number} options.timeout - Milliseconds to wait before timeout. Defaults to 4000. Use zero for no timeout.
163
+ * @param {string} options.finishOnKey - Optional last character to wait for. Defaults to '#'. It will not be included in the returned digits
164
+ * @param {GatherSource} options.source - Where to listen as input source. This option accepts `DTMF` and `SPEECH`. A speech provider must be configure
165
+ * when including the `SPEECH` source. You might inclue both with `SPEECH_AND_DTMF`. Defaults to `SPEECH_AND_DTMF`
166
+ * @note When including `SPEECH` the default timeout is 10000 (10s).
167
+ * @example
168
+ *
169
+ * async function handler (request, response) {
170
+ * await response.answer();
171
+ * const speech = await response.gather({ source: GatherSource.SPEECH, numDigits: 3 });
172
+ * console.log("speech: " + speech);
173
+ * await response.hangup();
174
+ * }
175
+ */
176
+ gather() {
177
+ return __awaiter(this, arguments, void 0, function* (options = { source: common_1.GatherSource.SPEECH_AND_DTMF }) {
178
+ const response = yield new verbs_1.Gather(this.request, this.voice).run(Object.assign({ sessionRef: this.request.sessionRef }, options));
179
+ return response.gatherResponse;
180
+ });
181
+ }
182
+ /**
183
+ * Send a text for a TTS engine to convert to speech.
184
+ *
185
+ * @param {string} text - The text to convert to speech
186
+ * @param {SayOptions} options - Options to control the TTS engine
187
+ * @param {string} options.playbackRef - Playback identifier to use in Playback operations
188
+ * @param {TTSOptions} options.ttsOptions - Options to control the TTS engine (specific to the TTS engine)
189
+ * @see Say
190
+ * @example
191
+ *
192
+ * async function handler (request, response) {
193
+ * await response.answer();
194
+ * const playbackRef = await response.say("Hello World");
195
+ *
196
+ * // Like the play verb, you can control the playback
197
+ * await response.playbackControl(playbackRef, PlaybackControlAction.STOP);
198
+ * }
199
+ */
200
+ say(text, options) {
201
+ return __awaiter(this, void 0, void 0, function* () {
202
+ const response = yield new verbs_1.Say(this.request, this.voice).run({
203
+ playbackRef: options === null || options === void 0 ? void 0 : options.playbackRef,
204
+ options: options ? pb_util_1.struct.encode(options) : undefined,
205
+ sessionRef: this.request.sessionRef,
206
+ text
207
+ });
208
+ return { playbackRef: response.sayResponse.playbackRef };
209
+ });
210
+ }
211
+ /**
212
+ * Record the audio of the call.
213
+ *
214
+ * @param {RecordOptions} options - Options to control the record operation
215
+ * @param {number} options.maxDuration - The maximum duration of the recording in seconds. Default is 60
216
+ * @param {number} options.maxSilence - The maximum duration of silence in seconds. Default is 5
217
+ * @param {boolean} options.beep - Play a beep before recording. Default is true
218
+ * @param {string} options.finishOnKey - Stop recording when a DTMF digit is received. Default is '#'
219
+ * @return {RecordResponse} The record response
220
+ * @example
221
+ *
222
+ * async function handler (request, response) {
223
+ * await response.answer();
224
+ * const record = await response.record();
225
+ * console.log("Recording: %s", record.name);
226
+ * }
227
+ */
228
+ record(options) {
229
+ return __awaiter(this, void 0, void 0, function* () {
230
+ const response = yield new verbs_1.Record(this.request, this.voice).run(Object.assign({ sessionRef: this.request.sessionRef }, options));
231
+ return response.recordResponse;
232
+ });
233
+ }
234
+ /**
235
+ * Dials a destination and returns a stream of status.
236
+ *
237
+ * @param {string} destination - The destination to dial
238
+ * @param {DialOptions} options - Options to control the dial operation
239
+ * @param {number} options.timeout - The timeout in seconds. Default is 60
240
+ * @param {RecordDirection} options.recordDirection - The direction to record the call (IN, OUT, BOTH). Default is BOTH
241
+ * @return {Promise<DialStatusStream>} The dial status stream
242
+ */
243
+ dial(destination, options) {
244
+ return __awaiter(this, void 0, void 0, function* () {
245
+ const stream = new verbs_1.DialStatusStream();
246
+ yield new verbs_1.Dial(this.request, this.voice).run(Object.assign({ sessionRef: this.request.sessionRef, destination }, options));
247
+ this.voice.on(common_1.StreamEvent.DATA, (result) => {
248
+ if (result.dialResponse) {
249
+ stream.emit(result.dialResponse.status);
250
+ }
251
+ });
252
+ return stream;
253
+ });
254
+ }
255
+ /**
256
+ * Starts a bidirectional audio stream between the call and the application.
257
+ *
258
+ * @param {StreamOptions} options - Options to control the stream operation
259
+ * @param {StreamDirection} options.direction - The direction to stream the audio (IN, OUT, BOTH). Default is BOTH
260
+ * @param {StreamAudioFormat} options.format - The audio format to stream (WAV). Default is WAV
261
+ * @param {boolean} options.enableVad - Enable voice activity detection. Default is false
262
+ * @return {Promise<Stream>} The stream object
263
+ * @example
264
+ *
265
+ * async function handler (request, response) {
266
+ * await response.answer();
267
+ *
268
+ * const stream = await response.stream({
269
+ * direction: StreamDirection.BOTH,
270
+ * format: StreamAudioFormat.WAV,
271
+ * enableVad: true
272
+ * });
273
+ *
274
+ * stream.onPayload((payload) => {
275
+ * // Use the payload
276
+ * });
277
+ *
278
+ * // Or write to the stream
279
+ * // stream.write({ type: StreamMessageType.AUDIO_OUT, payload: "\x00\x01\x02" });
280
+ * }
281
+ */
282
+ stream(options) {
283
+ return __awaiter(this, void 0, void 0, function* () {
284
+ const stream = new verbs_1.Stream();
285
+ const startStream = new verbs_1.StartStream(this.request, this.voice);
286
+ const stopStream = new verbs_1.StopStream(this.request, this.voice);
287
+ const { startStreamResponse } = yield startStream.run(Object.assign({ sessionRef: this.request.sessionRef }, options));
288
+ this.voice.on(common_1.StreamEvent.DATA, (result) => {
289
+ if (result.streamPayload) {
290
+ stream.emit("payloadOut", result.streamPayload);
291
+ }
292
+ });
293
+ stream.onPayloadIn((payload) => {
294
+ this.voice.write({
295
+ streamPayload: payload
296
+ });
297
+ });
298
+ stream.cleanup(() => {
299
+ stopStream.run({
300
+ sessionRef: this.request.sessionRef,
301
+ streamRef: startStreamResponse === null || startStreamResponse === void 0 ? void 0 : startStreamResponse.streamRef
302
+ });
303
+ });
304
+ return stream;
305
+ });
306
+ }
307
+ /**
308
+ * Mutes a call.
309
+ *
310
+ * @param {MuteOptions} options - Options to control the mute operation
311
+ * @param {MuteDirection} options.direction - The direction to mute the channel (IN, OUT, BOTH). Default is BOTH
312
+ * @see unmute
313
+ * @example
314
+ *
315
+ * async function handler (request, response) {
316
+ * await response.answer();
317
+ * await response.mute(); // Will mute both directions
318
+ * }
319
+ */
320
+ mute() {
321
+ return __awaiter(this, arguments, void 0, function* (options = { direction: common_1.MuteDirection.BOTH }) {
322
+ const { direction } = options;
323
+ yield new verbs_1.Mute(this.request, this.voice).run({
324
+ sessionRef: this.request.sessionRef,
325
+ direction
326
+ });
327
+ });
328
+ }
329
+ /**
330
+ * Unmutes a call.
331
+ *
332
+ * @param {MuteOptions} options - Options to control the unmute operation
333
+ * @param {MuteDirection} options.direction - The direction to unmute the call (IN, OUT, BOTH). Default is BOTH
334
+ * @see mute
335
+ * @example
336
+ *
337
+ * async function handler (request, response) {
338
+ * await response.answer();
339
+ * await response.unmute(); // Will unmute both directions
340
+ * }
341
+ */
342
+ unmute() {
343
+ return __awaiter(this, arguments, void 0, function* (options = { direction: common_1.MuteDirection.BOTH }) {
344
+ const { direction } = options;
345
+ yield new verbs_1.Unmute(this.request, this.voice).run({
346
+ sessionRef: this.request.sessionRef,
347
+ direction
348
+ });
349
+ });
350
+ }
351
+ }
352
+ exports.VoiceResponse = VoiceResponse;
@@ -0,0 +1,6 @@
1
+ import { ServerConfig, VoiceHandler } from "./types";
2
+ export default class VoiceServer {
3
+ config: ServerConfig;
4
+ constructor(config?: ServerConfig);
5
+ listen(handler: VoiceHandler): Promise<void>;
6
+ }