@gamastudio/sendwave-provider 0.0.2 → 0.0.4

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.
@@ -7,4 +7,5 @@ export interface ParsedMessage {
7
7
  type: string;
8
8
  media?: string;
9
9
  caption?: string;
10
+ originalPayload: any;
10
11
  }
@@ -1,5 +1,5 @@
1
1
  import { SendOptions } from "@builderbot/bot/dist/types";
2
- import { SendButton, SendList, SendMedia, SendMessage, SendPresence } from "../interface/types";
2
+ import { SendButton, SendList, SendLocation, SendMedia, SendMessage, SendPresence, SendReaction } from "../interface/types";
3
3
  export interface ProviderInterface {
4
4
  sendMessage?: (number: string, message: string, options?: SendOptions) => Promise<any>;
5
5
  sendText: (data: SendMessage) => Promise<any>;
@@ -13,4 +13,6 @@ export interface ProviderInterface {
13
13
  sendList: (data: SendList) => Promise<any>;
14
14
  sendPresence: (data: SendPresence) => Promise<any>;
15
15
  sendButton: (data: SendButton) => Promise<any>;
16
+ sendLocation: (data: SendLocation) => Promise<any>;
17
+ sendReaction: (data: SendReaction) => Promise<any>;
16
18
  }
@@ -15,6 +15,9 @@ export interface GlobalVendorArgs {
15
15
  message?: {
16
16
  mergeMessage?: boolean;
17
17
  timeMergeMessage?: number;
18
+ maxWaitTime?: number;
19
+ shortMessageThreshold?: number;
20
+ adaptiveTimer?: boolean;
18
21
  };
19
22
  queueFlow?: {
20
23
  enabled?: boolean;
@@ -84,3 +87,17 @@ export interface SendButton extends SendMessage {
84
87
  text: string;
85
88
  }[];
86
89
  }
90
+ export interface SendLocation extends SendMessage {
91
+ name: string;
92
+ address: string;
93
+ latitude: number;
94
+ longitude: number;
95
+ }
96
+ export interface SendReaction extends SendMessage {
97
+ key: {
98
+ remoteJid: string;
99
+ fromMe: boolean;
100
+ id: string;
101
+ };
102
+ reaction: string;
103
+ }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gamastudio/sendwave-provider",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Librería para interactuar con Sendwave usando configuración dinámica.",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -16,5 +16,7 @@ export declare class SendWaveCore extends EventEmitter {
16
16
  private processMessage;
17
17
  private messageBuffers;
18
18
  private bufferMessage;
19
+ private calculateSmartTimeout;
20
+ private processBufferedMessages;
19
21
  private flowMessage;
20
22
  }
@@ -77,25 +77,82 @@ class SendWaveCore extends node_events_1.EventEmitter {
77
77
  bufferMessage(data) {
78
78
  var _a;
79
79
  const user = data.from;
80
+ const now = Date.now();
80
81
  if (!this.messageBuffers[user]) {
81
- this.messageBuffers[user] = { messages: [], timeout: null };
82
+ this.messageBuffers[user] = {
83
+ messages: [],
84
+ messageTimeout: null,
85
+ maxWaitTimeout: null,
86
+ lastData: null,
87
+ lastMessageTime: now,
88
+ messageCount: 0,
89
+ };
82
90
  }
83
- this.messageBuffers[user].messages.push(data.body);
84
- // Reinicia el timer si ya existe
85
- if (this.messageBuffers[user].timeout) {
86
- clearTimeout(this.messageBuffers[user].timeout);
91
+ const buffer = this.messageBuffers[user];
92
+ buffer.messages.push(data.body);
93
+ buffer.lastData = data;
94
+ buffer.messageCount++;
95
+ // Calculate time between messages
96
+ const timeBetweenMessages = now - buffer.lastMessageTime;
97
+ buffer.lastMessageTime = now;
98
+ // Clear existing timeout
99
+ if (buffer.messageTimeout) {
100
+ clearTimeout(buffer.messageTimeout);
101
+ buffer.messageTimeout = null;
87
102
  }
88
- // Espera 3 segundos desde el último mensaje para procesar
89
- this.messageBuffers[user].timeout = setTimeout(() => {
90
- const fullMessage = this.messageBuffers[user].messages.join(" ");
91
- // Aquí es donde haces el procesamiento real
92
- this.messageQueue.enqueue(() => this.processMessage({
93
- ...data,
94
- body: fullMessage,
95
- }));
96
- // Limpia el buffer
97
- delete this.messageBuffers[user];
98
- }, ((_a = this.globalVendorArgs.message) === null || _a === void 0 ? void 0 : _a.timeMergeMessage) * 1000 || 3 * 1000);
103
+ // Set max wait timeout only on first message
104
+ if (buffer.messages.length === 1) {
105
+ const maxWaitTime = ((_a = this.globalVendorArgs.message) === null || _a === void 0 ? void 0 : _a.maxWaitTime) || 30;
106
+ buffer.maxWaitTimeout = setTimeout(() => {
107
+ this.processBufferedMessages(user);
108
+ }, maxWaitTime * 1000);
109
+ }
110
+ // Smart timeout calculation
111
+ let timeout = this.calculateSmartTimeout(data.body, timeBetweenMessages, buffer.messageCount);
112
+ buffer.messageTimeout = setTimeout(() => {
113
+ this.processBufferedMessages(user);
114
+ }, timeout);
115
+ }
116
+ calculateSmartTimeout(messageText, timeBetweenMessages, messageCount) {
117
+ var _a, _b, _c, _d;
118
+ const baseTimeout = (((_a = this.globalVendorArgs.message) === null || _a === void 0 ? void 0 : _a.timeMergeMessage) || 3) * 1000;
119
+ const shortThreshold = ((_b = this.globalVendorArgs.message) === null || _b === void 0 ? void 0 : _b.shortMessageThreshold) || 10;
120
+ const adaptiveTimer = (_d = (_c = this.globalVendorArgs.message) === null || _c === void 0 ? void 0 : _c.adaptiveTimer) !== null && _d !== void 0 ? _d : true;
121
+ let timeout = baseTimeout;
122
+ // If message is very short, assume more are coming
123
+ if (messageText.length < shortThreshold) {
124
+ timeout = Math.max(1000, timeout * 0.5); // Reduce timeout by 50%
125
+ }
126
+ // If adaptive timer is enabled
127
+ if (adaptiveTimer) {
128
+ // If messages are coming very fast (< 2 seconds), extend timeout
129
+ if (timeBetweenMessages < 2000 && messageCount > 1) {
130
+ timeout = Math.max(timeout, 4000); // At least 4 seconds
131
+ }
132
+ // If it's the first few messages, be more aggressive
133
+ if (messageCount <= 2) {
134
+ timeout = Math.max(2000, timeout * 0.7); // Reduce by 30%
135
+ }
136
+ }
137
+ return timeout;
138
+ }
139
+ processBufferedMessages(user) {
140
+ const buffer = this.messageBuffers[user];
141
+ if (!buffer || buffer.messages.length === 0)
142
+ return;
143
+ // Clear all timeouts
144
+ if (buffer.messageTimeout)
145
+ clearTimeout(buffer.messageTimeout);
146
+ if (buffer.maxWaitTimeout)
147
+ clearTimeout(buffer.maxWaitTimeout);
148
+ const fullMessage = buffer.messages.join(" ");
149
+ // Process the merged message
150
+ this.messageQueue.enqueue(() => this.processMessage({
151
+ ...buffer.lastData,
152
+ body: fullMessage,
153
+ }));
154
+ // Clean up buffer
155
+ delete this.messageBuffers[user];
99
156
  }
100
157
  flowMessage(message) {
101
158
  return new Promise((resolve, reject) => {
@@ -1,4 +1,4 @@
1
- import { GlobalVendorArgs, ProviderInterface, SendButton, SendList, SendMedia, SendMessage, SendPresence } from "../core/interface";
1
+ import { GlobalVendorArgs, ProviderInterface, SendButton, SendList, SendMedia, SendMessage, SendPresence, SendLocation, SendReaction } from "../core/interface";
2
2
  export declare class SenderMessage implements ProviderInterface {
3
3
  private sendwaveApi?;
4
4
  private globalVendorArgs?;
@@ -13,4 +13,6 @@ export declare class SenderMessage implements ProviderInterface {
13
13
  sendVideo(data: SendMedia): Promise<import("axios").AxiosResponse<any, any, {}> | undefined>;
14
14
  sendVoice(data: SendMedia): Promise<import("axios").AxiosResponse<any, any, {}> | undefined>;
15
15
  sendButton(data: SendButton): Promise<import("axios").AxiosResponse<any, any, {}> | undefined>;
16
+ sendLocation(data: SendLocation): Promise<import("axios").AxiosResponse<any, any, {}> | undefined>;
17
+ sendReaction(data: SendReaction): Promise<import("axios").AxiosResponse<any, any, {}> | undefined>;
16
18
  }
@@ -199,5 +199,35 @@ class SenderMessage {
199
199
  console.error(error.response.data.response.message);
200
200
  }
201
201
  }
202
+ async sendLocation(data) {
203
+ var _a, _b;
204
+ try {
205
+ return await ((_a = this.sendwaveApi) === null || _a === void 0 ? void 0 : _a.post(`/message/sendLocation/${(_b = this.globalVendorArgs) === null || _b === void 0 ? void 0 : _b.name}`, {
206
+ number: data.from.split("@")[0],
207
+ name: data.name,
208
+ address: data.address,
209
+ latitude: data.latitude,
210
+ longitude: data.longitude,
211
+ ...this.globalVendorArgs,
212
+ }));
213
+ }
214
+ catch (error) {
215
+ console.error(error.response.data.response.message);
216
+ }
217
+ }
218
+ async sendReaction(data) {
219
+ var _a, _b;
220
+ try {
221
+ return await ((_a = this.sendwaveApi) === null || _a === void 0 ? void 0 : _a.post(`/message/sendReaction/${(_b = this.globalVendorArgs) === null || _b === void 0 ? void 0 : _b.name}`, {
222
+ number: data.from.split("@")[0],
223
+ key: data.key,
224
+ reaction: data.reaction,
225
+ ...this.globalVendorArgs,
226
+ }));
227
+ }
228
+ catch (error) {
229
+ console.error(error.response.data.response.message);
230
+ }
231
+ }
202
232
  }
203
233
  exports.SenderMessage = SenderMessage;
@@ -94,7 +94,7 @@ function parseIncomingMsg(payload) {
94
94
  type,
95
95
  media,
96
96
  caption,
97
- // originalPayload: payload,
97
+ originalPayload: payload.data,
98
98
  };
99
99
  }
100
100
  catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gamastudio/sendwave-provider",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Librería para interactuar con Sendwave usando configuración dinámica.",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -1,9 +0,0 @@
1
- {
2
- "permissions": {
3
- "allow": [
4
- "Read(//c/Users/ameth.galarcio/Documents/Azure/bot-cnb/src/presentation/flows/**)"
5
- ],
6
- "deny": [],
7
- "ask": []
8
- }
9
- }