@ipcom/asterisk-ari 0.0.139 → 0.0.141

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/README.md ADDED
@@ -0,0 +1,311 @@
1
+ # @ipcom/asterisk-ari
2
+
3
+ A modern JavaScript/TypeScript library for interacting with the Asterisk REST Interface (ARI).
4
+
5
+ ## Features
6
+
7
+ - Complete Asterisk ARI support
8
+ - Written in TypeScript with full type support
9
+ - WebSocket support for real-time events
10
+ - Automatic reconnection management
11
+ - Simplified channel and playback handling
12
+ - ESM and CommonJS support
13
+ - Complete type documentation
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @ipcom/asterisk-ari
19
+ ```
20
+
21
+ ## Basic Usage
22
+
23
+ ### Initial Setup
24
+
25
+ ```typescript
26
+ import { AriClient } from '@ipcom/asterisk-ari';
27
+
28
+ const client = new AriClient({
29
+ host: 'localhost', // Asterisk host
30
+ port: 8088, // ARI port
31
+ username: 'username', // ARI username
32
+ password: 'password', // ARI password
33
+ secure: false // Use true for HTTPS/WSS
34
+ });
35
+ ```
36
+
37
+ ### WebSocket Connection
38
+
39
+ ```typescript
40
+ // Connect to WebSocket to receive events
41
+ await client.connectWebSocket(['myApp']); // 'myApp' is your application name
42
+
43
+ // Listen for specific events
44
+ client.on('StasisStart', event => {
45
+ console.log('New channel started:', event.channel.id);
46
+ });
47
+
48
+ client.on('StasisEnd', event => {
49
+ console.log('Channel ended:', event.channel.id);
50
+ });
51
+
52
+ // Listen for DTMF events
53
+ client.on('ChannelDtmfReceived', event => {
54
+ console.log('DTMF received:', event.digit);
55
+ });
56
+
57
+ // Close WebSocket connection
58
+ client.closeWebSocket();
59
+ ```
60
+
61
+ ## Event Instances
62
+
63
+ ### Channel and Playback Instances in Events
64
+
65
+ When working with WebSocket events, you get access to both the raw event data and convenient instance objects that allow direct interaction with the channel or playback:
66
+
67
+ ```typescript
68
+ client.on('StasisStart', async event => {
69
+ // event.channel contains the raw channel data
70
+ console.log('New channel started:', event.channel.id);
71
+
72
+ // event.instanceChannel provides a ready-to-use ChannelInstance
73
+ const channelInstance = event.instanceChannel;
74
+
75
+ // You can directly interact with the channel through the instance
76
+ await channelInstance.answer();
77
+ await channelInstance.play({ media: 'sound:welcome' });
78
+ });
79
+
80
+ // Similarly for playback events
81
+ client.on('PlaybackStarted', async event => {
82
+ // event.playback contains the raw playback data
83
+ console.log('Playback ID:', event.playback.id);
84
+
85
+ // event.instancePlayback provides a ready-to-use PlaybackInstance
86
+ const playbackInstance = event.instancePlayback;
87
+
88
+ // Direct control through the instance
89
+ await playbackInstance.control('pause');
90
+ });
91
+ ```
92
+
93
+ This approach provides two key benefits:
94
+ 1. No need to manually create instances using `client.Channel()` or `client.Playback()`
95
+ 2. Direct access to control methods without additional setup
96
+
97
+ ### Comparing Approaches
98
+
99
+ Traditional approach:
100
+ ```typescript
101
+ client.on('StasisStart', async event => {
102
+ // Need to create channel instance manually
103
+ const channel = client.Channel(event.channel.id);
104
+ await channel.answer();
105
+ });
106
+ ```
107
+
108
+ Using instance from event:
109
+ ```typescript
110
+ client.on('StasisStart', async event => {
111
+ // Instance is already available
112
+ await event.instanceChannel.answer();
113
+ });
114
+ ```
115
+
116
+ This feature is particularly useful when handling multiple events and needing to perform actions on channels or playbacks immediately within event handlers.
117
+
118
+ ### Channel Handling
119
+
120
+ ```typescript
121
+ // Create a channel instance
122
+ const channel = client.Channel();
123
+
124
+ // Originate a call
125
+ await channel.originate({
126
+ endpoint: 'PJSIP/1000',
127
+ extension: '1001',
128
+ context: 'default',
129
+ priority: 1
130
+ });
131
+
132
+ // Answer a call
133
+ await channel.answer();
134
+
135
+ // Play audio
136
+ const playback = await channel.play({
137
+ media: 'sound:welcome'
138
+ });
139
+
140
+ // Hangup the channel
141
+ await channel.hangup();
142
+ ```
143
+
144
+ ### Playback Handling
145
+
146
+ ```typescript
147
+ // Create a playback instance
148
+ const playback = client.Playback();
149
+
150
+ // Monitor playback events
151
+ playback.on('PlaybackStarted', event => {
152
+ console.log('Playback started:', event.playback.id);
153
+ });
154
+
155
+ playback.on('PlaybackFinished', event => {
156
+ console.log('Playback finished:', event.playback.id);
157
+ });
158
+
159
+ // Control playback
160
+ await playback.control('pause'); // Pause
161
+ await playback.control('unpause'); // Resume
162
+ await playback.control('restart'); // Restart
163
+ await playback.stop(); // Stop
164
+ ```
165
+
166
+ ### Specific Channel Monitoring
167
+
168
+ ```typescript
169
+ // Create an instance for a specific channel
170
+ const channel = client.Channel('channel-id');
171
+
172
+ // Monitor specific channel events
173
+ channel.on('ChannelStateChange', event => {
174
+ console.log('Channel state changed:', event.channel.state);
175
+ });
176
+
177
+ channel.on('ChannelDtmfReceived', event => {
178
+ console.log('DTMF received on channel:', event.digit);
179
+ });
180
+
181
+ // Get channel details
182
+ const details = await channel.getDetails();
183
+ console.log('Channel details:', details);
184
+
185
+ // Handle channel variables
186
+ await channel.getVariable('CALLERID');
187
+ await channel.setVariable('CUSTOM_VAR', 'value');
188
+ ```
189
+
190
+ ### Channel Playback Handling
191
+
192
+ ```typescript
193
+ // Play audio on a specific channel
194
+ const channel = client.Channel('channel-id');
195
+ const playback = await channel.play({
196
+ media: 'sound:welcome',
197
+ lang: 'en'
198
+ });
199
+
200
+ // Monitor specific playback
201
+ playback.on('PlaybackStarted', event => {
202
+ console.log('Playback started on channel');
203
+ });
204
+
205
+ // Control playback
206
+ await channel.stopPlayback(playback.id);
207
+ await channel.pausePlayback(playback.id);
208
+ await channel.resumePlayback(playback.id);
209
+ ```
210
+
211
+ ## Error Handling
212
+
213
+ ```typescript
214
+ try {
215
+ await client.connectWebSocket(['myApp']);
216
+ } catch (error) {
217
+ console.error('Error connecting to WebSocket:', error);
218
+ }
219
+
220
+ // Using with async/await
221
+ try {
222
+ const channel = client.Channel();
223
+ await channel.originate({
224
+ endpoint: 'PJSIP/1000'
225
+ });
226
+ } catch (error) {
227
+ console.error('Error originating call:', error);
228
+ }
229
+ ```
230
+
231
+ ## TypeScript Support
232
+
233
+ The library provides complete type definitions for all operations:
234
+
235
+ ```typescript
236
+ import type {
237
+ Channel,
238
+ ChannelEvent,
239
+ WebSocketEvent
240
+ } from '@ipcom/asterisk-ari';
241
+
242
+ // Types will be available for use
243
+ const handleChannelEvent = (event: ChannelEvent) => {
244
+ const channelId: string = event.channel.id;
245
+ };
246
+ ```
247
+
248
+ ## Additional Features
249
+
250
+ The library provides access to many other ARI features:
251
+
252
+ - Bridge management
253
+ - Endpoint handling
254
+ - Sound manipulation
255
+ - Application control
256
+ - Asterisk system information
257
+ - And much more...
258
+
259
+ ## Advanced Examples
260
+
261
+ ### Bridge Creation and Channel Management
262
+
263
+ ```typescript
264
+ // Create and manage a bridge
265
+ const bridge = await client.bridges.createBridge({
266
+ type: 'mixing',
267
+ name: 'myBridge'
268
+ });
269
+
270
+ // Add channels to bridge
271
+ await client.bridges.addChannels(bridge.id, {
272
+ channel: ['channel-id-1', 'channel-id-2']
273
+ });
274
+ ```
275
+
276
+ ### Recording Management
277
+
278
+ ```typescript
279
+ // Start recording on a channel
280
+ const channel = client.Channel('channel-id');
281
+ await channel.record({
282
+ name: 'recording-name',
283
+ format: 'wav',
284
+ maxDurationSeconds: 60,
285
+ beep: true
286
+ });
287
+ ```
288
+
289
+ ### External Media
290
+
291
+ ```typescript
292
+ // Create external media channel
293
+ const channel = await client.channels.createExternalMedia({
294
+ app: 'myApp',
295
+ external_host: 'media-server:8088',
296
+ format: 'slin16'
297
+ });
298
+ ```
299
+
300
+ ## API Reference
301
+
302
+ For complete API documentation, please refer to the TypeScript types and interfaces exported by the package.
303
+
304
+ ## Contributing
305
+
306
+ Contributions are welcome! Please feel free to submit a Pull Request.
307
+
308
+ ## License
309
+
310
+ Apache-2.0
311
+