@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/dist/voice.d.ts DELETED
@@ -1,284 +0,0 @@
1
- import { PlaybackControl } from "./playback/playback";
2
- import { SayOptions } from "./say/types";
3
- import { VoiceRequest } from "./types";
4
- import { Plugin } from "@fonoster/common";
5
- import { DialOptions } from "./dial/types";
6
- import { SGatherStream } from "./sgather/types";
7
- import { DtmfOptions } from "./dtmf/types";
8
- import { SGatherOptions } from "./sgather/gather";
9
- import StreamStatus from "./dial/status_stream";
10
- import { GatherOptions } from "./gather/gather";
11
- import { MuteOptions } from "./mute/mute";
12
- import { PlayOptions } from "./play/play";
13
- import { RecordOptions, RecordResult } from "./record/record";
14
- /**
15
- * @classdesc Use the VoiceResponse object, to construct advance Interactive
16
- * Voice Response (IVR) applications.
17
- *
18
- * @extends Verb
19
- * @example
20
- *
21
- * import { VoiceServer } from "@fonoster/voice";
22
- *
23
- * async function handler (request, response) {
24
- * await response.answer();
25
- * await response.play("sound:hello-world");
26
- * }
27
- *
28
- * const voiceServer = new VoiceServer({base: '/voiceapp'})
29
- * voiceServer.listen(handler, { port: 3000 })
30
- */
31
- export default class VoiceResponse {
32
- request: VoiceRequest;
33
- plugins: {};
34
- /**
35
- * Constructs a new VoiceResponse object.
36
- *
37
- * @param {VoiceRequest} request - Options to indicate the objects endpoint
38
- * @see module:core:APIClient
39
- */
40
- constructor(request: VoiceRequest);
41
- /**
42
- * Adds a tts or asr plugin. Only one type of plugin can be attached.
43
- *
44
- * @param plugin
45
- * @see GoogleTTS
46
- * @see GoogleASR
47
- */
48
- use(plugin: Plugin): void;
49
- /**
50
- * Play an audio in the channel.
51
- *
52
- * @param {string} media - Sound name or uri with audio file
53
- * @param {PlayOptions} options - Optional parameters to alter the command's normal
54
- * behavior
55
- * @param {string} options.offset - Milliseconds to skip before playing. Only applies to the first URI if multiple media URIs are specified
56
- * @param {string} options.skip - Milliseconds to skip for forward/reverse operations
57
- * @param {string} options.playbackId - Playback identifier to use in Playback operations
58
- * @see Playback
59
- * @example
60
- *
61
- * async function handler (request, response) {
62
- * await response.answer();
63
- * await response.play("https://soundsserver:9000/sounds/hello-world.wav");
64
- * }
65
- */
66
- play(media: string, options?: PlayOptions): Promise<void>;
67
- /**
68
- * Converts a text into a sound and sends sound to media server. To use this verb, you must
69
- * first setup a TTS plugin such as GoogleTTS, or AWS PollyTTS
70
- *
71
- * @param {string} text - Converts a text into a sound and sends sound to media server
72
- * @param {SayOptions} options - Optional parameters to alter the command's normal
73
- * behavior
74
- * @param {string} options.offset - Milliseconds to skip before playing
75
- * @param {string} options.skip - Milliseconds to skip for forward/reverse operations
76
- * @param {string} options.playbackId - Playback identifier to use in Playback operations
77
- * @see Play
78
- * @see Voice.use
79
- * @example
80
- *
81
- * async function handler (request, response) {
82
- * await response.answer();
83
- * response.use(new GoogleTTS())
84
- * await response.say("Hello workd"); // Plays the sound using GoogleTTS's default values
85
- * }
86
- */
87
- say(text: string, options?: SayOptions): Promise<void>;
88
- /**
89
- * Waits for data entry from the user's keypad or from a speech provider.
90
- *
91
- * @param {GatherOptions} options - Options to select the maximum number of digits, final character, and timeout
92
- * @param {number} options.numDigits - Milliseconds to skip before playing. Only applies to the first URI if multiple media URIs are specified
93
- * @param {number} options.timeout - Milliseconds to wait before timeout. Defaults to 4000. Use zero for no timeout.
94
- * @param {string} options.finishOnKey - Optional last character to wait for. Defaults to '#'. It will not be included in the returned digits
95
- * @param {string} options.source - Where to listen as input source. This option accepts `dtmf` and `speech`. A speech provider must be configure
96
- * when including the `speech` source. You might inclue both with `dtmf,speech`. Defaults to `dtmf`
97
- * @note When including `speech` the default timeout is 10000 (10s).
98
- * @see SpeechProvider
99
- * @example
100
- *
101
- * async function handler (request, response) {
102
- * await response.answer();
103
- * const digits = await response.gather({source: "dtmf,speech", numDigits: 3});
104
- * console.log("digits: " + digits);
105
- * }
106
- */
107
- gather(options?: GatherOptions): Promise<string>;
108
- /**
109
- * Waits for data entry from the user's keypad or from a stream speech provider. This command is different from `gather`
110
- * in that it returns a stream of results instead of a single result. You can think of it as active listening.
111
- *
112
- * @param {SGatherOptions} options - Options object for the SGather verb
113
- * @param {string} options.source - Where to listen as input source. This option accepts `dtmf` and `speech`. A speech provider must be configure
114
- * when including the `speech` source. You might inclue both with `dtmf,speech`. Defaults to `speech,dtmf`
115
- * @return {SGatherStream} The SGatherStream fires events via the `on` method for `transcription`, `dtmf`, and `error`. And the stream can be close
116
- * with the `close` function.
117
- * @see StreamSpeechProvider
118
- * @example
119
- *
120
- * async function handler (request, response) {
121
- * await response.answer();
122
- * const stream = await response.sgather({source: "dtmf,speech"});
123
- *
124
- * stream.on("transcript", (text, isFinal) => {
125
- * console.log("transcript: %s", text);
126
- * })
127
- *
128
- * stream.on("dtmf", digit => {
129
- * console.log("digit: " + digit);
130
- * if (digit === "#") stream.close();
131
- * })
132
- * }
133
- */
134
- sgather(options?: SGatherOptions): Promise<SGatherStream>;
135
- /**
136
- * Sends dtmf tones to the current session.
137
- *
138
- * @param {DtmfOptions} options - Options object for the Dtmf verb
139
- * @param {string} options.dtmf - A string of the dtmf tones
140
- * @example
141
- *
142
- * async function handler (request, response) {
143
- * await response.answer();
144
- * await response.play("sound:hello-world");
145
- * await response.dtmf({dtmf: "1234"});
146
- * }
147
- */
148
- dtmf(options: DtmfOptions): Promise<void>;
149
- /**
150
- * Forwards the call to an Agent or the PSTN.
151
- *
152
- * @param {string} destination - Number or Agent to forward the call to
153
- * @param {DialOptions} options - Options object for the Dial verb
154
- * @param {timeout} options.timeout - Dial timeout
155
- * @return {StatusStream} The StatusStream fires events via the `on` method for `progress`, `answer`, `noanswer`, and `busy`. And the stream can be close
156
- * with the `close` function.
157
- * @example
158
- *
159
- * async function handler (request, response) {
160
- * await response.answer();
161
- * await response.say("dialing number");
162
- * const stream = await response.dial("17853178070");
163
- * stream.on("progress", console.log)
164
- * stream.on("answer", console.log)
165
- * stream.on("busy", console.log)
166
- * }
167
- */
168
- dial(destination: string, options?: DialOptions): Promise<StreamStatus>;
169
- /**
170
- * Returns a PlaybackControl control object.
171
- *
172
- * @param {string} playbackId - Playback identifier to use in Playback operations
173
- * @see Play
174
- * @example
175
- *
176
- * async function handler (request, response) {
177
- * await response.answer();
178
- * response.onDtmfReceived(async(digit) => {
179
- * const control = response.playback("1234")
180
- * digit === "3"
181
- * ? await control.restart()
182
- * : await control.forward()
183
- * })
184
- *
185
- * await response.play("https://soundsserver:9000/sounds/hello-world.wav", {
186
- * playbackId: "1234"
187
- * });
188
- * }
189
- */
190
- playback(playbackId: string): PlaybackControl;
191
- /**
192
- * Listens event publication.
193
- *
194
- * @param {Function} handler - Event handler
195
- * @example
196
- *
197
- * async function handler (request, response) {
198
- * await response.answer();
199
- * response.on("DtmfReceived", async(digit) => {
200
- * const control = response.playback("1234")
201
- * digit === "3"
202
- * ? await control.restart()
203
- * : await control.forward()
204
- * })
205
- *
206
- * await response.play("https://soundsserver:9000/sounds/hello-world.wav", {
207
- * playbackId: "1234"
208
- * });
209
- * }
210
- */
211
- on(topic: string, handler: Function): Promise<void>;
212
- /**
213
- * Mutes a channel.
214
- *
215
- * @param {MuteOptions} options - Indicate which direction of he communication to mute
216
- * @param {string} options.direction - Possible values are 'in', 'out', and 'both'
217
- * @see unmute
218
- * @example
219
- *
220
- * async function handler (request, response) {
221
- * await response.answer();
222
- * await response.mute(); // Will mute both directions
223
- * }
224
- */
225
- mute(options?: MuteOptions): Promise<void>;
226
- /**
227
- * Unmutes a channel.
228
- *
229
- * @param {MuteOptions} options - Indicate which direction of he communication to unmute
230
- * @param {string} options.direction - Possible values are 'in', 'out', and 'both'
231
- * @see mute
232
- * @example
233
- *
234
- * async function handler (request, response) {
235
- * ...
236
- * await response.unmute({direction: "out"}); // Will unmute only the "out" direction
237
- * }
238
- */
239
- unmute(options?: MuteOptions): Promise<void>;
240
- /**
241
- * Answer the communication channel. Before running any other verb you
242
- * must run the anwer command.
243
- *
244
- * @example
245
- *
246
- * async function handler (request, response) {
247
- * await response.answer();
248
- * ...
249
- * }
250
- */
251
- answer(): Promise<void>;
252
- /**
253
- * Terminates the communication channel.
254
- *
255
- * @example
256
- *
257
- * async function handler (request, response) {
258
- * ...
259
- * await response.hangup();
260
- * }
261
- */
262
- hangup(): Promise<void>;
263
- /**
264
- * Records the current channel and uploads the file to the storage subsystem.
265
- *
266
- * @param {RecordOptions} options - optional parameters to alter the command's normal
267
- * behavior
268
- * @param {number} options.maxDuration - Maximum duration of the recording, in seconds. Use `0` for no limit
269
- * @param {number} options.maxSilence - Maximum duration of silence, in seconds. Use `0` for no limit
270
- * @param {boolean} options.beep - Play beep when recording begins
271
- * @param {string} options.finishOnKey - DTMF input to terminate recording
272
- * @return {Promise<RecordResult>} Returns useful information such as the duration of the recording, etc.
273
- * @example
274
- *
275
- * async function handler (request, response) {
276
- * await response.answer();;
277
- * const result = await response.record({finishOnKey: "#"});
278
- * console.log("recording result: " + JSON.stringify(result)) // recording result: { duration: 30 ...}
279
- * }
280
- */
281
- record(options: RecordOptions): Promise<RecordResult>;
282
- openMediaPipe(): Promise<void>;
283
- closeMediaPipe(): Promise<void>;
284
- }
package/dist/voice.js DELETED
@@ -1,367 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- /*
7
- * Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
8
- * http://github.com/fonoster/fonoster
9
- *
10
- * This file is part of Fonoster
11
- *
12
- * Licensed under the MIT License (the "License");
13
- * you may not use this file except in compliance with
14
- * the License. You may obtain a copy of the License at
15
- *
16
- * https://opensource.org/licenses/MIT
17
- *
18
- * Unless required by applicable law or agreed to in writing, software
19
- * distributed under the License is distributed on an "AS IS" BASIS,
20
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
- * See the License for the specific language governing permissions and
22
- * limitations under the License.
23
- */
24
- const playback_1 = require("./playback/playback");
25
- const asserts_1 = require("./asserts");
26
- const verb_1 = require("./verb");
27
- const utils_1 = require("./utils");
28
- const gather_1 = __importDefault(require("./sgather/gather"));
29
- const dtmf_1 = __importDefault(require("./dtmf/dtmf"));
30
- const dial_1 = __importDefault(require("./dial/dial"));
31
- const pubsub_js_1 = __importDefault(require("pubsub-js"));
32
- const answer_1 = __importDefault(require("./answer/answer"));
33
- const hangup_1 = __importDefault(require("./hangup/hangup"));
34
- const unmute_1 = __importDefault(require("./unmute/unmute"));
35
- const gather_2 = __importDefault(require("./gather/gather"));
36
- const mute_1 = __importDefault(require("./mute/mute"));
37
- const play_1 = __importDefault(require("./play/play"));
38
- const record_1 = __importDefault(require("./record/record"));
39
- /**
40
- * @classdesc Use the VoiceResponse object, to construct advance Interactive
41
- * Voice Response (IVR) applications.
42
- *
43
- * @extends Verb
44
- * @example
45
- *
46
- * import { VoiceServer } from "@fonoster/voice";
47
- *
48
- * async function handler (request, response) {
49
- * await response.answer();
50
- * await response.play("sound:hello-world");
51
- * }
52
- *
53
- * const voiceServer = new VoiceServer({base: '/voiceapp'})
54
- * voiceServer.listen(handler, { port: 3000 })
55
- */
56
- class VoiceResponse {
57
- request;
58
- plugins;
59
- /**
60
- * Constructs a new VoiceResponse object.
61
- *
62
- * @param {VoiceRequest} request - Options to indicate the objects endpoint
63
- * @see module:core:APIClient
64
- */
65
- constructor(request) {
66
- this.request = request;
67
- this.plugins = {};
68
- }
69
- /**
70
- * Adds a tts or asr plugin. Only one type of plugin can be attached.
71
- *
72
- * @param plugin
73
- * @see GoogleTTS
74
- * @see GoogleASR
75
- */
76
- use(plugin) {
77
- this.plugins[plugin.getType()] = plugin;
78
- }
79
- /**
80
- * Play an audio in the channel.
81
- *
82
- * @param {string} media - Sound name or uri with audio file
83
- * @param {PlayOptions} options - Optional parameters to alter the command's normal
84
- * behavior
85
- * @param {string} options.offset - Milliseconds to skip before playing. Only applies to the first URI if multiple media URIs are specified
86
- * @param {string} options.skip - Milliseconds to skip for forward/reverse operations
87
- * @param {string} options.playbackId - Playback identifier to use in Playback operations
88
- * @see Playback
89
- * @example
90
- *
91
- * async function handler (request, response) {
92
- * await response.answer();
93
- * await response.play("https://soundsserver:9000/sounds/hello-world.wav");
94
- * }
95
- */
96
- async play(media, options = {}) {
97
- await new play_1.default(this.request).run(media, options);
98
- }
99
- /**
100
- * Converts a text into a sound and sends sound to media server. To use this verb, you must
101
- * first setup a TTS plugin such as GoogleTTS, or AWS PollyTTS
102
- *
103
- * @param {string} text - Converts a text into a sound and sends sound to media server
104
- * @param {SayOptions} options - Optional parameters to alter the command's normal
105
- * behavior
106
- * @param {string} options.offset - Milliseconds to skip before playing
107
- * @param {string} options.skip - Milliseconds to skip for forward/reverse operations
108
- * @param {string} options.playbackId - Playback identifier to use in Playback operations
109
- * @see Play
110
- * @see Voice.use
111
- * @example
112
- *
113
- * async function handler (request, response) {
114
- * await response.answer();
115
- * response.use(new GoogleTTS())
116
- * await response.say("Hello workd"); // Plays the sound using GoogleTTS's default values
117
- * }
118
- */
119
- async say(text, options = {}) {
120
- (0, asserts_1.assertPluginExist)(this, "tts");
121
- const tts = this.plugins["tts"];
122
- // It should return the filename and the generated file location
123
- const result = await tts.synthesize(text, options);
124
- const media = `sound:${this.request.selfEndpoint}/tts/${result.filename}`;
125
- await new play_1.default(this.request).run(media, options);
126
- }
127
- /**
128
- * Waits for data entry from the user's keypad or from a speech provider.
129
- *
130
- * @param {GatherOptions} options - Options to select the maximum number of digits, final character, and timeout
131
- * @param {number} options.numDigits - Milliseconds to skip before playing. Only applies to the first URI if multiple media URIs are specified
132
- * @param {number} options.timeout - Milliseconds to wait before timeout. Defaults to 4000. Use zero for no timeout.
133
- * @param {string} options.finishOnKey - Optional last character to wait for. Defaults to '#'. It will not be included in the returned digits
134
- * @param {string} options.source - Where to listen as input source. This option accepts `dtmf` and `speech`. A speech provider must be configure
135
- * when including the `speech` source. You might inclue both with `dtmf,speech`. Defaults to `dtmf`
136
- * @note When including `speech` the default timeout is 10000 (10s).
137
- * @see SpeechProvider
138
- * @example
139
- *
140
- * async function handler (request, response) {
141
- * await response.answer();
142
- * const digits = await response.gather({source: "dtmf,speech", numDigits: 3});
143
- * console.log("digits: " + digits);
144
- * }
145
- */
146
- async gather(options = { source: "speech,dtmf" }) {
147
- let asr = null;
148
- if (options.source.includes("speech")) {
149
- (0, asserts_1.assertPluginExist)(this, "asr");
150
- asr = this.plugins["asr"];
151
- }
152
- const result = await new gather_2.default(this.request, asr).run(options);
153
- return result;
154
- }
155
- /**
156
- * Waits for data entry from the user's keypad or from a stream speech provider. This command is different from `gather`
157
- * in that it returns a stream of results instead of a single result. You can think of it as active listening.
158
- *
159
- * @param {SGatherOptions} options - Options object for the SGather verb
160
- * @param {string} options.source - Where to listen as input source. This option accepts `dtmf` and `speech`. A speech provider must be configure
161
- * when including the `speech` source. You might inclue both with `dtmf,speech`. Defaults to `speech,dtmf`
162
- * @return {SGatherStream} The SGatherStream fires events via the `on` method for `transcription`, `dtmf`, and `error`. And the stream can be close
163
- * with the `close` function.
164
- * @see StreamSpeechProvider
165
- * @example
166
- *
167
- * async function handler (request, response) {
168
- * await response.answer();
169
- * const stream = await response.sgather({source: "dtmf,speech"});
170
- *
171
- * stream.on("transcript", (text, isFinal) => {
172
- * console.log("transcript: %s", text);
173
- * })
174
- *
175
- * stream.on("dtmf", digit => {
176
- * console.log("digit: " + digit);
177
- * if (digit === "#") stream.close();
178
- * })
179
- * }
180
- */
181
- async sgather(options = { source: "speech,dtmf" }) {
182
- let asr = null;
183
- if (options.source.includes("speech")) {
184
- (0, asserts_1.assertPluginExist)(this, "asr");
185
- asr = this.plugins["asr"];
186
- }
187
- return await new gather_1.default(this.request, asr).run(options);
188
- }
189
- /**
190
- * Sends dtmf tones to the current session.
191
- *
192
- * @param {DtmfOptions} options - Options object for the Dtmf verb
193
- * @param {string} options.dtmf - A string of the dtmf tones
194
- * @example
195
- *
196
- * async function handler (request, response) {
197
- * await response.answer();
198
- * await response.play("sound:hello-world");
199
- * await response.dtmf({dtmf: "1234"});
200
- * }
201
- */
202
- async dtmf(options) {
203
- return await new dtmf_1.default(this.request).run(options);
204
- }
205
- /**
206
- * Forwards the call to an Agent or the PSTN.
207
- *
208
- * @param {string} destination - Number or Agent to forward the call to
209
- * @param {DialOptions} options - Options object for the Dial verb
210
- * @param {timeout} options.timeout - Dial timeout
211
- * @return {StatusStream} The StatusStream fires events via the `on` method for `progress`, `answer`, `noanswer`, and `busy`. And the stream can be close
212
- * with the `close` function.
213
- * @example
214
- *
215
- * async function handler (request, response) {
216
- * await response.answer();
217
- * await response.say("dialing number");
218
- * const stream = await response.dial("17853178070");
219
- * stream.on("progress", console.log)
220
- * stream.on("answer", console.log)
221
- * stream.on("busy", console.log)
222
- * }
223
- */
224
- async dial(destination, options) {
225
- return await new dial_1.default(this.request).run(destination, options);
226
- }
227
- /**
228
- * Returns a PlaybackControl control object.
229
- *
230
- * @param {string} playbackId - Playback identifier to use in Playback operations
231
- * @see Play
232
- * @example
233
- *
234
- * async function handler (request, response) {
235
- * await response.answer();
236
- * response.onDtmfReceived(async(digit) => {
237
- * const control = response.playback("1234")
238
- * digit === "3"
239
- * ? await control.restart()
240
- * : await control.forward()
241
- * })
242
- *
243
- * await response.play("https://soundsserver:9000/sounds/hello-world.wav", {
244
- * playbackId: "1234"
245
- * });
246
- * }
247
- */
248
- playback(playbackId) {
249
- return new playback_1.PlaybackControl(this.request, playbackId);
250
- }
251
- /**
252
- * Listens event publication.
253
- *
254
- * @param {Function} handler - Event handler
255
- * @example
256
- *
257
- * async function handler (request, response) {
258
- * await response.answer();
259
- * response.on("DtmfReceived", async(digit) => {
260
- * const control = response.playback("1234")
261
- * digit === "3"
262
- * ? await control.restart()
263
- * : await control.forward()
264
- * })
265
- *
266
- * await response.play("https://soundsserver:9000/sounds/hello-world.wav", {
267
- * playbackId: "1234"
268
- * });
269
- * }
270
- */
271
- async on(topic, handler) {
272
- pubsub_js_1.default.subscribe(`${topic}.${this.request.sessionId}`, (type, data) => {
273
- handler(data);
274
- });
275
- }
276
- /**
277
- * Mutes a channel.
278
- *
279
- * @param {MuteOptions} options - Indicate which direction of he communication to mute
280
- * @param {string} options.direction - Possible values are 'in', 'out', and 'both'
281
- * @see unmute
282
- * @example
283
- *
284
- * async function handler (request, response) {
285
- * await response.answer();
286
- * await response.mute(); // Will mute both directions
287
- * }
288
- */
289
- async mute(options) {
290
- return new mute_1.default(this.request).run(options);
291
- }
292
- /**
293
- * Unmutes a channel.
294
- *
295
- * @param {MuteOptions} options - Indicate which direction of he communication to unmute
296
- * @param {string} options.direction - Possible values are 'in', 'out', and 'both'
297
- * @see mute
298
- * @example
299
- *
300
- * async function handler (request, response) {
301
- * ...
302
- * await response.unmute({direction: "out"}); // Will unmute only the "out" direction
303
- * }
304
- */
305
- async unmute(options) {
306
- return new unmute_1.default(this.request).run(options);
307
- }
308
- /**
309
- * Answer the communication channel. Before running any other verb you
310
- * must run the anwer command.
311
- *
312
- * @example
313
- *
314
- * async function handler (request, response) {
315
- * await response.answer();
316
- * ...
317
- * }
318
- */
319
- async answer() {
320
- return new answer_1.default(this.request).run();
321
- }
322
- /**
323
- * Terminates the communication channel.
324
- *
325
- * @example
326
- *
327
- * async function handler (request, response) {
328
- * ...
329
- * await response.hangup();
330
- * }
331
- */
332
- async hangup() {
333
- return new hangup_1.default(this.request).run();
334
- }
335
- /**
336
- * Records the current channel and uploads the file to the storage subsystem.
337
- *
338
- * @param {RecordOptions} options - optional parameters to alter the command's normal
339
- * behavior
340
- * @param {number} options.maxDuration - Maximum duration of the recording, in seconds. Use `0` for no limit
341
- * @param {number} options.maxSilence - Maximum duration of silence, in seconds. Use `0` for no limit
342
- * @param {boolean} options.beep - Play beep when recording begins
343
- * @param {string} options.finishOnKey - DTMF input to terminate recording
344
- * @return {Promise<RecordResult>} Returns useful information such as the duration of the recording, etc.
345
- * @example
346
- *
347
- * async function handler (request, response) {
348
- * await response.answer();;
349
- * const result = await response.record({finishOnKey: "#"});
350
- * console.log("recording result: " + JSON.stringify(result)) // recording result: { duration: 30 ...}
351
- * }
352
- */
353
- async record(options) {
354
- return await new record_1.default(this.request).run(options);
355
- }
356
- // Requests media from Media server
357
- async openMediaPipe() {
358
- const genericVerb = new verb_1.Verb(this.request);
359
- await (0, utils_1.startMediaTransfer)(genericVerb, this.request.sessionId);
360
- }
361
- // Requests media stop from Media server
362
- async closeMediaPipe() {
363
- const genericVerb = new verb_1.Verb(this.request);
364
- await (0, utils_1.stopMediaTransfer)(genericVerb, this.request.sessionId);
365
- }
366
- }
367
- exports.default = VoiceResponse;