wa-multi-mongodb 3.10.2 → 3.10.3
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/dist/Defaults/index.js +4 -9
- package/dist/Error/index.js +1 -5
- package/dist/Messaging/index.d.ts +1 -1
- package/dist/Messaging/index.js +123 -137
- package/dist/Profile/index.d.ts +1 -1
- package/dist/Profile/index.js +6 -10
- package/dist/Socket/index.d.ts +1 -1
- package/dist/Socket/index.d.ts.map +1 -1
- package/dist/Socket/index.js +134 -200
- package/dist/Types/index.js +1 -2
- package/dist/Types/profile.js +1 -2
- package/dist/Utils/create-delay.js +2 -7
- package/dist/Utils/error.js +1 -4
- package/dist/Utils/group-cache.js +8 -15
- package/dist/Utils/index.d.ts +5 -5
- package/dist/Utils/index.js +5 -21
- package/dist/Utils/is-exist.d.ts +1 -1
- package/dist/Utils/is-exist.js +7 -10
- package/dist/Utils/lid-utils.js +13 -20
- package/dist/Utils/message-status.d.ts +1 -1
- package/dist/Utils/message-status.js +7 -11
- package/dist/Utils/mongo-auth-state.js +10 -13
- package/dist/Utils/phone-to-jid.js +6 -14
- package/dist/Utils/save-media.d.ts +1 -1
- package/dist/Utils/save-media.js +16 -26
- package/dist/cjs/Defaults/index.d.ts +21 -0
- package/dist/cjs/Defaults/index.d.ts.map +1 -0
- package/dist/cjs/Defaults/index.js +31 -0
- package/dist/cjs/Error/index.d.ts +5 -0
- package/dist/cjs/Error/index.d.ts.map +1 -0
- package/dist/cjs/Error/index.js +14 -0
- package/dist/cjs/Messaging/index.d.ts +45 -0
- package/dist/cjs/Messaging/index.d.ts.map +1 -0
- package/dist/cjs/Messaging/index.js +784 -0
- package/dist/cjs/Profile/index.d.ts +9 -0
- package/dist/cjs/Profile/index.d.ts.map +1 -0
- package/dist/cjs/Profile/index.js +34 -0
- package/dist/cjs/Socket/index.d.ts +105 -0
- package/dist/cjs/Socket/index.d.ts.map +1 -0
- package/dist/cjs/Socket/index.js +875 -0
- package/dist/cjs/Types/index.d.ts +74 -0
- package/dist/cjs/Types/index.d.ts.map +1 -0
- package/dist/cjs/Types/index.js +2 -0
- package/dist/cjs/Types/profile.d.ts +5 -0
- package/dist/cjs/Types/profile.d.ts.map +1 -0
- package/dist/cjs/Types/profile.js +2 -0
- package/dist/cjs/Utils/create-delay.d.ts +17 -0
- package/dist/cjs/Utils/create-delay.d.ts.map +1 -0
- package/dist/cjs/Utils/create-delay.js +38 -0
- package/dist/cjs/Utils/error.d.ts +4 -0
- package/dist/cjs/Utils/error.d.ts.map +1 -0
- package/dist/cjs/Utils/error.js +8 -0
- package/dist/cjs/Utils/group-cache.d.ts +23 -0
- package/dist/cjs/Utils/group-cache.d.ts.map +1 -0
- package/dist/cjs/Utils/group-cache.js +176 -0
- package/dist/cjs/Utils/index.d.ts +6 -0
- package/dist/cjs/Utils/index.d.ts.map +1 -0
- package/dist/cjs/Utils/index.js +23 -0
- package/dist/cjs/Utils/is-exist.d.ts +6 -0
- package/dist/cjs/Utils/is-exist.d.ts.map +1 -0
- package/dist/cjs/Utils/is-exist.js +53 -0
- package/dist/cjs/Utils/lid-utils.d.ts +26 -0
- package/dist/cjs/Utils/lid-utils.d.ts.map +1 -0
- package/dist/cjs/Utils/lid-utils.js +81 -0
- package/dist/cjs/Utils/message-status.d.ts +4 -0
- package/dist/cjs/Utils/message-status.d.ts.map +1 -0
- package/dist/cjs/Utils/message-status.js +18 -0
- package/dist/cjs/Utils/mongo-auth-state.d.ts +15 -0
- package/dist/cjs/Utils/mongo-auth-state.d.ts.map +1 -0
- package/dist/cjs/Utils/mongo-auth-state.js +109 -0
- package/dist/cjs/Utils/phone-to-jid.d.ts +17 -0
- package/dist/cjs/Utils/phone-to-jid.d.ts.map +1 -0
- package/dist/cjs/Utils/phone-to-jid.js +51 -0
- package/dist/cjs/Utils/save-media.d.ts +6 -0
- package/dist/cjs/Utils/save-media.d.ts.map +1 -0
- package/dist/cjs/Utils/save-media.js +55 -0
- package/dist/cjs/index.d.ts +8 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +46 -0
- package/dist/index.d.ts +6 -6
- package/dist/index.js +7 -46
- package/package.json +57 -42
|
@@ -0,0 +1,784 @@
|
|
|
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
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
22
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.deleteMessage = exports.readMessage = exports.sendTyping = exports.sendSticker = exports.sendVoiceNote = exports.sendMedia = exports.sendTextMessageWithMention = exports.sendTextMessage = void 0;
|
|
27
|
+
const Defaults_1 = require("../Defaults");
|
|
28
|
+
const Socket_1 = require("../Socket");
|
|
29
|
+
const Utils_1 = require("../Utils");
|
|
30
|
+
const Utils_2 = require("../Utils");
|
|
31
|
+
const create_delay_1 = require("../Utils/create-delay");
|
|
32
|
+
const is_exist_1 = require("../Utils/is-exist");
|
|
33
|
+
const mime_1 = __importDefault(require("mime"));
|
|
34
|
+
const Error_1 = require("../Error");
|
|
35
|
+
const sendTextMessage = (_a) => __awaiter(void 0, void 0, void 0, function* () {
|
|
36
|
+
var { sessionId, to, text = "", isGroup = false } = _a, props = __rest(_a, ["sessionId", "to", "text", "isGroup"]);
|
|
37
|
+
const session = (0, Socket_1.getSession)(sessionId);
|
|
38
|
+
if (!session)
|
|
39
|
+
throw new Error_1.WhatsappError(Defaults_1.Messages.sessionNotFound(sessionId));
|
|
40
|
+
// Deteksi otomatis apakah ini adalah grup berdasarkan format JID
|
|
41
|
+
const toStr = to.toString();
|
|
42
|
+
const isGroupChat = toStr.includes('@g.us');
|
|
43
|
+
// Jika sudah dalam format grup, gunakan to langsung
|
|
44
|
+
// Jika tidak, gunakan phoneToJid untuk mengkonversi ke format yang benar
|
|
45
|
+
let jid;
|
|
46
|
+
if (isGroupChat) {
|
|
47
|
+
if (!toStr.endsWith('@g.us')) {
|
|
48
|
+
jid = toStr + '@g.us';
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
jid = toStr;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
jid = (0, Utils_1.phoneToJid)({ to, isGroup: isGroup || isGroupChat });
|
|
56
|
+
}
|
|
57
|
+
// Validasi JID untuk memastikan format yang didukung
|
|
58
|
+
if (!(0, Utils_2.isValidJid)(jid)) {
|
|
59
|
+
throw new Error_1.WhatsappError(`Invalid JID format: ${jid}. Supported formats: @s.whatsapp.net, @lid, @g.us`);
|
|
60
|
+
}
|
|
61
|
+
let retryCount = 0;
|
|
62
|
+
const maxRetries = 3;
|
|
63
|
+
const attemptSend = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
64
|
+
var _a, _b, _c;
|
|
65
|
+
try {
|
|
66
|
+
// Gunakan flag force untuk mencoba memaksa pengiriman pesan bahkan jika koneksi tidak stabil
|
|
67
|
+
const messageOptions = {
|
|
68
|
+
quoted: props.answering,
|
|
69
|
+
};
|
|
70
|
+
// Konfirmasi sekali lagi apakah ini grup berdasarkan format JID setelah konversi
|
|
71
|
+
const isDestinationGroup = jid.endsWith('@g.us');
|
|
72
|
+
// Gunakan waktu jeda untuk memastikan koneksi siap
|
|
73
|
+
yield (0, create_delay_1.createDelay)(500);
|
|
74
|
+
// Beri timeout yang lebih pendek untuk grup untuk mencegah hanging
|
|
75
|
+
const timeoutMs = isDestinationGroup ? 15000 : 30000;
|
|
76
|
+
// Gunakan withTimeout untuk membatasi waktu operasi
|
|
77
|
+
const result = yield session.sendMessage(jid, {
|
|
78
|
+
text: text,
|
|
79
|
+
}, messageOptions);
|
|
80
|
+
// Tambahkan method toJSON untuk compatibility dengan WebMessageInfo
|
|
81
|
+
result.toJSON = () => ({});
|
|
82
|
+
return yield (0, create_delay_1.withTimeout)(Promise.resolve(result), timeoutMs, `Pengiriman pesan ke ${jid} melebihi batas waktu ${timeoutMs / 1000} detik`);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
console.error(`Error saat mengirim pesan (percobaan ${retryCount + 1}/${maxRetries}): ${(error === null || error === void 0 ? void 0 : error.message) || 'Unknown error'}`);
|
|
86
|
+
// Jika error terkait MessageCounterError (grup dengan channel pengumuman)
|
|
87
|
+
if (((_a = error === null || error === void 0 ? void 0 : error.message) === null || _a === void 0 ? void 0 : _a.includes('MessageCounterError')) || ((_b = error === null || error === void 0 ? void 0 : error.stack) === null || _b === void 0 ? void 0 : _b.includes('MessageCounterError'))) {
|
|
88
|
+
console.warn('Terdeteksi MessageCounterError, kemungkinan grup dengan channel pengumuman');
|
|
89
|
+
if (retryCount < maxRetries) {
|
|
90
|
+
retryCount++;
|
|
91
|
+
// Tambahkan delay lebih lama antara percobaan
|
|
92
|
+
yield (0, create_delay_1.createDelay)(2000 * retryCount);
|
|
93
|
+
return attemptSend();
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
console.error('Batas maksimum percobaan ulang tercapai');
|
|
97
|
+
throw new Error_1.WhatsappError(`Gagal mengirim pesan ke grup dengan channel pengumuman setelah ${maxRetries} kali percobaan`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// Jika koneksi tertutup, coba reconnect
|
|
101
|
+
if ((_c = error === null || error === void 0 ? void 0 : error.message) === null || _c === void 0 ? void 0 : _c.includes('Connection Closed')) {
|
|
102
|
+
try {
|
|
103
|
+
yield (0, Socket_1.reconnect)(sessionId);
|
|
104
|
+
yield (0, create_delay_1.createDelay)(3000); // Beri waktu untuk reconnect sepenuhnya
|
|
105
|
+
// Coba kirim lagi setelah reconnect
|
|
106
|
+
const reconnectResult = yield session.sendMessage(jid, {
|
|
107
|
+
text: text,
|
|
108
|
+
}, {
|
|
109
|
+
quoted: props.answering,
|
|
110
|
+
});
|
|
111
|
+
// Tambahkan method toJSON untuk compatibility dengan WebMessageInfo
|
|
112
|
+
reconnectResult.toJSON = () => ({});
|
|
113
|
+
return yield (0, create_delay_1.withTimeout)(Promise.resolve(reconnectResult), 30000, `Pengiriman pesan setelah reconnect melebihi batas waktu`);
|
|
114
|
+
}
|
|
115
|
+
catch (reconnectError) {
|
|
116
|
+
console.error(`Gagal reconnect: ${(reconnectError === null || reconnectError === void 0 ? void 0 : reconnectError.message) || 'Unknown error'}`);
|
|
117
|
+
throw new Error_1.WhatsappError(`Koneksi terputus dan gagal reconnect: ${(reconnectError === null || reconnectError === void 0 ? void 0 : reconnectError.message) || 'Unknown error'}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Jika ada error timeout atau error lain, coba lagi sekali dengan delay lebih lama
|
|
121
|
+
try {
|
|
122
|
+
yield (0, create_delay_1.createDelay)(2000);
|
|
123
|
+
// Untuk percobaan kedua, gunakan timeout yang lebih lama
|
|
124
|
+
const retryResult = yield session.sendMessage(jid, {
|
|
125
|
+
text: text,
|
|
126
|
+
}, {
|
|
127
|
+
quoted: props.answering,
|
|
128
|
+
});
|
|
129
|
+
// Tambahkan method toJSON untuk compatibility dengan WebMessageInfo
|
|
130
|
+
retryResult.toJSON = () => ({});
|
|
131
|
+
return yield (0, create_delay_1.withTimeout)(Promise.resolve(retryResult), 20000, `Percobaan kedua pengiriman pesan melebihi batas waktu`);
|
|
132
|
+
}
|
|
133
|
+
catch (retryError) {
|
|
134
|
+
console.error(`Gagal pada percobaan kedua: ${(retryError === null || retryError === void 0 ? void 0 : retryError.message) || 'Unknown error'}`);
|
|
135
|
+
throw new Error_1.WhatsappError(`Gagal mengirim pesan: ${(retryError === null || retryError === void 0 ? void 0 : retryError.message) || 'Unknown error'}`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return attemptSend();
|
|
140
|
+
});
|
|
141
|
+
exports.sendTextMessage = sendTextMessage;
|
|
142
|
+
/**
|
|
143
|
+
* Kirim pesan teks dengan mention
|
|
144
|
+
*
|
|
145
|
+
* @param sessionId - ID sesi WhatsApp
|
|
146
|
+
* @param to - Nomor telepon atau JID tujuan
|
|
147
|
+
* @param text - Teks pesan (bisa mengandung @nomor untuk mention)
|
|
148
|
+
* @param mentions - Array JID yang akan di-mention
|
|
149
|
+
* @param isGroup - Apakah ini adalah grup
|
|
150
|
+
* @param props - Properti tambahan seperti answering
|
|
151
|
+
*/
|
|
152
|
+
const sendTextMessageWithMention = (_a) => __awaiter(void 0, void 0, void 0, function* () {
|
|
153
|
+
var { sessionId, to, text = "", mentions = [], isGroup = false } = _a, props = __rest(_a, ["sessionId", "to", "text", "mentions", "isGroup"]);
|
|
154
|
+
const session = (0, Socket_1.getSession)(sessionId);
|
|
155
|
+
if (!session)
|
|
156
|
+
throw new Error_1.WhatsappError(Defaults_1.Messages.sessionNotFound(sessionId));
|
|
157
|
+
// Deteksi otomatis apakah ini adalah grup berdasarkan format JID
|
|
158
|
+
const toStr = to.toString();
|
|
159
|
+
const isGroupChat = toStr.includes('@g.us');
|
|
160
|
+
// Jika sudah dalam format grup, gunakan to langsung
|
|
161
|
+
// Jika tidak, gunakan phoneToJid untuk mengkonversi ke format yang benar
|
|
162
|
+
let jid;
|
|
163
|
+
if (isGroupChat) {
|
|
164
|
+
if (!toStr.endsWith('@g.us')) {
|
|
165
|
+
jid = toStr + '@g.us';
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
jid = toStr;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
jid = (0, Utils_1.phoneToJid)({ to, isGroup: isGroup || isGroupChat });
|
|
173
|
+
}
|
|
174
|
+
// Validasi JID untuk memastikan format yang didukung
|
|
175
|
+
if (!(0, Utils_2.isValidJid)(jid)) {
|
|
176
|
+
throw new Error_1.WhatsappError(`Invalid JID format: ${jid}. Supported formats: @s.whatsapp.net, @lid, @g.us`);
|
|
177
|
+
}
|
|
178
|
+
let retryCount = 0;
|
|
179
|
+
const maxRetries = 3;
|
|
180
|
+
const attemptSend = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
181
|
+
var _a, _b, _c;
|
|
182
|
+
try {
|
|
183
|
+
// Gunakan flag force untuk mencoba memaksa pengiriman pesan bahkan jika koneksi tidak stabil
|
|
184
|
+
const messageOptions = {
|
|
185
|
+
quoted: props.answering,
|
|
186
|
+
};
|
|
187
|
+
// Konfirmasi sekali lagi apakah ini grup berdasarkan format JID setelah konversi
|
|
188
|
+
const isDestinationGroup = jid.endsWith('@g.us');
|
|
189
|
+
// Gunakan waktu jeda untuk memastikan koneksi siap
|
|
190
|
+
yield (0, create_delay_1.createDelay)(500);
|
|
191
|
+
// Beri timeout yang lebih pendek untuk grup untuk mencegah hanging
|
|
192
|
+
const timeoutMs = isDestinationGroup ? 15000 : 30000;
|
|
193
|
+
// Persiapkan pesan dengan mention jika ada
|
|
194
|
+
const messageContent = {
|
|
195
|
+
text: text,
|
|
196
|
+
};
|
|
197
|
+
// Tambahkan mentions jika ada
|
|
198
|
+
if (mentions && mentions.length > 0) {
|
|
199
|
+
messageContent.mentions = mentions;
|
|
200
|
+
}
|
|
201
|
+
// Gunakan withTimeout untuk membatasi waktu operasi
|
|
202
|
+
const result = yield session.sendMessage(jid, messageContent, messageOptions);
|
|
203
|
+
// Tambahkan method toJSON untuk compatibility dengan WebMessageInfo
|
|
204
|
+
result.toJSON = () => ({});
|
|
205
|
+
return yield (0, create_delay_1.withTimeout)(Promise.resolve(result), timeoutMs, `Pengiriman pesan dengan mention ke ${jid} melebihi batas waktu ${timeoutMs / 1000} detik`);
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
console.error(`Error saat mengirim pesan dengan mention (percobaan ${retryCount + 1}/${maxRetries}): ${(error === null || error === void 0 ? void 0 : error.message) || 'Unknown error'}`);
|
|
209
|
+
// Jika error terkait MessageCounterError (grup dengan channel pengumuman)
|
|
210
|
+
if (((_a = error === null || error === void 0 ? void 0 : error.message) === null || _a === void 0 ? void 0 : _a.includes('MessageCounterError')) || ((_b = error === null || error === void 0 ? void 0 : error.stack) === null || _b === void 0 ? void 0 : _b.includes('MessageCounterError'))) {
|
|
211
|
+
console.warn('Terdeteksi MessageCounterError, kemungkinan grup dengan channel pengumuman');
|
|
212
|
+
if (retryCount < maxRetries) {
|
|
213
|
+
retryCount++;
|
|
214
|
+
// Tambahkan delay lebih lama antara percobaan
|
|
215
|
+
yield (0, create_delay_1.createDelay)(2000 * retryCount);
|
|
216
|
+
return attemptSend();
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
console.error('Batas maksimum percobaan ulang tercapai');
|
|
220
|
+
throw new Error_1.WhatsappError(`Gagal mengirim pesan dengan mention ke grup dengan channel pengumuman setelah ${maxRetries} kali percobaan`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
// Jika koneksi tertutup, coba reconnect
|
|
224
|
+
if ((_c = error === null || error === void 0 ? void 0 : error.message) === null || _c === void 0 ? void 0 : _c.includes('Connection Closed')) {
|
|
225
|
+
try {
|
|
226
|
+
yield (0, Socket_1.reconnect)(sessionId);
|
|
227
|
+
yield (0, create_delay_1.createDelay)(3000); // Beri waktu untuk reconnect sepenuhnya
|
|
228
|
+
// Persiapkan pesan dengan mention untuk reconnect
|
|
229
|
+
const messageContent = {
|
|
230
|
+
text: text,
|
|
231
|
+
};
|
|
232
|
+
if (mentions && mentions.length > 0) {
|
|
233
|
+
messageContent.mentions = mentions;
|
|
234
|
+
}
|
|
235
|
+
// Coba kirim lagi setelah reconnect
|
|
236
|
+
const reconnectResult = yield session.sendMessage(jid, messageContent, {
|
|
237
|
+
quoted: props.answering,
|
|
238
|
+
});
|
|
239
|
+
// Tambahkan method toJSON untuk compatibility dengan WebMessageInfo
|
|
240
|
+
reconnectResult.toJSON = () => ({});
|
|
241
|
+
return yield (0, create_delay_1.withTimeout)(Promise.resolve(reconnectResult), 30000, `Pengiriman pesan dengan mention setelah reconnect melebihi batas waktu`);
|
|
242
|
+
}
|
|
243
|
+
catch (reconnectError) {
|
|
244
|
+
console.error(`Gagal reconnect: ${(reconnectError === null || reconnectError === void 0 ? void 0 : reconnectError.message) || 'Unknown error'}`);
|
|
245
|
+
throw new Error_1.WhatsappError(`Koneksi terputus dan gagal reconnect: ${(reconnectError === null || reconnectError === void 0 ? void 0 : reconnectError.message) || 'Unknown error'}`);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// Jika ada error timeout atau error lain, coba lagi sekali dengan delay lebih lama
|
|
249
|
+
try {
|
|
250
|
+
yield (0, create_delay_1.createDelay)(2000);
|
|
251
|
+
// Persiapkan pesan dengan mention untuk percobaan kedua
|
|
252
|
+
const messageContent = {
|
|
253
|
+
text: text,
|
|
254
|
+
};
|
|
255
|
+
if (mentions && mentions.length > 0) {
|
|
256
|
+
messageContent.mentions = mentions;
|
|
257
|
+
}
|
|
258
|
+
// Untuk percobaan kedua, gunakan timeout yang lebih lama
|
|
259
|
+
const retryResult = yield session.sendMessage(jid, messageContent, {
|
|
260
|
+
quoted: props.answering,
|
|
261
|
+
});
|
|
262
|
+
// Tambahkan method toJSON untuk compatibility dengan WebMessageInfo
|
|
263
|
+
retryResult.toJSON = () => ({});
|
|
264
|
+
return yield (0, create_delay_1.withTimeout)(Promise.resolve(retryResult), 20000, `Percobaan kedua pengiriman pesan dengan mention melebihi batas waktu`);
|
|
265
|
+
}
|
|
266
|
+
catch (retryError) {
|
|
267
|
+
console.error(`Gagal pada percobaan kedua: ${(retryError === null || retryError === void 0 ? void 0 : retryError.message) || 'Unknown error'}`);
|
|
268
|
+
throw new Error_1.WhatsappError(`Gagal mengirim pesan dengan mention: ${(retryError === null || retryError === void 0 ? void 0 : retryError.message) || 'Unknown error'}`);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
return attemptSend();
|
|
273
|
+
});
|
|
274
|
+
exports.sendTextMessageWithMention = sendTextMessageWithMention;
|
|
275
|
+
const sendMedia = (_a) => __awaiter(void 0, [_a], void 0, function* ({ sessionId, to, media, type, caption = "", fileName, isGroup = false, answering, }) {
|
|
276
|
+
const session = (0, Socket_1.getSession)(sessionId);
|
|
277
|
+
if (!session)
|
|
278
|
+
throw new Error_1.WhatsappError(Defaults_1.Messages.sessionNotFound(sessionId));
|
|
279
|
+
// Deteksi otomatis apakah ini adalah grup berdasarkan format JID
|
|
280
|
+
const toStr = to.toString();
|
|
281
|
+
const isGroupChat = toStr.includes('@g.us');
|
|
282
|
+
// Jika sudah dalam format grup, gunakan to langsung
|
|
283
|
+
// Jika tidak, gunakan phoneToJid untuk mengkonversi ke format yang benar
|
|
284
|
+
let jid;
|
|
285
|
+
if (isGroupChat) {
|
|
286
|
+
if (!toStr.endsWith('@g.us')) {
|
|
287
|
+
jid = toStr + '@g.us';
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
jid = toStr;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
jid = (0, Utils_1.phoneToJid)({ to, isGroup: isGroup || isGroupChat });
|
|
295
|
+
}
|
|
296
|
+
// Validasi JID untuk memastikan format yang didukung
|
|
297
|
+
if (!(0, Utils_2.isValidJid)(jid)) {
|
|
298
|
+
throw new Error_1.WhatsappError(`Invalid JID format: ${jid}. Supported formats: @s.whatsapp.net, @lid, @g.us`);
|
|
299
|
+
}
|
|
300
|
+
let retryCount = 0;
|
|
301
|
+
const maxRetries = 3;
|
|
302
|
+
// Helper function untuk mendapatkan message content berdasarkan tipe
|
|
303
|
+
function getMessageContent() {
|
|
304
|
+
const processedMedia = (0, is_exist_1.isExist)(media);
|
|
305
|
+
const content = {};
|
|
306
|
+
const fileMimeType = mime_1.default.getType(typeof media === 'string' ? media : (fileName || '')) || 'application/octet-stream';
|
|
307
|
+
switch (type) {
|
|
308
|
+
case "image":
|
|
309
|
+
content.image = processedMedia;
|
|
310
|
+
content.caption = caption;
|
|
311
|
+
if (fileName)
|
|
312
|
+
content.fileName = fileName;
|
|
313
|
+
break;
|
|
314
|
+
case "video":
|
|
315
|
+
content.video = processedMedia;
|
|
316
|
+
content.caption = caption;
|
|
317
|
+
if (fileName)
|
|
318
|
+
content.fileName = fileName;
|
|
319
|
+
break;
|
|
320
|
+
case "audio":
|
|
321
|
+
content.audio = processedMedia;
|
|
322
|
+
content.mimetype = fileMimeType;
|
|
323
|
+
if (fileName)
|
|
324
|
+
content.fileName = fileName;
|
|
325
|
+
break;
|
|
326
|
+
default:
|
|
327
|
+
content.document = processedMedia;
|
|
328
|
+
content.mimetype = fileMimeType;
|
|
329
|
+
content.caption = caption;
|
|
330
|
+
if (fileName)
|
|
331
|
+
content.fileName = fileName;
|
|
332
|
+
}
|
|
333
|
+
return content;
|
|
334
|
+
}
|
|
335
|
+
const attemptSend = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
336
|
+
var _a, _b, _c;
|
|
337
|
+
try {
|
|
338
|
+
const mimeType = mime_1.default.getType(typeof media === 'string' ? media : (fileName || '')) || 'application/octet-stream';
|
|
339
|
+
if (!mimeType && type !== 'image' && type !== 'video')
|
|
340
|
+
throw new Error("Cannot get file mimetype!");
|
|
341
|
+
const messageContent = {};
|
|
342
|
+
const options = answering ? { quoted: answering } : {};
|
|
343
|
+
// Konfirmasi sekali lagi apakah ini grup berdasarkan format JID setelah konversi
|
|
344
|
+
const isDestinationGroup = jid.endsWith('@g.us');
|
|
345
|
+
// Tunda sejenak untuk memastikan koneksi stabil
|
|
346
|
+
yield (0, create_delay_1.createDelay)(500);
|
|
347
|
+
// Persiapkan media dengan isExist
|
|
348
|
+
const processedMedia = (0, is_exist_1.isExist)(media);
|
|
349
|
+
switch (type) {
|
|
350
|
+
case "image":
|
|
351
|
+
messageContent.image = processedMedia;
|
|
352
|
+
messageContent.caption = caption;
|
|
353
|
+
if (fileName)
|
|
354
|
+
messageContent.fileName = fileName;
|
|
355
|
+
break;
|
|
356
|
+
case "video":
|
|
357
|
+
messageContent.video = processedMedia;
|
|
358
|
+
messageContent.caption = caption;
|
|
359
|
+
if (fileName)
|
|
360
|
+
messageContent.fileName = fileName;
|
|
361
|
+
break;
|
|
362
|
+
case "audio":
|
|
363
|
+
messageContent.audio = processedMedia;
|
|
364
|
+
messageContent.mimetype = mimeType;
|
|
365
|
+
if (fileName)
|
|
366
|
+
messageContent.fileName = fileName;
|
|
367
|
+
break;
|
|
368
|
+
case "pdf":
|
|
369
|
+
case "xls":
|
|
370
|
+
case "xlsx":
|
|
371
|
+
case "doc":
|
|
372
|
+
case "docx":
|
|
373
|
+
case "zip":
|
|
374
|
+
case "mp3":
|
|
375
|
+
messageContent.document = processedMedia;
|
|
376
|
+
messageContent.mimetype = mimeType;
|
|
377
|
+
messageContent.caption = caption;
|
|
378
|
+
if (fileName)
|
|
379
|
+
messageContent.fileName = fileName;
|
|
380
|
+
break;
|
|
381
|
+
default:
|
|
382
|
+
throw new Error("Media type not found! (image, video, audio, pdf)");
|
|
383
|
+
}
|
|
384
|
+
// Timeout lebih lama untuk media karena ukuran file
|
|
385
|
+
const timeoutMs = isDestinationGroup ? 60000 : 120000;
|
|
386
|
+
const result = yield session.sendMessage(jid, messageContent, options);
|
|
387
|
+
// Tambahkan method toJSON untuk compatibility dengan WebMessageInfo
|
|
388
|
+
result.toJSON = () => ({});
|
|
389
|
+
return yield (0, create_delay_1.withTimeout)(Promise.resolve(result), timeoutMs, `Pengiriman media melebihi batas waktu ${timeoutMs / 1000} detik`);
|
|
390
|
+
}
|
|
391
|
+
catch (error) {
|
|
392
|
+
console.error(`Error saat mengirim media (percobaan ${retryCount + 1}/${maxRetries}): ${(error === null || error === void 0 ? void 0 : error.message) || 'Unknown error'}`);
|
|
393
|
+
// Jika error terkait MessageCounterError (grup dengan channel pengumuman)
|
|
394
|
+
if (((_a = error === null || error === void 0 ? void 0 : error.message) === null || _a === void 0 ? void 0 : _a.includes('MessageCounterError')) || ((_b = error === null || error === void 0 ? void 0 : error.stack) === null || _b === void 0 ? void 0 : _b.includes('MessageCounterError'))) {
|
|
395
|
+
console.warn('Terdeteksi MessageCounterError, kemungkinan grup dengan channel pengumuman');
|
|
396
|
+
if (retryCount < maxRetries) {
|
|
397
|
+
retryCount++;
|
|
398
|
+
// Tambahkan delay lebih lama antara percobaan
|
|
399
|
+
yield (0, create_delay_1.createDelay)(2000 * retryCount);
|
|
400
|
+
return attemptSend();
|
|
401
|
+
}
|
|
402
|
+
else {
|
|
403
|
+
console.error('Batas maksimum percobaan ulang tercapai');
|
|
404
|
+
throw new Error_1.WhatsappError(`Gagal mengirim media ke grup dengan channel pengumuman setelah ${maxRetries} kali percobaan`);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
// Jika koneksi tertutup, coba reconnect
|
|
408
|
+
if ((_c = error === null || error === void 0 ? void 0 : error.message) === null || _c === void 0 ? void 0 : _c.includes('Connection Closed')) {
|
|
409
|
+
try {
|
|
410
|
+
yield (0, Socket_1.reconnect)(sessionId);
|
|
411
|
+
yield (0, create_delay_1.createDelay)(3000); // Beri waktu untuk reconnect sepenuhnya
|
|
412
|
+
// Siapkan content media untuk percobaan ulang
|
|
413
|
+
const retryContent = getMessageContent();
|
|
414
|
+
// Coba kirim lagi setelah reconnect
|
|
415
|
+
const reconnectOptions = answering ? { quoted: answering } : {};
|
|
416
|
+
const reconnectResult = yield session.sendMessage(jid, retryContent, reconnectOptions);
|
|
417
|
+
// Tambahkan method toJSON untuk compatibility dengan WebMessageInfo
|
|
418
|
+
reconnectResult.toJSON = () => ({});
|
|
419
|
+
return yield (0, create_delay_1.withTimeout)(Promise.resolve(reconnectResult), 180000, // 3 menit untuk percobaan setelah reconnect
|
|
420
|
+
`Pengiriman media setelah reconnect melebihi batas waktu`);
|
|
421
|
+
}
|
|
422
|
+
catch (reconnectError) {
|
|
423
|
+
console.error(`Gagal reconnect: ${(reconnectError === null || reconnectError === void 0 ? void 0 : reconnectError.message) || 'Unknown error'}`);
|
|
424
|
+
throw new Error_1.WhatsappError(`Koneksi terputus dan gagal reconnect: ${(reconnectError === null || reconnectError === void 0 ? void 0 : reconnectError.message) || 'Unknown error'}`);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
// Untuk error timeout atau error lain, coba lagi dengan timeout lebih lama
|
|
428
|
+
try {
|
|
429
|
+
yield (0, create_delay_1.createDelay)(3000);
|
|
430
|
+
// Siapkan content media untuk percobaan ulang
|
|
431
|
+
const retryContent = getMessageContent();
|
|
432
|
+
// Untuk percobaan kedua, gunakan timeout yang lebih lama
|
|
433
|
+
const retryOptions = answering ? { quoted: answering } : {};
|
|
434
|
+
const retryResult = yield session.sendMessage(jid, retryContent, retryOptions);
|
|
435
|
+
// Tambahkan method toJSON untuk compatibility dengan WebMessageInfo
|
|
436
|
+
retryResult.toJSON = () => ({});
|
|
437
|
+
return yield (0, create_delay_1.withTimeout)(Promise.resolve(retryResult), 180000, // 3 menit untuk percobaan kedua
|
|
438
|
+
`Percobaan kedua pengiriman media melebihi batas waktu`);
|
|
439
|
+
}
|
|
440
|
+
catch (retryError) {
|
|
441
|
+
console.error(`Gagal pada percobaan kedua: ${(retryError === null || retryError === void 0 ? void 0 : retryError.message) || 'Unknown error'}`);
|
|
442
|
+
throw new Error_1.WhatsappError(`Gagal mengirim media: ${(retryError === null || retryError === void 0 ? void 0 : retryError.message) || 'Unknown error'}`);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
return attemptSend();
|
|
447
|
+
});
|
|
448
|
+
exports.sendMedia = sendMedia;
|
|
449
|
+
const sendVoiceNote = (_a) => __awaiter(void 0, void 0, void 0, function* () {
|
|
450
|
+
var { sessionId, to, isGroup = false, media } = _a, props = __rest(_a, ["sessionId", "to", "isGroup", "media"]);
|
|
451
|
+
const session = (0, Socket_1.getSession)(sessionId);
|
|
452
|
+
if (!session)
|
|
453
|
+
throw new Error_1.WhatsappError(Defaults_1.Messages.sessionNotFound(sessionId));
|
|
454
|
+
// Deteksi otomatis apakah ini adalah grup berdasarkan format JID
|
|
455
|
+
const toStr = to.toString();
|
|
456
|
+
const isGroupChat = toStr.includes('@g.us');
|
|
457
|
+
// Jika sudah dalam format grup, gunakan to langsung
|
|
458
|
+
// Jika tidak, gunakan phoneToJid untuk mengkonversi ke format yang benar
|
|
459
|
+
let jid;
|
|
460
|
+
if (isGroupChat) {
|
|
461
|
+
if (!toStr.endsWith('@g.us')) {
|
|
462
|
+
jid = toStr + '@g.us';
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
jid = toStr;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
jid = (0, Utils_1.phoneToJid)({ to, isGroup: isGroup || isGroupChat });
|
|
470
|
+
}
|
|
471
|
+
// Validasi JID untuk memastikan format yang didukung
|
|
472
|
+
if (!(0, Utils_2.isValidJid)(jid)) {
|
|
473
|
+
throw new Error_1.WhatsappError(`Invalid JID format: ${jid}. Supported formats: @s.whatsapp.net, @lid, @g.us`);
|
|
474
|
+
}
|
|
475
|
+
if (!media) {
|
|
476
|
+
throw new Error_1.WhatsappError(`Invalid Media`);
|
|
477
|
+
}
|
|
478
|
+
let retryCount = 0;
|
|
479
|
+
const maxRetries = 3;
|
|
480
|
+
const attemptSend = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
481
|
+
var _a, _b, _c;
|
|
482
|
+
try {
|
|
483
|
+
// Gunakan timeout untuk mencegah hanging
|
|
484
|
+
const voiceOptions = {
|
|
485
|
+
quoted: props.answering,
|
|
486
|
+
};
|
|
487
|
+
const result = yield session.sendMessage(jid, {
|
|
488
|
+
audio: typeof media == "string"
|
|
489
|
+
? {
|
|
490
|
+
url: media,
|
|
491
|
+
}
|
|
492
|
+
: media,
|
|
493
|
+
ptt: true,
|
|
494
|
+
}, voiceOptions);
|
|
495
|
+
// Tambahkan method toJSON untuk compatibility dengan WebMessageInfo
|
|
496
|
+
result.toJSON = () => ({});
|
|
497
|
+
return yield (0, create_delay_1.withTimeout)(Promise.resolve(result), 60000, `Pengiriman voice note melebihi batas waktu`);
|
|
498
|
+
}
|
|
499
|
+
catch (error) {
|
|
500
|
+
console.error(`Error saat mengirim voice note (percobaan ${retryCount + 1}/${maxRetries}): ${(error === null || error === void 0 ? void 0 : error.message) || 'Unknown error'}`);
|
|
501
|
+
// Jika error terkait MessageCounterError (grup dengan channel pengumuman)
|
|
502
|
+
if (((_a = error === null || error === void 0 ? void 0 : error.message) === null || _a === void 0 ? void 0 : _a.includes('MessageCounterError')) || ((_b = error === null || error === void 0 ? void 0 : error.stack) === null || _b === void 0 ? void 0 : _b.includes('MessageCounterError'))) {
|
|
503
|
+
console.warn('Terdeteksi MessageCounterError, kemungkinan grup dengan channel pengumuman');
|
|
504
|
+
if (retryCount < maxRetries) {
|
|
505
|
+
retryCount++;
|
|
506
|
+
// Tambahkan delay lebih lama antara percobaan
|
|
507
|
+
yield (0, create_delay_1.createDelay)(2000 * retryCount);
|
|
508
|
+
return attemptSend();
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
console.error('Batas maksimum percobaan ulang tercapai');
|
|
512
|
+
throw new Error_1.WhatsappError(`Gagal mengirim voice note ke grup dengan channel pengumuman setelah ${maxRetries} kali percobaan`);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
// Jika koneksi tertutup, coba reconnect
|
|
516
|
+
if ((_c = error === null || error === void 0 ? void 0 : error.message) === null || _c === void 0 ? void 0 : _c.includes('Connection Closed')) {
|
|
517
|
+
try {
|
|
518
|
+
yield (0, Socket_1.reconnect)(sessionId);
|
|
519
|
+
yield (0, create_delay_1.createDelay)(3000); // Beri waktu untuk reconnect sepenuhnya
|
|
520
|
+
// Coba kirim lagi setelah reconnect
|
|
521
|
+
const reconnectVoiceOptions = {
|
|
522
|
+
quoted: props.answering,
|
|
523
|
+
};
|
|
524
|
+
const reconnectResult = yield session.sendMessage(jid, {
|
|
525
|
+
audio: typeof media == "string"
|
|
526
|
+
? {
|
|
527
|
+
url: media,
|
|
528
|
+
}
|
|
529
|
+
: media,
|
|
530
|
+
ptt: true,
|
|
531
|
+
}, reconnectVoiceOptions);
|
|
532
|
+
// Tambahkan method toJSON untuk compatibility dengan WebMessageInfo
|
|
533
|
+
reconnectResult.toJSON = () => ({});
|
|
534
|
+
return yield (0, create_delay_1.withTimeout)(Promise.resolve(reconnectResult), 60000, `Pengiriman voice note setelah reconnect melebihi batas waktu`);
|
|
535
|
+
}
|
|
536
|
+
catch (reconnectError) {
|
|
537
|
+
console.error(`Gagal reconnect: ${(reconnectError === null || reconnectError === void 0 ? void 0 : reconnectError.message) || 'Unknown error'}`);
|
|
538
|
+
throw new Error_1.WhatsappError(`Koneksi terputus dan gagal reconnect: ${(reconnectError === null || reconnectError === void 0 ? void 0 : reconnectError.message) || 'Unknown error'}`);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
// Re-throw error jika bukan masalah koneksi atau reconnect gagal
|
|
542
|
+
throw new Error_1.WhatsappError(`Gagal mengirim voice note: ${(error === null || error === void 0 ? void 0 : error.message) || 'Unknown error'}`);
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
return attemptSend();
|
|
546
|
+
});
|
|
547
|
+
exports.sendVoiceNote = sendVoiceNote;
|
|
548
|
+
const sendSticker = (_a) => __awaiter(void 0, void 0, void 0, function* () {
|
|
549
|
+
var { sessionId, to, isGroup, media } = _a, props = __rest(_a, ["sessionId", "to", "isGroup", "media"]);
|
|
550
|
+
const session = (0, Socket_1.getSession)(sessionId);
|
|
551
|
+
if (!session)
|
|
552
|
+
throw new Error_1.WhatsappError(Defaults_1.Messages.sessionNotFound(sessionId));
|
|
553
|
+
// Deteksi otomatis apakah ini adalah grup berdasarkan format JID
|
|
554
|
+
const toStr = to.toString();
|
|
555
|
+
const isGroupChat = toStr.includes('@g.us');
|
|
556
|
+
// Jika sudah dalam format grup, gunakan to langsung
|
|
557
|
+
// Jika tidak, gunakan phoneToJid untuk mengkonversi ke format yang benar
|
|
558
|
+
let jid;
|
|
559
|
+
if (isGroupChat) {
|
|
560
|
+
if (!toStr.endsWith('@g.us')) {
|
|
561
|
+
jid = toStr + '@g.us';
|
|
562
|
+
}
|
|
563
|
+
else {
|
|
564
|
+
jid = toStr;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
else {
|
|
568
|
+
jid = (0, Utils_1.phoneToJid)({ to, isGroup: isGroup || isGroupChat });
|
|
569
|
+
}
|
|
570
|
+
// Validasi JID untuk memastikan format yang didukung
|
|
571
|
+
if (!(0, Utils_2.isValidJid)(jid)) {
|
|
572
|
+
throw new Error_1.WhatsappError(`Invalid JID format: ${jid}. Supported formats: @s.whatsapp.net, @lid, @g.us`);
|
|
573
|
+
}
|
|
574
|
+
if (!media) {
|
|
575
|
+
throw new Error_1.WhatsappError(`Invalid Media`);
|
|
576
|
+
}
|
|
577
|
+
let retryCount = 0;
|
|
578
|
+
const maxRetries = 3;
|
|
579
|
+
const attemptSend = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
580
|
+
var _a, _b, _c;
|
|
581
|
+
try {
|
|
582
|
+
// Gunakan timeout untuk mencegah hanging
|
|
583
|
+
const stickerOptions = {
|
|
584
|
+
quoted: props.answering,
|
|
585
|
+
};
|
|
586
|
+
const result = yield session.sendMessage(jid, {
|
|
587
|
+
sticker: typeof media == "string"
|
|
588
|
+
? {
|
|
589
|
+
url: media,
|
|
590
|
+
}
|
|
591
|
+
: media,
|
|
592
|
+
}, stickerOptions);
|
|
593
|
+
// Tambahkan method toJSON untuk compatibility dengan WebMessageInfo
|
|
594
|
+
result.toJSON = () => ({});
|
|
595
|
+
return yield (0, create_delay_1.withTimeout)(Promise.resolve(result), 60000, `Pengiriman sticker melebihi batas waktu`);
|
|
596
|
+
}
|
|
597
|
+
catch (error) {
|
|
598
|
+
console.error(`Error saat mengirim sticker (percobaan ${retryCount + 1}/${maxRetries}): ${(error === null || error === void 0 ? void 0 : error.message) || 'Unknown error'}`);
|
|
599
|
+
// Jika error terkait MessageCounterError (grup dengan channel pengumuman)
|
|
600
|
+
if (((_a = error === null || error === void 0 ? void 0 : error.message) === null || _a === void 0 ? void 0 : _a.includes('MessageCounterError')) || ((_b = error === null || error === void 0 ? void 0 : error.stack) === null || _b === void 0 ? void 0 : _b.includes('MessageCounterError'))) {
|
|
601
|
+
console.warn('Terdeteksi MessageCounterError, kemungkinan grup dengan channel pengumuman');
|
|
602
|
+
if (retryCount < maxRetries) {
|
|
603
|
+
retryCount++;
|
|
604
|
+
// Tambahkan delay lebih lama antara percobaan
|
|
605
|
+
yield (0, create_delay_1.createDelay)(2000 * retryCount);
|
|
606
|
+
return attemptSend();
|
|
607
|
+
}
|
|
608
|
+
else {
|
|
609
|
+
console.error('Batas maksimum percobaan ulang tercapai');
|
|
610
|
+
throw new Error_1.WhatsappError(`Gagal mengirim sticker ke grup dengan channel pengumuman setelah ${maxRetries} kali percobaan`);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
// Jika koneksi tertutup, coba reconnect
|
|
614
|
+
if ((_c = error === null || error === void 0 ? void 0 : error.message) === null || _c === void 0 ? void 0 : _c.includes('Connection Closed')) {
|
|
615
|
+
try {
|
|
616
|
+
yield (0, Socket_1.reconnect)(sessionId);
|
|
617
|
+
yield (0, create_delay_1.createDelay)(3000); // Beri waktu untuk reconnect sepenuhnya
|
|
618
|
+
// Coba kirim lagi setelah reconnect
|
|
619
|
+
const reconnectStickerOptions = {
|
|
620
|
+
quoted: props.answering,
|
|
621
|
+
};
|
|
622
|
+
const reconnectResult = yield session.sendMessage(jid, {
|
|
623
|
+
sticker: typeof media == "string"
|
|
624
|
+
? {
|
|
625
|
+
url: media,
|
|
626
|
+
}
|
|
627
|
+
: media,
|
|
628
|
+
}, reconnectStickerOptions);
|
|
629
|
+
// Tambahkan method toJSON untuk compatibility dengan WebMessageInfo
|
|
630
|
+
reconnectResult.toJSON = () => ({});
|
|
631
|
+
return yield (0, create_delay_1.withTimeout)(Promise.resolve(reconnectResult), 60000, `Pengiriman sticker setelah reconnect melebihi batas waktu`);
|
|
632
|
+
}
|
|
633
|
+
catch (reconnectError) {
|
|
634
|
+
console.error(`Gagal reconnect: ${(reconnectError === null || reconnectError === void 0 ? void 0 : reconnectError.message) || 'Unknown error'}`);
|
|
635
|
+
throw new Error_1.WhatsappError(`Koneksi terputus dan gagal reconnect: ${(reconnectError === null || reconnectError === void 0 ? void 0 : reconnectError.message) || 'Unknown error'}`);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
// Re-throw error jika bukan masalah koneksi atau reconnect gagal
|
|
639
|
+
throw new Error_1.WhatsappError(`Gagal mengirim sticker: ${(error === null || error === void 0 ? void 0 : error.message) || 'Unknown error'}`);
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
return attemptSend();
|
|
643
|
+
});
|
|
644
|
+
exports.sendSticker = sendSticker;
|
|
645
|
+
/**
|
|
646
|
+
* Give typing effect to target
|
|
647
|
+
*
|
|
648
|
+
* Looks like human typing
|
|
649
|
+
*
|
|
650
|
+
* @param sessionId - Session ID
|
|
651
|
+
* @param to - Target
|
|
652
|
+
* @param duration - Duration in miliseconds typing effect will appear
|
|
653
|
+
* @param isGroup - Whether the chat is a group
|
|
654
|
+
*/
|
|
655
|
+
const sendTyping = (_a) => __awaiter(void 0, [_a], void 0, function* ({ sessionId, to, duration, isGroup, }) {
|
|
656
|
+
var _b;
|
|
657
|
+
const session = (0, Socket_1.getSession)(sessionId);
|
|
658
|
+
if (!session)
|
|
659
|
+
throw new Error_1.WhatsappError(Defaults_1.Messages.sessionNotFound(sessionId));
|
|
660
|
+
// Deteksi otomatis apakah ini adalah grup berdasarkan format JID
|
|
661
|
+
const toStr = to.toString();
|
|
662
|
+
const isGroupChat = toStr.includes('@g.us');
|
|
663
|
+
// Jika sudah dalam format grup, gunakan to langsung
|
|
664
|
+
// Jika tidak, gunakan phoneToJid untuk mengkonversi ke format yang benar
|
|
665
|
+
let jid;
|
|
666
|
+
if (isGroupChat) {
|
|
667
|
+
if (!toStr.endsWith('@g.us')) {
|
|
668
|
+
jid = toStr + '@g.us';
|
|
669
|
+
}
|
|
670
|
+
else {
|
|
671
|
+
jid = toStr;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
else {
|
|
675
|
+
jid = (0, Utils_1.phoneToJid)({ to, isGroup: isGroup || isGroupChat });
|
|
676
|
+
}
|
|
677
|
+
// Validasi JID untuk memastikan format yang didukung
|
|
678
|
+
if (!(0, Utils_2.isValidJid)(jid)) {
|
|
679
|
+
throw new Error_1.WhatsappError(`Invalid JID format: ${jid}. Supported formats: @s.whatsapp.net, @lid, @g.us`);
|
|
680
|
+
}
|
|
681
|
+
try {
|
|
682
|
+
// Deteksi apakah ini grup
|
|
683
|
+
const isDestinationGroup = jid.endsWith('@g.us');
|
|
684
|
+
const timeoutMs = isDestinationGroup ? 5000 : 10000;
|
|
685
|
+
// Kirim status mengetik dengan timeout
|
|
686
|
+
yield (0, create_delay_1.withTimeout)(session.sendPresenceUpdate("composing", jid), timeoutMs, `Timeout saat mengirim status mengetik`);
|
|
687
|
+
// Tunggu durasi yang ditentukan
|
|
688
|
+
yield (0, create_delay_1.createDelay)(duration);
|
|
689
|
+
// Setelah durasi, atur kembali status
|
|
690
|
+
yield (0, create_delay_1.withTimeout)(session.sendPresenceUpdate("paused", jid), timeoutMs, `Timeout saat menghentikan status mengetik`);
|
|
691
|
+
}
|
|
692
|
+
catch (error) {
|
|
693
|
+
// Jika koneksi tertutup, coba reconnect
|
|
694
|
+
if ((_b = error === null || error === void 0 ? void 0 : error.message) === null || _b === void 0 ? void 0 : _b.includes('Connection Closed')) {
|
|
695
|
+
try {
|
|
696
|
+
yield (0, Socket_1.reconnect)(sessionId);
|
|
697
|
+
yield (0, create_delay_1.createDelay)(2000); // Beri waktu untuk reconnect sepenuhnya
|
|
698
|
+
// Setelah reconnect, coba kirim status mengetik lagi
|
|
699
|
+
const isDestinationGroup = jid.endsWith('@g.us');
|
|
700
|
+
const timeoutMs = isDestinationGroup ? 5000 : 10000;
|
|
701
|
+
yield (0, create_delay_1.withTimeout)(session.sendPresenceUpdate("composing", jid), timeoutMs, `Timeout saat mengirim status mengetik setelah reconnect`);
|
|
702
|
+
yield (0, create_delay_1.createDelay)(duration);
|
|
703
|
+
yield (0, create_delay_1.withTimeout)(session.sendPresenceUpdate("paused", jid), timeoutMs, `Timeout saat menghentikan status mengetik setelah reconnect`);
|
|
704
|
+
}
|
|
705
|
+
catch (reconnectError) {
|
|
706
|
+
// Tidak melempar error agar aplikasi tetap berjalan
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
});
|
|
711
|
+
exports.sendTyping = sendTyping;
|
|
712
|
+
/**
|
|
713
|
+
* Mark message as read
|
|
714
|
+
*
|
|
715
|
+
* @param sessionId - Session ID
|
|
716
|
+
* @param key - Message key to mark as read
|
|
717
|
+
*/
|
|
718
|
+
const readMessage = (_a) => __awaiter(void 0, [_a], void 0, function* ({ sessionId, key, }) {
|
|
719
|
+
var _b;
|
|
720
|
+
const session = (0, Socket_1.getSession)(sessionId);
|
|
721
|
+
if (!session)
|
|
722
|
+
throw new Error_1.WhatsappError(Defaults_1.Messages.sessionNotFound(sessionId));
|
|
723
|
+
try {
|
|
724
|
+
// Terapkan timeout untuk mencegah hanging
|
|
725
|
+
yield (0, create_delay_1.withTimeout)(session.readMessages([key]), 10000, "Timeout saat menandai pesan sebagai telah dibaca");
|
|
726
|
+
}
|
|
727
|
+
catch (error) {
|
|
728
|
+
// Jika koneksi tertutup, coba reconnect
|
|
729
|
+
if ((_b = error === null || error === void 0 ? void 0 : error.message) === null || _b === void 0 ? void 0 : _b.includes('Connection Closed')) {
|
|
730
|
+
try {
|
|
731
|
+
yield (0, Socket_1.reconnect)(sessionId);
|
|
732
|
+
yield (0, create_delay_1.createDelay)(2000); // Beri waktu untuk reconnect sepenuhnya
|
|
733
|
+
// Setelah reconnect, coba tandai dibaca lagi
|
|
734
|
+
yield (0, create_delay_1.withTimeout)(session.readMessages([key]), 10000, "Timeout saat menandai pesan sebagai telah dibaca setelah reconnect");
|
|
735
|
+
}
|
|
736
|
+
catch (reconnectError) {
|
|
737
|
+
// Tidak melempar error agar aplikasi tetap berjalan
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
exports.readMessage = readMessage;
|
|
743
|
+
/**
|
|
744
|
+
* Menghapus pesan yang telah dikirim
|
|
745
|
+
*
|
|
746
|
+
* Dapat digunakan untuk menghapus pesan bot sendiri atau menghapus pesan orang lain (jika bot adalah admin di grup)
|
|
747
|
+
*
|
|
748
|
+
* @param sessionId - Session ID
|
|
749
|
+
* @param key - Message key dari pesan yang akan dihapus
|
|
750
|
+
*/
|
|
751
|
+
const deleteMessage = (_a) => __awaiter(void 0, [_a], void 0, function* ({ sessionId, key, }) {
|
|
752
|
+
var _b;
|
|
753
|
+
const session = (0, Socket_1.getSession)(sessionId);
|
|
754
|
+
if (!session)
|
|
755
|
+
throw new Error_1.WhatsappError(Defaults_1.Messages.sessionNotFound(sessionId));
|
|
756
|
+
try {
|
|
757
|
+
// Hapus pesan dengan timeout untuk mencegah hanging
|
|
758
|
+
yield (0, create_delay_1.withTimeout)(session.sendMessage(key.remoteJid, {
|
|
759
|
+
delete: key
|
|
760
|
+
}), 10000, "Timeout saat menghapus pesan");
|
|
761
|
+
}
|
|
762
|
+
catch (error) {
|
|
763
|
+
console.error(`Error saat menghapus pesan: ${(error === null || error === void 0 ? void 0 : error.message) || 'Unknown error'}`);
|
|
764
|
+
// Jika koneksi tertutup, coba reconnect
|
|
765
|
+
if ((_b = error === null || error === void 0 ? void 0 : error.message) === null || _b === void 0 ? void 0 : _b.includes('Connection Closed')) {
|
|
766
|
+
try {
|
|
767
|
+
yield (0, Socket_1.reconnect)(sessionId);
|
|
768
|
+
yield (0, create_delay_1.createDelay)(2000); // Beri waktu untuk reconnect sepenuhnya
|
|
769
|
+
// Coba hapus lagi setelah reconnect
|
|
770
|
+
yield (0, create_delay_1.withTimeout)(session.sendMessage(key.remoteJid, {
|
|
771
|
+
delete: key
|
|
772
|
+
}), 10000, "Timeout saat menghapus pesan setelah reconnect");
|
|
773
|
+
}
|
|
774
|
+
catch (reconnectError) {
|
|
775
|
+
console.error(`Gagal reconnect atau menghapus pesan: ${(reconnectError === null || reconnectError === void 0 ? void 0 : reconnectError.message) || 'Unknown error'}`);
|
|
776
|
+
throw new Error_1.WhatsappError(`Gagal menghapus pesan: ${(reconnectError === null || reconnectError === void 0 ? void 0 : reconnectError.message) || 'Unknown error'}`);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
else {
|
|
780
|
+
throw new Error_1.WhatsappError(`Gagal menghapus pesan: ${(error === null || error === void 0 ? void 0 : error.message) || 'Unknown error'}`);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
});
|
|
784
|
+
exports.deleteMessage = deleteMessage;
|