-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwagate-client.ts
68 lines (58 loc) · 1.63 KB
/
wagate-client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import qrcode from "qrcode-terminal";
import {
Client,
ClientOptions,
LocalAuth,
MessageMedia,
} from "whatsapp-web.js";
import { Helper } from "../utils/helper.util";
import logger from "../utils/log.util";
const CLIENT_OPTIONS: ClientOptions = {
authStrategy: new LocalAuth(),
webVersion: "2.2323.4",
webVersionCache: {
type: "remote",
remotePath:
"https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/{version}.html",
},
puppeteer: {
args: ["--no-sandbox", "--disable-setuid-sandbox"],
},
};
export class WagateClient {
constructor(
private client = new Client(CLIENT_OPTIONS),
private helper = new Helper()
) {}
async init() {
this.client.on("qr", (qr) => {
qrcode.generate(qr, { small: true });
});
this.client.on("ready", () => {
logger.info("The bot is ready");
});
await this.client.initialize();
this.setupProfile();
}
setupProfile() {
if (process.env.NODE_ENV == "production") {
const media = MessageMedia.fromFilePath("./logo.jpg");
this.client.setProfilePicture(media);
this.client.setDisplayName(process.env.DISPLAY_NAME || "");
}
}
async sendToAdmin(msg: string) {
this.sendMsg(msg, process.env.ADMIN_NUMBER || "");
}
async sendMsg(msg: string, to: string) {
await this.helper.delay();
this.client.sendMessage(`${to}@c.us`, msg);
}
async sendFile(msg: string = "", to: string, filePath: string) {
await this.helper.delay();
const messageMedia = MessageMedia.fromFilePath(filePath);
this.client.sendMessage(`${to}@c.us`, messageMedia, {
caption: msg,
});
}
}