whatsauto.js 2.0.1 โ†’ 2.0.2

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 CHANGED
@@ -1,171 +1,210 @@
1
- # WhatsAuto.js - Easy WhatsApp Automation
1
+ # WhatsAuto.js - Lightweight WhatsApp Automation
2
2
 
3
- An easy-to-use library for creating WhatsApp Automation App.
3
+ **WhatsAuto.js** is a powerful, lightweight, and easy-to-use library for building WhatsApp automation applications using Node.js. Built on top of the robust [Baileys](https://github.com/WhiskeySockets/Baileys) library, it provides a high-level, Object-Oriented interface to interact with WhatsApp without the overhead of browser automation tools like Selenium or Puppeteer.
4
4
 
5
- Stand above [Baileys](https://github.com/WhiskeySockets/Baileys) Library. This library will be lightweight library for WhatsApp. Not require Selenium or any other browser.
5
+ ## โœจ Features
6
6
 
7
- ## ๐Ÿš€ Installation
7
+ - **๐Ÿš€ Lightweight & Fast**: Runs directly on Node.js using WebSocket connections. No browser required.
8
+ - **๐Ÿ”‘ Flexible Authentication**: Supports both **QR Code** scanning and **Pairing Code** (phone number) login methods.
9
+ - **๐Ÿ“ฆ Object-Oriented Design**: Clean class-based architecture (`AutoWA`) making it easy to manage sessions and logic.
10
+ - **๐Ÿ’ฌ Rich Message Support**: Send and receive Text, Images, Videos, Audio (Voice Notes), Documents, and Stickers easily.
11
+ - **โšก Event-Driven**: Listen to real-time events like `message`, `group-participants.update`, `connection.update`, etc.
12
+ - **๐Ÿ‘ฅ Group Management**: Create tools to manage groups (add, remove, promote, demote members).
13
+ - **๐Ÿ› ๏ธ Developer Friendly**: Written in TypeScript with full type definitions included.
8
14
 
9
- Install package using npm
15
+ ---
10
16
 
11
- ```
12
- npm i whatsauto.js@latest
13
- ```
17
+ ## ๐Ÿ“ฅ Installation
14
18
 
15
- Then import the library
19
+ Install the package via npm:
16
20
 
17
- ```ts
18
- import AutoWA from "whatsauto.js";
19
- // or
20
- import { AutoWA } from "whatsauto.js";
21
+ ```bash
22
+ npm install whatsauto.js
21
23
  ```
22
24
 
23
- ## ๐Ÿ—’๏ธ Insight
25
+ ---
24
26
 
25
- WhatsAuto.js is designed to simplify the process of automating WhatsApp tasks. By leveraging the power of the Baileys library, it provides a robust and efficient way to interact with WhatsApp without the need for browser automation tools like Selenium.
27
+ ## ๐Ÿš€ Quick Start
26
28
 
27
- This makes it a lightweight and efficient solution for developers looking to integrate WhatsApp functionalities into their applications.
29
+ ### 1. Initialize a Client
28
30
 
29
- Additionally, WhatsAuto.js uses the Object-Oriented Programming (OOP) paradigm, making it an excellent choice for developers who prefer or are accustomed to OOP. This approach enhances code reusability, scalability, and maintainability, which are essential for long-term project development.
31
+ You can start a session using either a QR Code (default) or a Pairing Code.
30
32
 
31
- ## ๐Ÿชง Examples
33
+ **Option A: Using QR Code**
34
+ ```ts
35
+ import { AutoWA } from "whatsauto.js";
32
36
 
33
- ### Make WA Session / Client
37
+ const client = new AutoWA("my-session", {
38
+ printQR: true, // Prints QR code in the terminal
39
+ logging: true, // Enable logs
40
+ });
34
41
 
35
- ```ts
36
- import AutoWA from "whatsauto.js";
42
+ client.initialize();
37
43
 
38
- // using QR (default)
39
- const autoWA = new AutoWA("session_name", { printQR: true });
40
- // or
41
- const autoWA = new AutoWA("session_name");
42
- // or, using pair code (experimental)
43
- const autoWA = new AutoWA("session_name", { phoneNumber: "628xxxx" });
44
+ client.on("connected", () => {
45
+ console.log("โœ… Client is ready!");
46
+ });
47
+ ```
44
48
 
45
- autoWA.on("connected", () => {
46
- console.log("Client Ready!");
49
+ **Option B: Using Pairing Code**
50
+ ```ts
51
+ import { AutoWA } from "whatsauto.js";
52
+
53
+ const client = new AutoWA("my-session", {
54
+ phoneNumber: "6281234567890", // Your phone number (Country Code + Number)
47
55
  });
48
56
 
49
- autoWA.on("message", async (msg) => {
50
- console.log(msg.text);
57
+ client.initialize();
58
+
59
+ client.on("pairing-code", (code) => {
60
+ console.log(`๐Ÿ”‘ Pairing Code: ${code}`);
51
61
  });
52
62
 
53
- // initialize session
54
- await autoWA.initialize();
63
+ client.on("connected", () => {
64
+ console.log("โœ… Client is ready!");
65
+ });
55
66
  ```
56
67
 
57
- ### Session Parameters
68
+ ### 2. Handling Messages
69
+
70
+ Listen to the `message` event to handle incoming messages. The `msg` object comes with built-in helper methods!
58
71
 
59
72
  ```ts
60
- {
61
- /**
62
- * Print logs into Terminal
63
- */
64
- logging?: boolean; // true (default), false
65
- /**
66
- * Print QR Code into Terminal
67
- */
68
- printQR?: boolean; // true (default), false
69
- /**
70
- * Phone number for session with pairing code
71
- */
72
- phoneNumber?: string; // 62822xxxxx (62 is your country code)
73
- }
74
- ```
73
+ client.on("message", async (msg) => {
74
+ if (msg.key.fromMe) return; // Ignore messages from yourself
75
75
 
76
- ### IWAutoMessage APIs
76
+ console.log(`๐Ÿ“ฉ New Message from ${msg.from}: ${msg.text}`);
77
77
 
78
- ```ts
79
- autoWA.on("message-received", async (msg) => {
80
- // read this message
81
- await msg.read();
82
-
83
- if (msg.text == "react")
84
- // react this message
85
- await msg.react("๐Ÿพ");
86
-
87
- if (msg.text == "text")
88
- // reply this message with text
89
- await msg.replyWithText("Hello!");
90
-
91
- if (msg.text == "image")
92
- // reply this message with image
93
- await msg.replyWithImage("https://picsum.photos/536/354");
94
-
95
- if (msg.text == "video")
96
- // reply this message with video
97
- await msg.replyWithVideo(
98
- "https://github.com/rafaelreis-hotmart/Audio-Sample-files/raw/master/sample.mp4"
99
- );
100
-
101
- if (msg.text == "audio")
102
- // reply this message with audio
103
- await msg.replyWithAudio(
104
- "https://github.com/rafaelreis-hotmart/Audio-Sample-files/raw/master/sample.mp3"
105
- );
106
-
107
- if (msg.text == "sticker") {
108
- // convert this message to sticker buffer (if its has media)
109
- const [sticker, hasMedia] = await msg.toSticker();
110
- // or
111
- // const sticker = await msg.toSticker({ pack: "whatsauto.js", author: "freack21" });
112
- if (hasMedia) {
113
- // reply this message with audio
114
- await msg.replyWithSticker(sticker);
115
- }
78
+ if (msg.text === "!ping") {
79
+ // Reply directly using the message object
80
+ await msg.replyWithText("Pong! ๐Ÿ“");
116
81
  }
117
82
 
118
- if (msg.text == "typing")
119
- // reply this message with typing presence
120
- await msg.replyWithTyping(async () => {
121
- await msg.replyWithText("This is typing"); // action to take while typing
122
- });
123
-
124
- if (msg.text == "recording")
125
- // reply this message with recording presence
126
- await msg.replyWithRecording(async () => {
127
- await msg.replyWithText("This is recording"); // action to take while recording
128
- });
83
+ if (msg.text === "!sticker" && msg.hasMedia) {
84
+ // Convert received image/video to sticker
85
+ const [stickerBuffer] = await msg.toSticker({ pack: "MyBot", author: "Me" });
86
+ if (stickerBuffer) {
87
+ await msg.replyWithSticker(stickerBuffer);
88
+ }
89
+ }
129
90
  });
130
91
  ```
131
92
 
132
- ### AutoWA Events
93
+ ---
94
+
95
+ ## ๐Ÿ“š Core Concepts & API
96
+
97
+ ### `AutoWA` Class
98
+ The main entry point for the library.
99
+
100
+ **Constructor**
101
+ `new AutoWA(sessionId: string, options?: IWAutoSessionConfig)`
102
+
103
+ - `sessionId`: Unique identifier for the session (auth credentials will be saved under this name).
104
+ - `options`:
105
+ - `printQR`: (boolean) Auto-print QR in terminal.
106
+ - `phoneNumber`: (string) Use pairing code with this number.
107
+ - `logging`: (boolean) Enable/disable console logs.
108
+
109
+ **Main Methods**
110
+ | Method | Description |
111
+ | :--- | :--- |
112
+ | `initialize()` | Starts the WhatsApp connection. |
113
+ | `destroy(full?)` | Stops the session. If `full` is true, deletes session files. |
114
+ | `sendText({ to, text })` | Sends a text message. |
115
+ | `sendImage({ to, media, text })` | Sends an image (URL or Buffer). |
116
+ | `sendVideo({ to, media, text })` | Sends a video. |
117
+ | `sendAudio({ to, media, voiceNote })` | Sends audio. Set `voiceNote: true` for PTT (Push-to-Talk). |
118
+ | `sendDocument({ to, media, filename })` | Sends a file/document. |
119
+ | `sendSticker({ to, sticker })` | Sends a sticker (Buffer). |
120
+ | `getProfileInfo(jid)` | Get status and profile picture of a user. |
121
+ | `getGroupInfo(jid)` | Get group metadata (participants, description, etc.). |
122
+
123
+ **Group Management Methods**
124
+ - `addMemberToGroup({ to, participants })`
125
+ - `removeMemberFromGroup({ to, participants })`
126
+ - `promoteMemberGroup({ to, participants })`
127
+ - `demoteMemberGroup({ to, participants })`
128
+
129
+ ---
130
+
131
+ ### `IWAutoMessage` Object
132
+ When an event triggers, you receive an `IWAutoMessage` object. It wraps the raw Baileys message with useful properties and methods.
133
+
134
+ **Properties**
135
+ - `from`: Sender's JID.
136
+ - `text`: Message content (Text/Caption).
137
+ - `hasMedia`: Boolean, true if message contains media.
138
+ - `mediaType`: 'image', 'video', 'audio', etc.
139
+ - `isGroup`: Boolean.
140
+ - `isStory`: Boolean.
141
+ - `quotedMessage`: The message this message is replying to (if any).
142
+
143
+ **Helper Methods (Context-Aware)**
144
+ These methods automatically reply to the current message (quote it).
145
+
146
+ - `msg.replyWithText("Hello")`
147
+ - `msg.replyWithImage("http://...", { text: "Caption" })`
148
+ - `msg.replyWithSticker(buffer)`
149
+ - `msg.react("โค๏ธ")`
150
+ - `msg.read()` - Mark as read.
151
+ - `msg.downloadMedia()` - Downloads media to disk or buffer.
152
+ - `msg.toSticker()` - Converts the message's media to a sticker buffer.
153
+
154
+ ---
155
+
156
+ ## โšก Events (`client.on`)
157
+
158
+ | Event Name | Description |
159
+ | :--- | :--- |
160
+ | `qr` | Emitted when a new QR code is generated. Access the QR string as the first argument. |
161
+ | `pairing-code` | Emitted when a pairing code is requested. |
162
+ | `connecting` | Connection is being established. |
163
+ | `connected` | Client is successfully connected to WhatsApp. |
164
+ | `disconnected` | Client disconnected. |
165
+ | `message` | Emitted for **ALL** incoming messages (private, group, status). |
166
+ | `group-message` | Emitted only for group messages. |
167
+ | `private-message` | Emitted only for private chats. |
168
+ | `message-deleted` | Emitted when a message is deleted (Revoke). |
169
+ | `group-participants.update`| Emitted when members join, leave, or are promoted/demoted in a group. |
170
+
171
+ ---
172
+
173
+ ## ๐Ÿ› ๏ธ Advanced Usage
174
+
175
+ ### Handling Media
176
+ Downloading media from a message is simple:
177
+
178
+ ```ts
179
+ client.on("message", async (msg) => {
180
+ if (msg.hasMedia) {
181
+ // defaults: saves to 'my_media.{ext}' in current dir
182
+ const filePath = await msg.downloadMedia();
183
+ console.log(`Media saved at: ${filePath}`);
184
+
185
+ // OR get as buffer
186
+ // const buffer = await msg.downloadMedia({ asBuffer: true });
187
+ }
188
+ });
189
+ ```
133
190
 
191
+ ### Group Member Updates
192
+ Welcome new members:
134
193
  ```ts
135
- "connecting", // => passing null;
136
- "qr", // => passing string;
137
- "pairing-code", // => passing string;
138
- "connected", // => passing null;
139
- "disconnected", // => passing null;
140
-
141
- "message", // => passing IWAutoMessage;
142
- "group-message", // => passing IWAutoMessage;
143
- "private-message", // => passing IWAutoMessage;
144
- "message-received", // => passing IWAutoMessage;
145
- "group-message-received", // => passing IWAutoMessage;
146
- "private-message-received", // => passing IWAutoMessage;
147
- "message-sent", // => passing IWAutoMessage;
148
- "group-message-sent", // => passing IWAutoMessage;
149
- "private-message-sent", // => passing IWAutoMessage;
150
- "story", // => passing IWAutoMessage;
151
- "story-received", // => passing IWAutoMessage;
152
- "story-sent", // => passing IWAutoMessage;
153
- "reaction", // => passing IWAutoMessage;
154
- "reaction-received", // => passing IWAutoMessage;
155
- "reaction-sent", // => passing IWAutoMessage;
156
- "group-reaction", // => passing IWAutoMessage;
157
- "group-reaction-received", // => passing IWAutoMessage;
158
- "group-reaction-sent", // => passing IWAutoMessage;
159
- "private-reaction", // => passing IWAutoMessage;
160
- "private-reaction-received", // => passing IWAutoMessage;
161
- "private-reaction-sent", // => passing IWAutoMessage;
162
-
163
- "message-updated", // => passing WAutoMessageUpdated;
164
- "group-member-update", // => passing IGroupMemberUpdate;
165
-
166
- "message-deleted", // => passing IWAutoDeleteMessage;
194
+ client.on("group-member-update", async (update) => {
195
+ if (update.action === "add") {
196
+ // update.id = Group JID
197
+ // update.participants = Array of new members
198
+
199
+ // You can reply directly to the update event!
200
+ await update.replyWithText(`Welcome to the group! ๐Ÿ‘‹`);
201
+ }
202
+ });
167
203
  ```
168
204
 
169
- ## ๐Ÿงพ Disclaimer
205
+ ---
206
+
207
+ ## ๐Ÿ“„ License
170
208
 
171
- This library is for educational and research purposes only. Use it responsibly and at your own risk.
209
+ This library is essentially a wrapper around Baileys and is provided for educational purposes. Use responsibly.
210
+ ISC License.
@@ -1,5 +1,8 @@
1
1
  import { AutoWA } from "./AutoWA.js";
2
2
  export * from "./AutoWA.js";
3
3
  export declare const sessions: Map<string, AutoWA>;
4
+ export declare const session: (sessionId: string) => AutoWA;
5
+ export declare const sessionsList: () => string[];
6
+ export declare function loadSessions(): Promise<void>;
4
7
  export default AutoWA;
5
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/WhatsApp/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,cAAc,aAAa,CAAC;AAE5B,eAAO,MAAM,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAa,CAAC;AAEvD,eAAe,MAAM,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/WhatsApp/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,cAAc,aAAa,CAAC;AAE5B,eAAO,MAAM,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAa,CAAC;AAEvD,eAAO,MAAM,OAAO,cAAe,MAAM,WAA4B,CAAC;AAEtE,eAAO,MAAM,YAAY,gBAAoC,CAAC;AAE9D,wBAAsB,YAAY,kBAajC;AAED,eAAe,MAAM,CAAC"}
@@ -1,4 +1,21 @@
1
+ import path from "path";
2
+ import fs from "fs";
1
3
  import { AutoWA } from "./AutoWA.js";
4
+ import { CREDENTIALS } from "../Defaults/index.js";
2
5
  export * from "./AutoWA.js";
3
6
  export const sessions = new Map();
7
+ export const session = (sessionId) => sessions.get(sessionId);
8
+ export const sessionsList = () => Array.from(sessions.keys());
9
+ export async function loadSessions() {
10
+ const dir = path.resolve(CREDENTIALS.DIR_NAME);
11
+ if (!fs.existsSync(dir))
12
+ return;
13
+ const files = fs.readdirSync(dir);
14
+ for (const file of files) {
15
+ const sessionId = file.replace(CREDENTIALS.PREFIX, "");
16
+ const client = new AutoWA(sessionId);
17
+ await client.initialize();
18
+ sessions.set(sessionId, client);
19
+ }
20
+ }
4
21
  export default AutoWA;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "whatsauto.js",
3
3
  "type": "module",
4
- "version": "2.0.1",
4
+ "version": "2.0.2",
5
5
  "description": "Easy WhatsApp Automation with Session",
6
6
  "files": [
7
7
  "dist/**/*"