@ipcom/asterisk-ari 0.0.38 → 0.0.40

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.
@@ -1740,7 +1740,7 @@ var AriClient = class {
1740
1740
  async connectWebSocket(app, subscribedEvents) {
1741
1741
  if (!app) {
1742
1742
  throw new Error(
1743
- "The 'app' parameter is required to connect to the WebSocket."
1743
+ "O par\xE2metro 'app' \xE9 obrigat\xF3rio para conectar ao WebSocket."
1744
1744
  );
1745
1745
  }
1746
1746
  if (this.webSocketReady) {
@@ -1751,16 +1751,14 @@ var AriClient = class {
1751
1751
  try {
1752
1752
  if (this.isReconnecting) {
1753
1753
  console.warn(
1754
- "Already attempting to reconnect. Skipping this attempt."
1754
+ "J\xE1 est\xE1 tentando reconectar. Ignorando esta tentativa."
1755
1755
  );
1756
1756
  return;
1757
1757
  }
1758
1758
  this.isReconnecting = true;
1759
1759
  const eventsParam = subscribedEvents && subscribedEvents.length > 0 ? `&event=${subscribedEvents.join(",")}` : "&subscribeAll=true";
1760
1760
  const protocol = this.config.secure ? "wss" : "ws";
1761
- const wsUrl = `${protocol}://${encodeURIComponent(
1762
- this.config.username
1763
- )}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
1761
+ const wsUrl = `${protocol}://${encodeURIComponent(this.config.username)}:${encodeURIComponent(this.config.password)}@${this.config.host}:${this.config.port}/ari/events?app=${app}${eventsParam}`;
1764
1762
  const backoffOptions = {
1765
1763
  delayFirstAttempt: false,
1766
1764
  startingDelay: 1e3,
@@ -1788,6 +1786,7 @@ var AriClient = class {
1788
1786
  console.log(`WebSocket conectado para o app: ${app}`);
1789
1787
  await this.ensureAppRegistered(app);
1790
1788
  this.processPendingListeners();
1789
+ this.isWebSocketConnectedFlag = true;
1791
1790
  }, backoffOptions);
1792
1791
  resolve();
1793
1792
  } catch (err) {
@@ -1813,46 +1812,30 @@ var AriClient = class {
1813
1812
  );
1814
1813
  }
1815
1814
  const eventHandlers = {
1816
- PlaybackFinished: (data) => {
1815
+ PlaybackStarted: (data) => {
1817
1816
  if ("playbackId" in data) {
1818
- this.playbacks.emitPlaybackEvent("PlaybackFinished", data);
1817
+ console.log(`[${app}] PlaybackStarted:`, data);
1819
1818
  }
1820
- console.log(`[${app}] PlaybackFinished:`, data);
1821
- this.emitGlobalEvent(data);
1822
1819
  },
1823
- ChannelStateChange: (data) => {
1824
- if ("channel" in data) {
1825
- console.log(`[${app}] Estado do canal alterado:`, data.channel);
1826
- this.channels.emitChannelEvent("ChannelStateChange", data);
1827
- }
1828
- this.emitGlobalEvent(data);
1829
- },
1830
- BridgeDestroyed: (data) => {
1831
- if ("bridge" in data) {
1832
- console.log(`[${app}] Bridge destru\xEDda:`, data.bridge);
1820
+ PlaybackFinished: (data) => {
1821
+ if ("playbackId" in data) {
1822
+ console.log(`[${app}] PlaybackFinished:`, data);
1833
1823
  }
1834
- this.emitGlobalEvent(data);
1835
1824
  },
1836
1825
  ChannelDtmfReceived: (data) => {
1837
- if ("channel" in data) {
1838
- console.log(`[${app}] DTMF recebido no canal:`, data.channel);
1839
- this.channels.emitChannelEvent("ChannelDtmfReceived", data);
1840
- }
1841
- this.emitGlobalEvent(data);
1842
- },
1843
- // Adicione mais eventos conforme necessário
1844
- EndpointStateChange: (data) => {
1845
- if ("endpoint" in data) {
1846
- console.log(`[${app}] Estado do endpoint alterado:`, data.endpoint);
1826
+ if (data.type === "ChannelDtmfReceived" && "digit" in data) {
1827
+ console.log(`[${app}] DTMF recebido: ${data.digit}`);
1828
+ } else {
1829
+ console.warn(
1830
+ `[${app}] Evento inesperado em ChannelDtmfReceived`,
1831
+ data
1832
+ );
1847
1833
  }
1848
- this.emitGlobalEvent(data);
1849
1834
  },
1850
- ChannelHangupRequest: (data) => {
1835
+ ChannelStateChange: (data) => {
1851
1836
  if ("channel" in data) {
1852
- console.log(`[${app}] Requisi\xE7\xE3o de hangup no canal:`, data.channel);
1853
- this.channels.emitChannelEvent("ChannelHangupRequest", data);
1837
+ console.log(`[${app}] Estado do canal alterado:`, data.channel);
1854
1838
  }
1855
- this.emitGlobalEvent(data);
1856
1839
  }
1857
1840
  };
1858
1841
  for (const [eventType, handler] of Object.entries(eventHandlers)) {
@@ -1896,6 +1879,17 @@ var AriClient = class {
1896
1879
  unregisterPlaybackListener(eventType, playbackId, callback) {
1897
1880
  this.playbacks.unregisterListener(eventType, playbackId, callback);
1898
1881
  }
1882
+ registerListener(eventType, condition, callback) {
1883
+ if (!this.isWebSocketConnected()) {
1884
+ throw new Error("WebSocket n\xE3o est\xE1 conectado.");
1885
+ }
1886
+ const handler = (eventData) => {
1887
+ if (eventData.type === eventType && condition(eventData)) {
1888
+ callback(eventData);
1889
+ }
1890
+ };
1891
+ this.wsClient?.on(eventType, handler);
1892
+ }
1899
1893
  /**
1900
1894
  * Registers a listener for playback events.
1901
1895
  * The listener is triggered for events such as "PlaybackFinished".
@@ -1905,7 +1899,34 @@ var AriClient = class {
1905
1899
  * @param callback - The callback function to execute when the event occurs.
1906
1900
  */
1907
1901
  registerPlaybackListener(eventType, playbackId, callback) {
1908
- this.playbacks.registerListener(eventType, playbackId, callback);
1902
+ if (!this.isWebSocketConnected()) {
1903
+ throw new Error("WebSocket n\xE3o est\xE1 conectado.");
1904
+ }
1905
+ console.log(
1906
+ `Registrando listener para evento ${eventType} com playbackId ${playbackId}`
1907
+ );
1908
+ const handler = (eventData) => {
1909
+ if ("playbackId" in eventData && eventData.playbackId === playbackId) {
1910
+ callback(eventData);
1911
+ }
1912
+ };
1913
+ this.wsClient?.on(eventType, handler);
1914
+ }
1915
+ registerChannelListener(eventType, channelId, callback) {
1916
+ if (!this.isWebSocketConnected()) {
1917
+ throw new Error("WebSocket n\xE3o est\xE1 conectado.");
1918
+ }
1919
+ console.log(
1920
+ `Registrando listener para evento ${eventType} com channelId ${channelId}`
1921
+ );
1922
+ const handler = (eventData) => {
1923
+ if (eventData.type === eventType && "channel" in eventData && eventData.channel?.id === channelId) {
1924
+ callback(
1925
+ eventData
1926
+ );
1927
+ }
1928
+ };
1929
+ this.wsClient?.on(eventType, handler);
1909
1930
  }
1910
1931
  /**
1911
1932
  * Checks if a listener is already registered for a specific event and playback.