whatsauto.js 1.2.3 → 2.0.0
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 +40 -1
- package/dist/Defaults/index.d.ts +3 -2
- package/dist/Defaults/index.d.ts.map +1 -1
- package/dist/Defaults/index.js +8 -13
- package/dist/Error/index.js +2 -7
- package/dist/Logger/index.d.ts +5 -5
- package/dist/Logger/index.d.ts.map +1 -1
- package/dist/Logger/index.js +19 -39
- package/dist/Types/index.d.ts +21 -14
- package/dist/Types/index.d.ts.map +1 -1
- package/dist/Types/index.js +1 -2
- package/dist/Utils/helper.d.ts +3 -2
- package/dist/Utils/helper.d.ts.map +1 -1
- package/dist/Utils/helper.js +56 -96
- package/dist/Utils/index.d.ts +1 -1
- package/dist/Utils/index.d.ts.map +1 -1
- package/dist/Utils/index.js +1 -17
- package/dist/Utils/make-stiker.d.ts +1 -1
- package/dist/Utils/make-stiker.d.ts.map +1 -1
- package/dist/Utils/make-stiker.js +35 -50
- package/dist/WhatsApp/AutoWA.d.ts +22 -21
- package/dist/WhatsApp/AutoWA.d.ts.map +1 -1
- package/dist/WhatsApp/AutoWA.js +730 -775
- package/dist/WhatsApp/AutoWAEvent.js +3 -8
- package/dist/WhatsApp/index.d.ts +3 -2
- package/dist/WhatsApp/index.d.ts.map +1 -1
- package/dist/WhatsApp/index.js +4 -19
- package/dist/index.d.ts +5 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -22
- package/package.json +15 -5
- package/dist/Utils/set-credentials-dir.d.ts +0 -2
- package/dist/Utils/set-credentials-dir.d.ts.map +0 -1
- package/dist/Utils/set-credentials-dir.js +0 -15
- package/dist/WhatsApp/AutoWAManager.d.ts +0 -12
- package/dist/WhatsApp/AutoWAManager.d.ts.map +0 -1
- package/dist/WhatsApp/AutoWAManager.js +0 -68
package/dist/WhatsApp/AutoWA.js
CHANGED
|
@@ -1,869 +1,825 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
o["default"] = v;
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import makeWASocket, { DisconnectReason, downloadMediaMessage, Browsers, fetchLatestBaileysVersion, useMultiFileAuthState, proto, } from "@whiskeysockets/baileys";
|
|
4
|
+
import { CREDENTIALS, Messages } from "../Defaults/index.js";
|
|
5
|
+
import { ValidationError, AutoWAError } from "../Error/index.js";
|
|
6
|
+
import { parseMessageStatusCodeToReadable, getMediaMimeType, phoneToJid, createDelay, isSessionExist, getRandomFromArrays, getContextInfo, } from "../Utils/helper.js";
|
|
7
|
+
import AutoWAEvent from "./AutoWAEvent.js";
|
|
8
|
+
import mime from "mime";
|
|
9
|
+
import Logger from "../Logger/index.js";
|
|
10
|
+
import { makeWebpBuffer } from "../Utils/make-stiker.js";
|
|
11
|
+
import { sessions } from "./index.js";
|
|
12
|
+
import pino from "pino";
|
|
13
|
+
import qrcode from "qrcode-terminal";
|
|
14
|
+
const P = pino({
|
|
15
|
+
level: "silent",
|
|
17
16
|
});
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
17
|
+
export class AutoWA {
|
|
18
|
+
logger;
|
|
19
|
+
retryCount;
|
|
20
|
+
sock;
|
|
21
|
+
sessionId;
|
|
22
|
+
options;
|
|
23
|
+
events = new AutoWAEvent();
|
|
24
|
+
pairingCode;
|
|
25
|
+
defaultStickerProps = {
|
|
26
|
+
pack: "whatsauto.js",
|
|
27
|
+
author: "freack21",
|
|
28
|
+
media: null,
|
|
33
29
|
};
|
|
34
|
-
})();
|
|
35
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
45
|
-
var t = {};
|
|
46
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
47
|
-
t[p] = s[p];
|
|
48
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
49
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
50
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
51
|
-
t[p[i]] = s[p[i]];
|
|
52
|
-
}
|
|
53
|
-
return t;
|
|
54
|
-
};
|
|
55
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
56
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
57
|
-
};
|
|
58
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
59
|
-
exports.AutoWA = void 0;
|
|
60
|
-
const path_1 = __importDefault(require("path"));
|
|
61
|
-
const fs_1 = __importDefault(require("fs"));
|
|
62
|
-
const baileys_1 = __importStar(require("@whiskeysockets/baileys"));
|
|
63
|
-
const Defaults_1 = require("../Defaults");
|
|
64
|
-
const Error_1 = require("../Error");
|
|
65
|
-
const helper_1 = require("../Utils/helper");
|
|
66
|
-
const AutoWAEvent_1 = __importDefault(require("./AutoWAEvent"));
|
|
67
|
-
const mime_1 = __importDefault(require("mime"));
|
|
68
|
-
const Logger_1 = __importDefault(require("../Logger"));
|
|
69
|
-
const make_stiker_1 = require("../Utils/make-stiker");
|
|
70
|
-
const _1 = require(".");
|
|
71
|
-
const P = require("pino")({
|
|
72
|
-
level: "fatal",
|
|
73
|
-
});
|
|
74
|
-
const qrcode = require("qrcode-terminal");
|
|
75
|
-
class AutoWA {
|
|
76
30
|
constructor(sessionId, options) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
pack: "whatsauto.js",
|
|
80
|
-
author: "freack21",
|
|
81
|
-
media: null,
|
|
82
|
-
};
|
|
83
|
-
if ((0, helper_1.isSessionExist)(sessionId) && _1.sessions.get(sessionId))
|
|
84
|
-
throw new Error_1.ValidationError(Defaults_1.Messages.sessionAlreadyExist(sessionId));
|
|
31
|
+
if (isSessionExist(sessionId) && sessions.get(sessionId))
|
|
32
|
+
throw new ValidationError(Messages.sessionAlreadyExist(sessionId));
|
|
85
33
|
const defaultOptions = {
|
|
86
34
|
printQR: true,
|
|
87
35
|
logging: true,
|
|
88
36
|
};
|
|
89
37
|
this.sessionId = sessionId;
|
|
90
|
-
this.options =
|
|
38
|
+
this.options = { ...defaultOptions, ...options };
|
|
91
39
|
this.retryCount = 0;
|
|
92
|
-
this.logger = new
|
|
93
|
-
|
|
40
|
+
this.logger = new Logger(sessionId, this);
|
|
41
|
+
sessions.set(sessionId, this);
|
|
94
42
|
this.logger.info("Created!");
|
|
95
43
|
}
|
|
96
|
-
setLogging(logging) {
|
|
97
|
-
|
|
98
|
-
this.options.logging = logging;
|
|
99
|
-
});
|
|
44
|
+
async setLogging(logging) {
|
|
45
|
+
this.options.logging = logging;
|
|
100
46
|
}
|
|
101
|
-
initialize() {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
yield this.startWhatsApp(this.sessionId, this.options);
|
|
105
|
-
});
|
|
47
|
+
async initialize() {
|
|
48
|
+
this.logger.info("Initializing...");
|
|
49
|
+
await this.startWhatsApp(this.sessionId, this.options);
|
|
106
50
|
}
|
|
107
|
-
startWhatsApp() {
|
|
108
|
-
|
|
109
|
-
if (
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
options.phoneNumber
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
51
|
+
async startWhatsApp(sessionId = "mySession", options = { printQR: true }) {
|
|
52
|
+
if (typeof options.phoneNumber == "string") {
|
|
53
|
+
if (options.phoneNumber === "")
|
|
54
|
+
throw new ValidationError(Messages.paremetersNotValid("phoneNumber"));
|
|
55
|
+
options.printQR = false;
|
|
56
|
+
options.phoneNumber = phoneToJid({
|
|
57
|
+
from: options.phoneNumber,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return this.startSocket(sessionId, options);
|
|
61
|
+
}
|
|
62
|
+
async startSocket(sessionId, options) {
|
|
63
|
+
try {
|
|
64
|
+
const { version } = await fetchLatestBaileysVersion();
|
|
65
|
+
const { state, saveCreds } = await useMultiFileAuthState(path.resolve(CREDENTIALS.DIR_NAME, sessionId + CREDENTIALS.PREFIX));
|
|
66
|
+
const [platform, browser] = getRandomFromArrays([Browsers.macOS, Browsers.ubuntu, Browsers.windows], ["Chrome", "Firefox", "Safari"]);
|
|
67
|
+
this.sock = makeWASocket({
|
|
68
|
+
version,
|
|
69
|
+
auth: state,
|
|
70
|
+
logger: P,
|
|
71
|
+
markOnlineOnConnect: false,
|
|
72
|
+
browser: platform(browser),
|
|
73
|
+
});
|
|
74
|
+
return this.setupWASocket(saveCreds);
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
const msg = `Failed initiliaze WASocket: ${error.message}`;
|
|
78
|
+
this.logger.error(msg);
|
|
79
|
+
throw new AutoWAError(msg);
|
|
80
|
+
}
|
|
119
81
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
logger:
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
82
|
+
async setupWASocket(saveCreds) {
|
|
83
|
+
try {
|
|
84
|
+
if (typeof this.options.phoneNumber == "string" &&
|
|
85
|
+
!this.options.printQR &&
|
|
86
|
+
!this.pairingCode &&
|
|
87
|
+
!this.sock.authState.creds.registered) {
|
|
88
|
+
const phoneNumber = phoneToJid({ from: this.options.phoneNumber, reverse: true });
|
|
89
|
+
try {
|
|
90
|
+
this.pairingCode = await this.sock.requestPairingCode(phoneNumber);
|
|
91
|
+
this.logger.info(`Pairing Code: ${this.pairingCode}`);
|
|
92
|
+
this.events.emit("pairing-code", this.pairingCode);
|
|
93
|
+
this.retryCount = 0;
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
this.retryCount++;
|
|
97
|
+
this.logger.warn(`Retry get pairing code for ${phoneNumber} (${this.retryCount}x)`);
|
|
98
|
+
await createDelay(1000);
|
|
99
|
+
return await this.startSocket(this.sessionId, this.options);
|
|
100
|
+
}
|
|
139
101
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
!this.options.printQR &&
|
|
147
|
-
!this.pairingCode &&
|
|
148
|
-
!this.sock.authState.creds.registered) {
|
|
149
|
-
const phoneNumber = (0, helper_1.phoneToJid)({ from: this.options.phoneNumber, reverse: true });
|
|
150
|
-
try {
|
|
151
|
-
this.pairingCode = yield this.sock.requestPairingCode(phoneNumber);
|
|
152
|
-
this.logger.info(`Pairing Code: ${this.pairingCode}`);
|
|
153
|
-
this.events.emit("pairing-code", this.pairingCode);
|
|
154
|
-
this.retryCount = 0;
|
|
155
|
-
}
|
|
156
|
-
catch (error) {
|
|
157
|
-
this.retryCount++;
|
|
158
|
-
this.logger.warn(`Retry get pairing code for ${phoneNumber} (${this.retryCount}x)`);
|
|
159
|
-
yield (0, helper_1.createDelay)(1000);
|
|
160
|
-
return yield this.startSocket(this.sessionId, this.options);
|
|
102
|
+
this.sock.ev.on("connection.update", async (update) => {
|
|
103
|
+
const { connection, lastDisconnect, qr } = update;
|
|
104
|
+
if (this.options.printQR && qr) {
|
|
105
|
+
this.logger.info("QR Updated!");
|
|
106
|
+
if (this.options.printQR) {
|
|
107
|
+
qrcode.generate(qr, { small: true });
|
|
161
108
|
}
|
|
109
|
+
this.events.emit("qr", qr);
|
|
162
110
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
this.
|
|
111
|
+
if (connection == "connecting") {
|
|
112
|
+
this.logger.info("Connecting...");
|
|
113
|
+
this.events.emit("connecting");
|
|
114
|
+
}
|
|
115
|
+
if (connection === "close" && !this.pairingCode) {
|
|
116
|
+
const code = lastDisconnect?.error?.output?.statusCode;
|
|
117
|
+
let shouldRetry = false;
|
|
118
|
+
if (code == DisconnectReason.restartRequired) {
|
|
119
|
+
this.logger.info("Restarting...");
|
|
120
|
+
return await this.startSocket(this.sessionId, this.options);
|
|
172
121
|
}
|
|
173
|
-
if (
|
|
174
|
-
this.logger.
|
|
175
|
-
|
|
122
|
+
else if (code == DisconnectReason.connectionLost) {
|
|
123
|
+
this.logger.warn("No Internet!");
|
|
124
|
+
shouldRetry = true;
|
|
176
125
|
}
|
|
177
|
-
if (
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
if (code == baileys_1.DisconnectReason.restartRequired) {
|
|
181
|
-
this.logger.info("Restarting...");
|
|
182
|
-
return yield this.startSocket(this.sessionId, this.options);
|
|
183
|
-
}
|
|
184
|
-
else if (code == baileys_1.DisconnectReason.connectionLost) {
|
|
185
|
-
this.logger.warn("No Internet!");
|
|
186
|
-
shouldRetry = true;
|
|
187
|
-
}
|
|
188
|
-
else if (code == baileys_1.DisconnectReason.connectionClosed) {
|
|
189
|
-
this.logger.warn("Connection Closed!");
|
|
190
|
-
shouldRetry = true;
|
|
191
|
-
}
|
|
192
|
-
else if (code == baileys_1.DisconnectReason.loggedOut) {
|
|
193
|
-
this.logger.warn("Logged Out!");
|
|
194
|
-
}
|
|
195
|
-
else if (code != baileys_1.DisconnectReason.loggedOut && this.retryCount < 10) {
|
|
196
|
-
this.logger.warn("Connection Status : " + code);
|
|
197
|
-
shouldRetry = true;
|
|
198
|
-
}
|
|
199
|
-
if (shouldRetry) {
|
|
200
|
-
this.logger.warn("Retry connecting...");
|
|
201
|
-
this.retryCount++;
|
|
202
|
-
yield (0, helper_1.createDelay)(5000);
|
|
203
|
-
return yield this.startSocket(this.sessionId, this.options);
|
|
204
|
-
}
|
|
205
|
-
else {
|
|
206
|
-
this.retryCount = 0;
|
|
207
|
-
this.logger.warn("Disconnected!");
|
|
208
|
-
this.events.emit("disconnected");
|
|
209
|
-
try {
|
|
210
|
-
yield this.destroy(true);
|
|
211
|
-
}
|
|
212
|
-
catch (error) { }
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
126
|
+
else if (code == DisconnectReason.connectionClosed) {
|
|
127
|
+
this.logger.warn("Connection Closed!");
|
|
128
|
+
shouldRetry = true;
|
|
215
129
|
}
|
|
216
|
-
if (
|
|
217
|
-
this.logger.
|
|
218
|
-
this.retryCount = 0;
|
|
219
|
-
this.events.emit("connected");
|
|
130
|
+
else if (code == DisconnectReason.loggedOut) {
|
|
131
|
+
this.logger.warn("Logged Out!");
|
|
220
132
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
133
|
+
else if (code != DisconnectReason.loggedOut && this.retryCount < 10) {
|
|
134
|
+
this.logger.warn("Connection Status : " + code);
|
|
135
|
+
shouldRetry = true;
|
|
136
|
+
}
|
|
137
|
+
if (shouldRetry) {
|
|
138
|
+
this.logger.warn("Retry connecting...");
|
|
139
|
+
this.retryCount++;
|
|
140
|
+
await createDelay(5000);
|
|
141
|
+
return await this.startSocket(this.sessionId, this.options);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
this.retryCount = 0;
|
|
145
|
+
this.logger.warn("Disconnected!");
|
|
146
|
+
this.events.emit("disconnected");
|
|
147
|
+
try {
|
|
148
|
+
await this.destroy(true);
|
|
149
|
+
}
|
|
150
|
+
catch (error) { }
|
|
233
151
|
return;
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (connection == "open") {
|
|
155
|
+
this.logger.info("Connected!");
|
|
156
|
+
this.retryCount = 0;
|
|
157
|
+
this.events.emit("connected");
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
this.sock.ev.on("creds.update", async () => {
|
|
161
|
+
await saveCreds();
|
|
162
|
+
});
|
|
163
|
+
this.sock.ev.on("messages.update", async (message) => {
|
|
164
|
+
const msg = message[0];
|
|
165
|
+
const data = {
|
|
166
|
+
sessionId: this.sessionId,
|
|
167
|
+
messageStatus: parseMessageStatusCodeToReadable(msg.update.status),
|
|
168
|
+
...msg,
|
|
169
|
+
};
|
|
170
|
+
this.events.emit("message-updated", data);
|
|
171
|
+
});
|
|
172
|
+
this.sock.ev.on("messages.upsert", async (new_message) => {
|
|
173
|
+
if (new_message.type == "append")
|
|
174
|
+
return;
|
|
175
|
+
const myJid = phoneToJid({ from: this.sock.user.id });
|
|
176
|
+
let msg = new_message.messages?.[0];
|
|
177
|
+
const isDeletedMsg = new_message.messages?.[0].message?.protocolMessage?.type ==
|
|
178
|
+
proto.Message.ProtocolMessage.Type.REVOKE;
|
|
179
|
+
if (isDeletedMsg) {
|
|
180
|
+
msg = {
|
|
181
|
+
...new_message.messages?.[0],
|
|
182
|
+
deletedMessage: {
|
|
183
|
+
key: {
|
|
184
|
+
id: new_message.messages?.[0].message?.protocolMessage?.key?.id,
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
if (msg.message?.documentWithCaptionMessage)
|
|
191
|
+
msg = {
|
|
192
|
+
...msg,
|
|
193
|
+
message: msg.message.documentWithCaptionMessage.message,
|
|
194
|
+
};
|
|
195
|
+
else if (msg.message?.ephemeralMessage)
|
|
196
|
+
msg = {
|
|
197
|
+
...msg,
|
|
198
|
+
message: msg.message.ephemeralMessage?.message,
|
|
199
|
+
};
|
|
240
200
|
msg.sessionId = this.sessionId;
|
|
241
201
|
let quotedMessage = null;
|
|
242
|
-
const msgContextInfo = (
|
|
243
|
-
|
|
244
|
-
((_k = (_j = msg.message) === null || _j === void 0 ? void 0 : _j.videoMessage) === null || _k === void 0 ? void 0 : _k.contextInfo) ||
|
|
245
|
-
((_m = (_l = msg.message) === null || _l === void 0 ? void 0 : _l.stickerMessage) === null || _m === void 0 ? void 0 : _m.contextInfo) ||
|
|
246
|
-
((_p = (_o = msg.message) === null || _o === void 0 ? void 0 : _o.documentMessage) === null || _p === void 0 ? void 0 : _p.contextInfo);
|
|
247
|
-
if (msgContextInfo === null || msgContextInfo === void 0 ? void 0 : msgContextInfo.quotedMessage) {
|
|
202
|
+
const msgContextInfo = getContextInfo(msg);
|
|
203
|
+
if (msgContextInfo?.quotedMessage) {
|
|
248
204
|
quotedMessage = {
|
|
249
205
|
key: {
|
|
250
|
-
remoteJid:
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
206
|
+
remoteJid: msg.key?.remoteJid,
|
|
207
|
+
remoteJidAlt: msg.key?.remoteJidAlt,
|
|
208
|
+
id: msgContextInfo?.stanzaId,
|
|
209
|
+
participant: msgContextInfo?.participant,
|
|
210
|
+
fromMe: msgContextInfo?.participant == myJid,
|
|
254
211
|
},
|
|
255
|
-
message: msgContextInfo
|
|
212
|
+
message: msgContextInfo?.quotedMessage,
|
|
256
213
|
};
|
|
257
214
|
}
|
|
258
|
-
if (
|
|
259
|
-
quotedMessage =
|
|
215
|
+
if (quotedMessage?.message?.documentWithCaptionMessage) {
|
|
216
|
+
quotedMessage = {
|
|
217
|
+
...quotedMessage,
|
|
218
|
+
message: quotedMessage.message.documentWithCaptionMessage.message,
|
|
219
|
+
};
|
|
260
220
|
}
|
|
261
221
|
msg.quotedMessage = quotedMessage;
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
222
|
+
}
|
|
223
|
+
const mediaTypes = ["image", "audio", "video", "document"];
|
|
224
|
+
const setupMsg = (msg) => {
|
|
225
|
+
const text = msg.message?.conversation ||
|
|
226
|
+
msg.message?.extendedTextMessage?.text ||
|
|
227
|
+
msg.message?.imageMessage?.caption ||
|
|
228
|
+
msg.message?.videoMessage?.caption ||
|
|
229
|
+
msg.message?.documentMessage?.caption ||
|
|
230
|
+
"";
|
|
231
|
+
msg.text = text;
|
|
232
|
+
const mimeType = getMediaMimeType(msg);
|
|
233
|
+
const ext = mime.getExtension(mimeType);
|
|
234
|
+
msg.hasMedia = mimeType !== "";
|
|
235
|
+
msg.mediaType = "";
|
|
236
|
+
if (mimeType)
|
|
237
|
+
msg.mediaType =
|
|
238
|
+
mediaTypes[mediaTypes.indexOf(mimeType.split("/")[0]) !== -1
|
|
239
|
+
? mediaTypes.indexOf(mimeType.split("/")[0])
|
|
240
|
+
: 3];
|
|
241
|
+
msg.downloadMedia = async () => Promise.resolve(null);
|
|
242
|
+
msg.toSticker = async () => Promise.resolve([null, false]);
|
|
243
|
+
if (msg.hasMedia) {
|
|
244
|
+
msg.downloadMedia = async (opts = {}) => this.downloadMedia(msg, opts, ext);
|
|
245
|
+
}
|
|
246
|
+
if (msg.hasMedia || msg.quotedMessage?.hasMedia) {
|
|
247
|
+
msg.toSticker = async (props) => {
|
|
248
|
+
let mediaBuf;
|
|
249
|
+
if (msg.hasMedia && ["image", "video"].includes(msg.mediaType)) {
|
|
250
|
+
mediaBuf = await msg.downloadMedia({ asBuffer: true });
|
|
251
|
+
}
|
|
252
|
+
else if (msg.quotedMessage &&
|
|
253
|
+
msg.quotedMessage.hasMedia &&
|
|
254
|
+
["image", "video"].includes(msg.quotedMessage.mediaType)) {
|
|
255
|
+
mediaBuf = await msg.quotedMessage.downloadMedia({ asBuffer: true });
|
|
256
|
+
}
|
|
257
|
+
if (!mediaBuf)
|
|
258
|
+
return [null, false];
|
|
259
|
+
const stickerProps = {
|
|
260
|
+
...this.defaultStickerProps,
|
|
261
|
+
...props,
|
|
262
|
+
media: mediaBuf,
|
|
263
|
+
};
|
|
264
|
+
const buffer = await makeWebpBuffer(stickerProps);
|
|
265
|
+
return [buffer, true];
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
const from = msg.key?.remoteJidAlt || msg.key?.remoteJid || "";
|
|
269
|
+
const participant = msg.key?.participant || "";
|
|
270
|
+
const isGroup = from.includes("@g.us");
|
|
271
|
+
const isStory = from.includes("status@broadcast");
|
|
272
|
+
const isReaction = msg.message?.reactionMessage ? true : false;
|
|
273
|
+
if (msg.key?.fromMe) {
|
|
274
|
+
msg.receiver = from;
|
|
275
|
+
msg.author = myJid;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
msg.receiver = myJid;
|
|
279
|
+
msg.author = from;
|
|
280
|
+
if (isGroup || isStory)
|
|
281
|
+
msg.author = participant;
|
|
282
|
+
if (isGroup)
|
|
311
283
|
msg.receiver = from;
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
msg.replyWithVideo = (media, opts) => __awaiter(this, void 0, void 0, function* () {
|
|
338
|
-
return yield this.sendVideo(Object.assign(Object.assign({ media }, opts), { to: from, answering: msg }));
|
|
339
|
-
});
|
|
340
|
-
msg.replyWithSticker = (sticker, opts) => __awaiter(this, void 0, void 0, function* () {
|
|
341
|
-
return yield this.sendSticker(Object.assign(Object.assign({ sticker }, opts), { to: from, answering: msg }));
|
|
342
|
-
});
|
|
343
|
-
msg.replyWithTyping = (callback) => __awaiter(this, void 0, void 0, function* () {
|
|
344
|
-
return yield this.sendTyping({ to: from, callback });
|
|
345
|
-
});
|
|
346
|
-
msg.replyWithRecording = (callback) => __awaiter(this, void 0, void 0, function* () {
|
|
347
|
-
return yield this.sendRecording({ to: from, callback });
|
|
348
|
-
});
|
|
349
|
-
msg.read = () => __awaiter(this, void 0, void 0, function* () {
|
|
350
|
-
return yield this.readMessage([msg]);
|
|
351
|
-
});
|
|
352
|
-
msg.react = (reaction) => __awaiter(this, void 0, void 0, function* () {
|
|
353
|
-
return yield this.sendReaction({ to: from, answering: msg, text: reaction });
|
|
354
|
-
});
|
|
355
|
-
msg.forward = (to, opts) => __awaiter(this, void 0, void 0, function* () {
|
|
356
|
-
return yield this.forwardMessage(Object.assign({ to, msg }, opts));
|
|
284
|
+
}
|
|
285
|
+
msg.from = from;
|
|
286
|
+
msg.isGroup = isGroup;
|
|
287
|
+
msg.isStory = isStory;
|
|
288
|
+
msg.isReaction = isReaction;
|
|
289
|
+
if (isReaction)
|
|
290
|
+
msg.text = msg.message?.reactionMessage?.text;
|
|
291
|
+
msg.replyWithText = async (text, opts) => {
|
|
292
|
+
return await this.sendText({ ...opts, text, to: from, answering: msg });
|
|
293
|
+
};
|
|
294
|
+
msg.replyWithAudio = async (media, opts) => {
|
|
295
|
+
return await this.sendAudio({ media, ...opts, to: from, answering: msg });
|
|
296
|
+
};
|
|
297
|
+
msg.replyWithImage = async (media, opts) => {
|
|
298
|
+
return await this.sendImage({ media, ...opts, to: from, answering: msg });
|
|
299
|
+
};
|
|
300
|
+
msg.replyWithVideo = async (media, opts) => {
|
|
301
|
+
return await this.sendVideo({ media, ...opts, to: from, answering: msg });
|
|
302
|
+
};
|
|
303
|
+
msg.replyWithSticker = async (sticker, opts) => {
|
|
304
|
+
return await this.sendSticker({
|
|
305
|
+
sticker,
|
|
306
|
+
...opts,
|
|
307
|
+
to: from,
|
|
308
|
+
answering: msg,
|
|
357
309
|
});
|
|
358
310
|
};
|
|
359
|
-
msg.
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
this.
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
311
|
+
msg.replyWithTyping = async (callback) => {
|
|
312
|
+
return await this.sendTyping({ to: from, callback });
|
|
313
|
+
};
|
|
314
|
+
msg.replyWithRecording = async (callback) => {
|
|
315
|
+
return await this.sendRecording({ to: from, callback });
|
|
316
|
+
};
|
|
317
|
+
msg.read = async () => {
|
|
318
|
+
return await this.readMessage([msg]);
|
|
319
|
+
};
|
|
320
|
+
msg.react = async (reaction) => {
|
|
321
|
+
return await this.sendReaction({ to: from, answering: msg, text: reaction });
|
|
322
|
+
};
|
|
323
|
+
msg.forward = async (to, opts) => {
|
|
324
|
+
return await this.forwardMessage({ to, msg, ...opts });
|
|
325
|
+
};
|
|
326
|
+
};
|
|
327
|
+
msg.quotedMessage && setupMsg(msg.quotedMessage);
|
|
328
|
+
setupMsg(msg);
|
|
329
|
+
const { isStory, isReaction, isGroup } = msg;
|
|
330
|
+
if (msg.key.fromMe) {
|
|
331
|
+
this.events.emit("message-sent", msg);
|
|
332
|
+
if (isStory) {
|
|
333
|
+
this.events.emit("story-sent", msg);
|
|
334
|
+
}
|
|
335
|
+
else if (isReaction) {
|
|
336
|
+
this.events.emit("reaction-sent", msg);
|
|
337
|
+
if (isGroup)
|
|
338
|
+
this.events.emit("group-reaction-sent", msg);
|
|
339
|
+
else
|
|
340
|
+
this.events.emit("private-reaction-sent", msg);
|
|
341
|
+
}
|
|
342
|
+
else if (isGroup) {
|
|
343
|
+
this.events.emit("group-message-sent", msg);
|
|
380
344
|
}
|
|
381
345
|
else {
|
|
382
|
-
this.events.emit("message-
|
|
383
|
-
if (isStory) {
|
|
384
|
-
this.events.emit("story-received", msg);
|
|
385
|
-
}
|
|
386
|
-
else if (isReaction) {
|
|
387
|
-
this.events.emit("reaction-received", msg);
|
|
388
|
-
if (isGroup)
|
|
389
|
-
this.events.emit("group-reaction-received", msg);
|
|
390
|
-
else
|
|
391
|
-
this.events.emit("private-reaction-received", msg);
|
|
392
|
-
}
|
|
393
|
-
else if (isGroup) {
|
|
394
|
-
this.events.emit("group-message-received", msg);
|
|
395
|
-
}
|
|
396
|
-
else {
|
|
397
|
-
this.events.emit("private-message-received", msg);
|
|
398
|
-
}
|
|
346
|
+
this.events.emit("private-message-sent", msg);
|
|
399
347
|
}
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
this.events.emit("message-received", msg);
|
|
400
351
|
if (isStory) {
|
|
401
|
-
this.events.emit("story", msg);
|
|
352
|
+
this.events.emit("story-received", msg);
|
|
402
353
|
}
|
|
403
354
|
else if (isReaction) {
|
|
404
|
-
this.events.emit("reaction", msg);
|
|
355
|
+
this.events.emit("reaction-received", msg);
|
|
405
356
|
if (isGroup)
|
|
406
|
-
this.events.emit("group-reaction", msg);
|
|
357
|
+
this.events.emit("group-reaction-received", msg);
|
|
407
358
|
else
|
|
408
|
-
this.events.emit("private-reaction", msg);
|
|
359
|
+
this.events.emit("private-reaction-received", msg);
|
|
409
360
|
}
|
|
410
361
|
else if (isGroup) {
|
|
411
|
-
this.events.emit("group-message", msg);
|
|
362
|
+
this.events.emit("group-message-received", msg);
|
|
412
363
|
}
|
|
413
364
|
else {
|
|
414
|
-
this.events.emit("private-message", msg);
|
|
415
|
-
}
|
|
416
|
-
this.events.emit("message", msg);
|
|
417
|
-
}));
|
|
418
|
-
this.sock.ev.on("group-participants.update", (data) => __awaiter(this, void 0, void 0, function* () {
|
|
419
|
-
const msg = Object.assign(Object.assign({}, data), { sessionId: this.sessionId });
|
|
420
|
-
msg.replyWithText = (text, opts) => __awaiter(this, void 0, void 0, function* () {
|
|
421
|
-
return yield this.sendText(Object.assign(Object.assign({}, opts), { text, to: data.id }));
|
|
422
|
-
});
|
|
423
|
-
msg.replyWithAudio = (media, opts) => __awaiter(this, void 0, void 0, function* () {
|
|
424
|
-
return yield this.sendAudio(Object.assign(Object.assign({ media }, opts), { to: data.id }));
|
|
425
|
-
});
|
|
426
|
-
msg.replyWithImage = (media, opts) => __awaiter(this, void 0, void 0, function* () {
|
|
427
|
-
return yield this.sendImage(Object.assign(Object.assign({ media }, opts), { to: data.id }));
|
|
428
|
-
});
|
|
429
|
-
msg.replyWithVideo = (media, opts) => __awaiter(this, void 0, void 0, function* () {
|
|
430
|
-
return yield this.sendVideo(Object.assign(Object.assign({ media }, opts), { to: data.id }));
|
|
431
|
-
});
|
|
432
|
-
msg.replyWithSticker = (sticker, opts) => __awaiter(this, void 0, void 0, function* () {
|
|
433
|
-
return yield this.sendSticker(Object.assign(Object.assign({ sticker }, opts), { to: data.id }));
|
|
434
|
-
});
|
|
435
|
-
msg.replyWithTyping = (callback) => __awaiter(this, void 0, void 0, function* () {
|
|
436
|
-
return yield this.sendTyping({ to: data.id, callback });
|
|
437
|
-
});
|
|
438
|
-
msg.replyWithRecording = (callback) => __awaiter(this, void 0, void 0, function* () {
|
|
439
|
-
return yield this.sendRecording({ to: data.id, callback });
|
|
440
|
-
});
|
|
441
|
-
this.events.emit("group-member-update", msg);
|
|
442
|
-
}));
|
|
443
|
-
return this.sock;
|
|
444
|
-
}
|
|
445
|
-
catch (error) {
|
|
446
|
-
const msg = `Failed setup WASocket: ${error.message}`;
|
|
447
|
-
this.logger.error(msg);
|
|
448
|
-
throw new Error_1.AutoWAError(msg);
|
|
449
|
-
}
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
destroy(full) {
|
|
453
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
454
|
-
this.logger.info("Destroying...");
|
|
455
|
-
let error = false;
|
|
456
|
-
let msg = "";
|
|
457
|
-
try {
|
|
458
|
-
yield this.sock.logout();
|
|
459
|
-
}
|
|
460
|
-
catch (err) {
|
|
461
|
-
msg = `Logout failed: ${err.message}`;
|
|
462
|
-
error = true;
|
|
463
|
-
}
|
|
464
|
-
finally {
|
|
465
|
-
this.sock.end(undefined);
|
|
466
|
-
if (full) {
|
|
467
|
-
const dir = path_1.default.resolve(Defaults_1.CREDENTIALS.DIR_NAME, this.sessionId + Defaults_1.CREDENTIALS.PREFIX);
|
|
468
|
-
if (fs_1.default.existsSync(dir)) {
|
|
469
|
-
fs_1.default.rmSync(dir, { force: true, recursive: true });
|
|
365
|
+
this.events.emit("private-message-received", msg);
|
|
470
366
|
}
|
|
471
367
|
}
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
}
|
|
478
|
-
});
|
|
479
|
-
}
|
|
480
|
-
isExist(_a) {
|
|
481
|
-
return __awaiter(this, arguments, void 0, function* ({ from, isGroup = false }) {
|
|
482
|
-
var _b, _c;
|
|
483
|
-
try {
|
|
484
|
-
const receiver = (0, helper_1.phoneToJid)({
|
|
485
|
-
from: from,
|
|
486
|
-
isGroup,
|
|
487
|
-
});
|
|
488
|
-
if (receiver.includes("@broadcast")) {
|
|
489
|
-
return true;
|
|
368
|
+
if (isDeletedMsg) {
|
|
369
|
+
this.events.emit("message-deleted", msg);
|
|
370
|
+
}
|
|
371
|
+
else if (isStory) {
|
|
372
|
+
this.events.emit("story", msg);
|
|
490
373
|
}
|
|
491
|
-
else if (
|
|
492
|
-
|
|
374
|
+
else if (isReaction) {
|
|
375
|
+
this.events.emit("reaction", msg);
|
|
376
|
+
if (isGroup)
|
|
377
|
+
this.events.emit("group-reaction", msg);
|
|
378
|
+
else
|
|
379
|
+
this.events.emit("private-reaction", msg);
|
|
380
|
+
}
|
|
381
|
+
else if (isGroup) {
|
|
382
|
+
this.events.emit("group-message", msg);
|
|
493
383
|
}
|
|
494
384
|
else {
|
|
495
|
-
|
|
385
|
+
this.events.emit("private-message", msg);
|
|
496
386
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
387
|
+
this.events.emit("message", msg);
|
|
388
|
+
});
|
|
389
|
+
this.sock.ev.on("group-participants.update", async (data) => {
|
|
390
|
+
const msg = {
|
|
391
|
+
...data,
|
|
392
|
+
sessionId: this.sessionId,
|
|
393
|
+
};
|
|
394
|
+
msg.replyWithText = async (text, opts) => {
|
|
395
|
+
return await this.sendText({ ...opts, text, to: data.id });
|
|
396
|
+
};
|
|
397
|
+
msg.replyWithAudio = async (media, opts) => {
|
|
398
|
+
return await this.sendAudio({ media, ...opts, to: data.id });
|
|
399
|
+
};
|
|
400
|
+
msg.replyWithImage = async (media, opts) => {
|
|
401
|
+
return await this.sendImage({ media, ...opts, to: data.id });
|
|
402
|
+
};
|
|
403
|
+
msg.replyWithVideo = async (media, opts) => {
|
|
404
|
+
return await this.sendVideo({ media, ...opts, to: data.id });
|
|
405
|
+
};
|
|
406
|
+
msg.replyWithSticker = async (sticker, opts) => {
|
|
407
|
+
return await this.sendSticker({ sticker, ...opts, to: data.id });
|
|
408
|
+
};
|
|
409
|
+
msg.replyWithTyping = async (callback) => {
|
|
410
|
+
return await this.sendTyping({ to: data.id, callback });
|
|
411
|
+
};
|
|
412
|
+
msg.replyWithRecording = async (callback) => {
|
|
413
|
+
return await this.sendRecording({ to: data.id, callback });
|
|
414
|
+
};
|
|
415
|
+
this.events.emit("group-member-update", msg);
|
|
416
|
+
});
|
|
417
|
+
this.sock.ev.on("messages.delete", async (msgs) => {
|
|
418
|
+
this.logger.info("Msg Deleted : " + JSON.stringify(msgs, null, 2));
|
|
419
|
+
});
|
|
420
|
+
return this.sock;
|
|
421
|
+
}
|
|
422
|
+
catch (error) {
|
|
423
|
+
const msg = `Failed setup WASocket: ${error.message}`;
|
|
424
|
+
this.logger.error(msg);
|
|
425
|
+
throw new AutoWAError(msg);
|
|
426
|
+
}
|
|
504
427
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
428
|
+
async destroy(full) {
|
|
429
|
+
this.logger.info("Destroying...");
|
|
430
|
+
let error = false;
|
|
431
|
+
let msg = "";
|
|
432
|
+
try {
|
|
433
|
+
await this.sock.logout();
|
|
434
|
+
}
|
|
435
|
+
catch (err) {
|
|
436
|
+
msg = `Logout failed: ${err.message}`;
|
|
437
|
+
error = true;
|
|
438
|
+
}
|
|
439
|
+
finally {
|
|
440
|
+
this.sock.end(undefined);
|
|
441
|
+
if (full) {
|
|
442
|
+
const dir = path.resolve(CREDENTIALS.DIR_NAME, this.sessionId + CREDENTIALS.PREFIX);
|
|
443
|
+
if (fs.existsSync(dir)) {
|
|
444
|
+
fs.rmSync(dir, { force: true, recursive: true });
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
this.logger.info("Destroyed!");
|
|
448
|
+
}
|
|
449
|
+
if (error) {
|
|
450
|
+
this.logger.error(msg);
|
|
451
|
+
throw new AutoWAError(msg);
|
|
452
|
+
}
|
|
514
453
|
}
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
const
|
|
518
|
-
|
|
519
|
-
const isRegistered = yield this.isExist({
|
|
520
|
-
from,
|
|
454
|
+
async isExist({ from, isGroup = false }) {
|
|
455
|
+
try {
|
|
456
|
+
const receiver = phoneToJid({
|
|
457
|
+
from: from,
|
|
521
458
|
isGroup,
|
|
522
459
|
});
|
|
523
|
-
if (
|
|
524
|
-
return
|
|
525
|
-
msg: `${oldPhone} is not registered on Whatsapp`,
|
|
526
|
-
};
|
|
460
|
+
if (receiver.includes("@broadcast")) {
|
|
461
|
+
return true;
|
|
527
462
|
}
|
|
463
|
+
else if (!receiver.includes("@g.us")) {
|
|
464
|
+
return Boolean((await this.sock.onWhatsApp(receiver))?.[0]?.exists);
|
|
465
|
+
}
|
|
466
|
+
else {
|
|
467
|
+
return Boolean((await this.sock.groupMetadata(receiver)).id);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
catch (error) {
|
|
471
|
+
const msg = `Failed get exist status: ${error.message}`;
|
|
472
|
+
this.logger.error(msg);
|
|
473
|
+
throw new AutoWAError(msg);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
async downloadMedia(msg, opts, ext) {
|
|
477
|
+
this.logger.debug(JSON.stringify(msg, null, 2));
|
|
478
|
+
const filePath = path.join(process.cwd(), (opts.path || "my_media") + "." + ext);
|
|
479
|
+
const buf = await downloadMediaMessage(msg, "buffer", {});
|
|
480
|
+
if (opts.asBuffer)
|
|
481
|
+
return Promise.resolve(buf);
|
|
482
|
+
fs.writeFileSync(filePath, buf);
|
|
483
|
+
return Promise.resolve(filePath);
|
|
484
|
+
}
|
|
485
|
+
async validateReceiver({ from, isGroup = false }) {
|
|
486
|
+
const oldPhone = from;
|
|
487
|
+
from = phoneToJid({ from, isGroup });
|
|
488
|
+
const isRegistered = await this.isExist({
|
|
489
|
+
from,
|
|
490
|
+
isGroup,
|
|
491
|
+
});
|
|
492
|
+
if (!isRegistered) {
|
|
528
493
|
return {
|
|
529
|
-
|
|
494
|
+
msg: `${oldPhone} is not registered on Whatsapp`,
|
|
530
495
|
};
|
|
496
|
+
}
|
|
497
|
+
return {
|
|
498
|
+
receiver: from,
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
async sendText({ to, text = "", isGroup = false, ...props }) {
|
|
502
|
+
const { receiver, msg } = await this.validateReceiver({
|
|
503
|
+
from: to,
|
|
504
|
+
isGroup,
|
|
505
|
+
});
|
|
506
|
+
if (msg)
|
|
507
|
+
throw new AutoWAError(msg);
|
|
508
|
+
return await this.sock.sendMessage(receiver, {
|
|
509
|
+
text: text,
|
|
510
|
+
mentions: props.mentions,
|
|
511
|
+
}, {
|
|
512
|
+
quoted: props.answering,
|
|
531
513
|
});
|
|
532
514
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
515
|
+
async sendImage({ to, text = "", isGroup = false, media, failMsg, ...props }) {
|
|
516
|
+
if (!media)
|
|
517
|
+
throw new AutoWAError("'media' parameter must be Buffer or String URL");
|
|
518
|
+
const { receiver, msg } = await this.validateReceiver({
|
|
519
|
+
from: to,
|
|
520
|
+
isGroup,
|
|
521
|
+
});
|
|
522
|
+
if (msg)
|
|
523
|
+
throw new AutoWAError(msg);
|
|
524
|
+
try {
|
|
525
|
+
return await this.sock.sendMessage(receiver, {
|
|
526
|
+
image: typeof media == "string"
|
|
527
|
+
? {
|
|
528
|
+
url: media,
|
|
529
|
+
}
|
|
530
|
+
: media,
|
|
531
|
+
caption: text,
|
|
544
532
|
mentions: props.mentions,
|
|
545
533
|
}, {
|
|
546
534
|
quoted: props.answering,
|
|
547
535
|
});
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
const { receiver, msg } = yield this.validateReceiver({
|
|
556
|
-
from: to,
|
|
557
|
-
isGroup,
|
|
536
|
+
}
|
|
537
|
+
catch (error) {
|
|
538
|
+
this.logger.error("Failed send media:" + error.message);
|
|
539
|
+
return await this.sendText({
|
|
540
|
+
to: receiver,
|
|
541
|
+
text: failMsg || "There is error while trying to send the image🥹",
|
|
542
|
+
...props,
|
|
558
543
|
});
|
|
559
|
-
|
|
560
|
-
throw new Error_1.AutoWAError(msg);
|
|
561
|
-
try {
|
|
562
|
-
return yield this.sock.sendMessage(receiver, {
|
|
563
|
-
image: typeof media == "string"
|
|
564
|
-
? {
|
|
565
|
-
url: media,
|
|
566
|
-
}
|
|
567
|
-
: media,
|
|
568
|
-
caption: text,
|
|
569
|
-
mentions: props.mentions,
|
|
570
|
-
}, {
|
|
571
|
-
quoted: props.answering,
|
|
572
|
-
});
|
|
573
|
-
}
|
|
574
|
-
catch (error) {
|
|
575
|
-
this.logger.error("Failed send media:" + error.message);
|
|
576
|
-
return yield this.sendText(Object.assign({ to: receiver, text: failMsg || "There is error while trying to send the image🥹" }, props));
|
|
577
|
-
}
|
|
578
|
-
});
|
|
544
|
+
}
|
|
579
545
|
}
|
|
580
|
-
sendVideo(
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
from: to,
|
|
587
|
-
isGroup,
|
|
588
|
-
});
|
|
589
|
-
if (msg)
|
|
590
|
-
throw new Error_1.AutoWAError(msg);
|
|
591
|
-
try {
|
|
592
|
-
return yield this.sock.sendMessage(receiver, {
|
|
593
|
-
video: typeof media == "string"
|
|
594
|
-
? {
|
|
595
|
-
url: media,
|
|
596
|
-
}
|
|
597
|
-
: media,
|
|
598
|
-
caption: text,
|
|
599
|
-
mentions: props.mentions,
|
|
600
|
-
}, {
|
|
601
|
-
quoted: props.answering,
|
|
602
|
-
});
|
|
603
|
-
}
|
|
604
|
-
catch (error) {
|
|
605
|
-
this.logger.error("Failed send media:" + error.message);
|
|
606
|
-
return yield this.sendText(Object.assign({ to: receiver, text: failMsg || "There is error while trying to send the video🥹" }, props));
|
|
607
|
-
}
|
|
546
|
+
async sendVideo({ to, text = "", isGroup = false, media, failMsg, ...props }) {
|
|
547
|
+
if (!media)
|
|
548
|
+
throw new AutoWAError("'media' parameter must be Buffer or String URL");
|
|
549
|
+
const { receiver, msg } = await this.validateReceiver({
|
|
550
|
+
from: to,
|
|
551
|
+
isGroup,
|
|
608
552
|
});
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
553
|
+
if (msg)
|
|
554
|
+
throw new AutoWAError(msg);
|
|
555
|
+
try {
|
|
556
|
+
return await this.sock.sendMessage(receiver, {
|
|
557
|
+
video: typeof media == "string"
|
|
558
|
+
? {
|
|
559
|
+
url: media,
|
|
560
|
+
}
|
|
561
|
+
: media,
|
|
562
|
+
caption: text,
|
|
563
|
+
mentions: props.mentions,
|
|
564
|
+
}, {
|
|
565
|
+
quoted: props.answering,
|
|
621
566
|
});
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
url: media,
|
|
630
|
-
}
|
|
631
|
-
: media,
|
|
632
|
-
mimetype: mimetype,
|
|
633
|
-
caption: text,
|
|
634
|
-
mentions: props.mentions,
|
|
635
|
-
}, {
|
|
636
|
-
quoted: props.answering,
|
|
637
|
-
});
|
|
638
|
-
}
|
|
639
|
-
catch (error) {
|
|
640
|
-
this.logger.error("Failed send media:" + error.message);
|
|
641
|
-
return yield this.sendText(Object.assign({ to: receiver, text: failMsg || "There is error while trying to send the document" }, props));
|
|
642
|
-
}
|
|
643
|
-
});
|
|
644
|
-
}
|
|
645
|
-
sendAudio(_a) {
|
|
646
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
647
|
-
var { to, isGroup = false, media, voiceNote = false, failMsg } = _a, props = __rest(_a, ["to", "isGroup", "media", "voiceNote", "failMsg"]);
|
|
648
|
-
if (!media)
|
|
649
|
-
throw new Error_1.AutoWAError("'media' parameter must be Buffer or String URL");
|
|
650
|
-
const { receiver, msg } = yield this.validateReceiver({
|
|
651
|
-
from: to,
|
|
652
|
-
isGroup,
|
|
567
|
+
}
|
|
568
|
+
catch (error) {
|
|
569
|
+
this.logger.error("Failed send media:" + error.message);
|
|
570
|
+
return await this.sendText({
|
|
571
|
+
to: receiver,
|
|
572
|
+
text: failMsg || "There is error while trying to send the video🥹",
|
|
573
|
+
...props,
|
|
653
574
|
});
|
|
654
|
-
|
|
655
|
-
throw new Error_1.AutoWAError(msg);
|
|
656
|
-
try {
|
|
657
|
-
return yield this.sock.sendMessage(receiver, {
|
|
658
|
-
audio: typeof media == "string"
|
|
659
|
-
? {
|
|
660
|
-
url: media,
|
|
661
|
-
}
|
|
662
|
-
: media,
|
|
663
|
-
ptt: voiceNote,
|
|
664
|
-
mentions: props.mentions,
|
|
665
|
-
}, {
|
|
666
|
-
quoted: props.answering,
|
|
667
|
-
});
|
|
668
|
-
}
|
|
669
|
-
catch (error) {
|
|
670
|
-
this.logger.error("Failed send media:" + error.message);
|
|
671
|
-
return yield this.sendText(Object.assign({ to: receiver, text: failMsg || "There is error while trying to send the audio🥹" }, props));
|
|
672
|
-
}
|
|
673
|
-
});
|
|
575
|
+
}
|
|
674
576
|
}
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
577
|
+
async sendDocument({ to, text = "", isGroup = false, media, filename, failMsg, ...props }) {
|
|
578
|
+
if (!media)
|
|
579
|
+
throw new AutoWAError("'media' parameter must be Buffer or String URL");
|
|
580
|
+
const mimetype = mime.getType(filename);
|
|
581
|
+
if (!mimetype)
|
|
582
|
+
throw new AutoWAError(`Filename must include valid extension`);
|
|
583
|
+
const { receiver, msg } = await this.validateReceiver({
|
|
584
|
+
from: to,
|
|
585
|
+
isGroup,
|
|
586
|
+
});
|
|
587
|
+
if (msg)
|
|
588
|
+
throw new AutoWAError(msg);
|
|
589
|
+
try {
|
|
590
|
+
return await this.sock.sendMessage(receiver, {
|
|
591
|
+
fileName: filename,
|
|
592
|
+
document: typeof media == "string"
|
|
593
|
+
? {
|
|
594
|
+
url: media,
|
|
595
|
+
}
|
|
596
|
+
: media,
|
|
597
|
+
mimetype: mimetype,
|
|
598
|
+
caption: text,
|
|
599
|
+
mentions: props.mentions,
|
|
600
|
+
}, {
|
|
601
|
+
quoted: props.answering,
|
|
680
602
|
});
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
603
|
+
}
|
|
604
|
+
catch (error) {
|
|
605
|
+
this.logger.error("Failed send media:" + error.message);
|
|
606
|
+
return await this.sendText({
|
|
607
|
+
to: receiver,
|
|
608
|
+
text: failMsg || "There is error while trying to send the document",
|
|
609
|
+
...props,
|
|
688
610
|
});
|
|
689
|
-
}
|
|
611
|
+
}
|
|
690
612
|
}
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
if (msg)
|
|
698
|
-
throw new Error_1.AutoWAError(msg);
|
|
699
|
-
yield this.sock.sendPresenceUpdate("composing", receiver);
|
|
700
|
-
yield callback();
|
|
701
|
-
yield this.sock.sendPresenceUpdate("available", receiver);
|
|
613
|
+
async sendAudio({ to, isGroup = false, media, voiceNote = false, failMsg, ...props }) {
|
|
614
|
+
if (!media)
|
|
615
|
+
throw new AutoWAError("'media' parameter must be Buffer or String URL");
|
|
616
|
+
const { receiver, msg } = await this.validateReceiver({
|
|
617
|
+
from: to,
|
|
618
|
+
isGroup,
|
|
702
619
|
});
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
620
|
+
if (msg)
|
|
621
|
+
throw new AutoWAError(msg);
|
|
622
|
+
try {
|
|
623
|
+
return await this.sock.sendMessage(receiver, {
|
|
624
|
+
audio: typeof media == "string"
|
|
625
|
+
? {
|
|
626
|
+
url: media,
|
|
627
|
+
}
|
|
628
|
+
: media,
|
|
629
|
+
ptt: voiceNote,
|
|
630
|
+
mentions: props.mentions,
|
|
631
|
+
}, {
|
|
632
|
+
quoted: props.answering,
|
|
709
633
|
});
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
634
|
+
}
|
|
635
|
+
catch (error) {
|
|
636
|
+
this.logger.error("Failed send media:" + error.message);
|
|
637
|
+
return await this.sendText({
|
|
638
|
+
to: receiver,
|
|
639
|
+
text: failMsg || "There is error while trying to send the audio🥹",
|
|
640
|
+
...props,
|
|
641
|
+
});
|
|
642
|
+
}
|
|
716
643
|
}
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
644
|
+
async sendReaction({ to, text, isGroup = false, answering }) {
|
|
645
|
+
const { receiver, msg } = await this.validateReceiver({
|
|
646
|
+
from: to,
|
|
647
|
+
isGroup,
|
|
720
648
|
});
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
return
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
});
|
|
729
|
-
if (msg)
|
|
730
|
-
throw new Error_1.AutoWAError(msg);
|
|
731
|
-
if (!media && !sticker && !hasMedia)
|
|
732
|
-
throw new Error_1.AutoWAError("'media' or 'sticker' parameter must be filled");
|
|
733
|
-
if (!sticker) {
|
|
734
|
-
if (!(typeof media === "string" || Buffer.isBuffer(media)) && !hasMedia) {
|
|
735
|
-
throw new Error_1.AutoWAError("'media' parameter must be string or buffer");
|
|
736
|
-
}
|
|
737
|
-
const stickerProps = Object.assign(Object.assign(Object.assign({}, this.defaultStickerProps), { media }), props);
|
|
738
|
-
sticker = yield (0, make_stiker_1.makeWebpBuffer)(stickerProps);
|
|
739
|
-
}
|
|
740
|
-
if (!sticker || !Buffer.isBuffer(sticker)) {
|
|
741
|
-
return yield this.sendText(Object.assign({ to, text: failMsg || "There is error while creating the sticker🥹", isGroup }, props));
|
|
742
|
-
}
|
|
743
|
-
try {
|
|
744
|
-
return yield this.sock.sendMessage(receiver, {
|
|
745
|
-
sticker,
|
|
746
|
-
mentions: props.mentions,
|
|
747
|
-
}, {
|
|
748
|
-
quoted: props.answering,
|
|
749
|
-
});
|
|
750
|
-
}
|
|
751
|
-
catch (error) {
|
|
752
|
-
this.logger.error("Failed send media:" + error.message);
|
|
753
|
-
return yield this.sendText(Object.assign({ to: receiver, text: failMsg || "There is error while trying to send the sticker🥹" }, props));
|
|
754
|
-
}
|
|
649
|
+
if (msg)
|
|
650
|
+
throw new AutoWAError(msg);
|
|
651
|
+
return await this.sock.sendMessage(receiver, {
|
|
652
|
+
react: {
|
|
653
|
+
text,
|
|
654
|
+
key: answering.key,
|
|
655
|
+
},
|
|
755
656
|
});
|
|
756
657
|
}
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
658
|
+
async sendTyping({ to, callback, isGroup = false }) {
|
|
659
|
+
const { receiver, msg } = await this.validateReceiver({
|
|
660
|
+
from: to,
|
|
661
|
+
isGroup,
|
|
662
|
+
});
|
|
663
|
+
if (msg)
|
|
664
|
+
throw new AutoWAError(msg);
|
|
665
|
+
await this.sock.sendPresenceUpdate("composing", receiver);
|
|
666
|
+
await callback();
|
|
667
|
+
await this.sock.sendPresenceUpdate("available", receiver);
|
|
668
|
+
}
|
|
669
|
+
async sendRecording({ to, callback, isGroup = false }) {
|
|
670
|
+
const { receiver, msg } = await this.validateReceiver({
|
|
671
|
+
from: to,
|
|
672
|
+
isGroup,
|
|
673
|
+
});
|
|
674
|
+
if (msg)
|
|
675
|
+
throw new AutoWAError(msg);
|
|
676
|
+
await this.sock.sendPresenceUpdate("recording", receiver);
|
|
677
|
+
await callback();
|
|
678
|
+
await this.sock.sendPresenceUpdate("available", receiver);
|
|
679
|
+
}
|
|
680
|
+
async readMessage(msgs) {
|
|
681
|
+
await this.sock.readMessages(msgs.map((msg) => msg.key));
|
|
682
|
+
}
|
|
683
|
+
async sendSticker({ to, isGroup, sticker, media, failMsg, hasMedia, ...props }) {
|
|
684
|
+
const { receiver, msg } = await this.validateReceiver({
|
|
685
|
+
from: to,
|
|
686
|
+
isGroup,
|
|
687
|
+
});
|
|
688
|
+
if (msg)
|
|
689
|
+
throw new AutoWAError(msg);
|
|
690
|
+
if (!media && !sticker && !hasMedia)
|
|
691
|
+
throw new AutoWAError("'media' or 'sticker' parameter must be filled");
|
|
692
|
+
if (!sticker) {
|
|
693
|
+
if (!(typeof media === "string" || Buffer.isBuffer(media)) && !hasMedia) {
|
|
694
|
+
throw new AutoWAError("'media' parameter must be string or buffer");
|
|
695
|
+
}
|
|
696
|
+
const stickerProps = {
|
|
697
|
+
...this.defaultStickerProps,
|
|
698
|
+
media,
|
|
699
|
+
...props,
|
|
700
|
+
};
|
|
701
|
+
sticker = await makeWebpBuffer(stickerProps);
|
|
702
|
+
}
|
|
703
|
+
if (!sticker || !Buffer.isBuffer(sticker)) {
|
|
704
|
+
return await this.sendText({
|
|
705
|
+
to,
|
|
706
|
+
text: failMsg || "There is error while creating the sticker🥹",
|
|
762
707
|
isGroup,
|
|
708
|
+
...props,
|
|
763
709
|
});
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
});
|
|
772
|
-
}
|
|
773
|
-
catch (error) {
|
|
774
|
-
this.logger.error("Failed forward a message!");
|
|
775
|
-
}
|
|
776
|
-
});
|
|
777
|
-
}
|
|
778
|
-
getProfileInfo(target) {
|
|
779
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
780
|
-
const { receiver, msg } = yield this.validateReceiver({
|
|
781
|
-
from: target,
|
|
710
|
+
}
|
|
711
|
+
try {
|
|
712
|
+
return await this.sock.sendMessage(receiver, {
|
|
713
|
+
sticker,
|
|
714
|
+
mentions: props.mentions,
|
|
715
|
+
}, {
|
|
716
|
+
quoted: props.answering,
|
|
782
717
|
});
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
return {
|
|
791
|
-
profilePictureUrl: profilePictureUrl.status === "fulfilled" ? profilePictureUrl.value || null : null,
|
|
792
|
-
status: status.status === "fulfilled" ? status.value || null : null,
|
|
793
|
-
};
|
|
794
|
-
}
|
|
795
|
-
catch (error) {
|
|
796
|
-
const msg = `Failed get profile info: ${error.message}`;
|
|
797
|
-
this.logger.error(msg);
|
|
798
|
-
}
|
|
799
|
-
return null;
|
|
800
|
-
});
|
|
801
|
-
}
|
|
802
|
-
getGroupInfo(target) {
|
|
803
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
804
|
-
const { receiver, msg } = yield this.validateReceiver({
|
|
805
|
-
from: target,
|
|
806
|
-
isGroup: true,
|
|
718
|
+
}
|
|
719
|
+
catch (error) {
|
|
720
|
+
this.logger.error("Failed send media:" + error.message);
|
|
721
|
+
return await this.sendText({
|
|
722
|
+
to: receiver,
|
|
723
|
+
text: failMsg || "There is error while trying to send the sticker🥹",
|
|
724
|
+
...props,
|
|
807
725
|
});
|
|
808
|
-
|
|
809
|
-
throw new Error_1.AutoWAError(msg);
|
|
810
|
-
try {
|
|
811
|
-
return yield this.sock.groupMetadata(receiver);
|
|
812
|
-
}
|
|
813
|
-
catch (error) {
|
|
814
|
-
const msg = `Failed get group info: ${error.message}`;
|
|
815
|
-
this.logger.error(msg);
|
|
816
|
-
}
|
|
817
|
-
return null;
|
|
818
|
-
});
|
|
726
|
+
}
|
|
819
727
|
}
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
isGroup: true,
|
|
825
|
-
});
|
|
826
|
-
if (msg)
|
|
827
|
-
throw new Error_1.AutoWAError(msg);
|
|
828
|
-
participants = participants.map((d) => (0, helper_1.phoneToJid)({ from: d }));
|
|
829
|
-
return yield this.sock.groupParticipantsUpdate(group, participants, "add");
|
|
728
|
+
async forwardMessage({ to, msg, isGroup = false, ...props }) {
|
|
729
|
+
const { receiver, msg: err_msg } = await this.validateReceiver({
|
|
730
|
+
from: to,
|
|
731
|
+
isGroup,
|
|
830
732
|
});
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
733
|
+
if (err_msg)
|
|
734
|
+
throw new AutoWAError(err_msg);
|
|
735
|
+
try {
|
|
736
|
+
return await this.sock.sendMessage(receiver, {
|
|
737
|
+
forward: msg,
|
|
738
|
+
mentions: props.mentions,
|
|
739
|
+
force: true,
|
|
837
740
|
});
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
741
|
+
}
|
|
742
|
+
catch (error) {
|
|
743
|
+
this.logger.error("Failed forward a message!");
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
async getProfileInfo(target) {
|
|
747
|
+
const { receiver, msg } = await this.validateReceiver({
|
|
748
|
+
from: target,
|
|
842
749
|
});
|
|
750
|
+
if (msg)
|
|
751
|
+
throw new AutoWAError(msg);
|
|
752
|
+
try {
|
|
753
|
+
const [profilePictureUrl, status] = await Promise.allSettled([
|
|
754
|
+
this.sock.profilePictureUrl(receiver, "image", 5000),
|
|
755
|
+
this.sock.fetchStatus(receiver),
|
|
756
|
+
]);
|
|
757
|
+
return {
|
|
758
|
+
profilePictureUrl: profilePictureUrl.status === "fulfilled" ? profilePictureUrl.value || null : null,
|
|
759
|
+
status: status.status === "fulfilled" ? status.value || null : null,
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
catch (error) {
|
|
763
|
+
const msg = `Failed get profile info: ${error.message}`;
|
|
764
|
+
this.logger.error(msg);
|
|
765
|
+
}
|
|
766
|
+
return null;
|
|
843
767
|
}
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
isGroup: true,
|
|
849
|
-
});
|
|
850
|
-
if (msg)
|
|
851
|
-
throw new Error_1.AutoWAError(msg);
|
|
852
|
-
participants = participants.map((d) => (0, helper_1.phoneToJid)({ from: d }));
|
|
853
|
-
return yield this.sock.groupParticipantsUpdate(group, participants, "promote");
|
|
768
|
+
async getGroupInfo(target) {
|
|
769
|
+
const { receiver, msg } = await this.validateReceiver({
|
|
770
|
+
from: target,
|
|
771
|
+
isGroup: true,
|
|
854
772
|
});
|
|
773
|
+
if (msg)
|
|
774
|
+
throw new AutoWAError(msg);
|
|
775
|
+
try {
|
|
776
|
+
return await this.sock.groupMetadata(receiver);
|
|
777
|
+
}
|
|
778
|
+
catch (error) {
|
|
779
|
+
const msg = `Failed get group info: ${error.message}`;
|
|
780
|
+
this.logger.error(msg);
|
|
781
|
+
}
|
|
782
|
+
return null;
|
|
855
783
|
}
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
784
|
+
async addMemberToGroup({ participants, to }) {
|
|
785
|
+
const { receiver: group, msg } = await this.validateReceiver({
|
|
786
|
+
from: to,
|
|
787
|
+
isGroup: true,
|
|
788
|
+
});
|
|
789
|
+
if (msg)
|
|
790
|
+
throw new AutoWAError(msg);
|
|
791
|
+
participants = participants.map((d) => phoneToJid({ from: d }));
|
|
792
|
+
return await this.sock.groupParticipantsUpdate(group, participants, "add");
|
|
793
|
+
}
|
|
794
|
+
async removeMemberFromGroup({ participants, to }) {
|
|
795
|
+
const { receiver: group, msg } = await this.validateReceiver({
|
|
796
|
+
from: to,
|
|
797
|
+
isGroup: true,
|
|
798
|
+
});
|
|
799
|
+
if (msg)
|
|
800
|
+
throw new AutoWAError(msg);
|
|
801
|
+
participants = participants.map((d) => phoneToJid({ from: d }));
|
|
802
|
+
return await this.sock.groupParticipantsUpdate(group, participants, "remove");
|
|
803
|
+
}
|
|
804
|
+
async promoteMemberGroup({ participants, to }) {
|
|
805
|
+
const { receiver: group, msg } = await this.validateReceiver({
|
|
806
|
+
from: to,
|
|
807
|
+
isGroup: true,
|
|
808
|
+
});
|
|
809
|
+
if (msg)
|
|
810
|
+
throw new AutoWAError(msg);
|
|
811
|
+
participants = participants.map((d) => phoneToJid({ from: d }));
|
|
812
|
+
return await this.sock.groupParticipantsUpdate(group, participants, "promote");
|
|
813
|
+
}
|
|
814
|
+
async demoteMemberGroup({ participants, to }) {
|
|
815
|
+
const { receiver: group, msg } = await this.validateReceiver({
|
|
816
|
+
from: to,
|
|
817
|
+
isGroup: true,
|
|
866
818
|
});
|
|
819
|
+
if (msg)
|
|
820
|
+
throw new AutoWAError(msg);
|
|
821
|
+
participants = participants.map((d) => phoneToJid({ from: d }));
|
|
822
|
+
return await this.sock.groupParticipantsUpdate(group, participants, "demote");
|
|
867
823
|
}
|
|
868
824
|
on(event, listener) {
|
|
869
825
|
this.events.on(event, listener);
|
|
@@ -881,4 +837,3 @@ class AutoWA {
|
|
|
881
837
|
this.events.removeAllListeners(event);
|
|
882
838
|
}
|
|
883
839
|
}
|
|
884
|
-
exports.AutoWA = AutoWA;
|