Skip to content

Commit e19afb8

Browse files
authored
Merge pull request #18 from mimamch/dev-3.3.0
v.3.3.0
2 parents 39061ca + 34caa4b commit e19afb8

File tree

7 files changed

+158
-10
lines changed

7 files changed

+158
-10
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wa-multi-session",
3-
"version": "3.2.1",
3+
"version": "3.3.0",
44
"description": "Multi Session Whatsapp Library",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

readme.md

+23-3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ const send = await whatsapp.sendDocument({
110110
});
111111
```
112112

113+
Send Voice Note
114+
115+
```ts
116+
const filename = "myaudio.mp3";
117+
const audio = fs.readFileSync(filename); // return Buffer
118+
const send = await whatsapp.sendVoiceNote({
119+
sessionId: "session1",
120+
to: "6281234567890",
121+
media: audio,
122+
});
123+
```
124+
113125
Read a Message
114126

115127
```ts
@@ -211,15 +223,23 @@ whatsapp.setCredentialsDir("my_custom_dir");
211223

212224
## Change Log
213225

214-
v3.2.1 July 2023 (LATEST)
226+
### v3.3 September 2023 (LATEST)
227+
228+
What's New:
229+
230+
- Send Voice Note
231+
- Send Sticker
232+
- onMessageUpdate (message ack status)
233+
234+
### v3.2.1 July 2023
215235

216236
- Add error class named: WhatsappError
217237

218-
v3.1.2 July 2023
238+
### v3.1.2 July 2023
219239

220240
- Add send document message
221241

222-
v3.0.0 June 2023
242+
### v3.0.0 June 2023
223243

224244
- Fix Logout Issue
225245
- Switching into [@whiskeysockets/baileys](https://github.com/WhiskeySockets/Baileys)

src/Defaults/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export enum CALLBACK_KEY {
99
ON_CONNECTED = "on-connected",
1010
ON_DISCONNECTED = "on-disconnected",
1111
ON_CONNECTING = "on-connecting",
12+
ON_MESSAGE_UPDATED = "on-message-updated",
1213
}
1314

1415
export abstract class Messages {

src/Messaging/index.ts

+87-4
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ export const sendDocument = async ({
127127
filename,
128128
...props
129129
}: SendMediaTypes & {
130-
media: Buffer;
131130
filename: string;
132131
}): Promise<proto.WebMessageInfo | undefined> => {
133132
const session = getSession(sessionId);
@@ -142,8 +141,8 @@ export const sendDocument = async ({
142141
if (!isRegistered) {
143142
throw new WhatsappError(`${oldPhone} is not registered on Whatsapp`);
144143
}
145-
if (!media || !Buffer.isBuffer(media)) {
146-
throw new WhatsappError(`Media File must be Buffer`);
144+
if (!media) {
145+
throw new WhatsappError(`Invalid Media`);
147146
}
148147

149148
const mimetype = mime.getType(filename);
@@ -155,7 +154,12 @@ export const sendDocument = async ({
155154
to,
156155
{
157156
fileName: filename,
158-
document: media,
157+
document:
158+
typeof media == "string"
159+
? {
160+
url: media,
161+
}
162+
: media,
159163
mimetype: mimetype,
160164
caption: text,
161165
},
@@ -165,6 +169,85 @@ export const sendDocument = async ({
165169
);
166170
};
167171

172+
export const sendVoiceNote = async ({
173+
sessionId,
174+
to,
175+
isGroup = false,
176+
media,
177+
...props
178+
}: Omit<SendMediaTypes, "text">): Promise<proto.WebMessageInfo | undefined> => {
179+
const session = getSession(sessionId);
180+
if (!session) throw new WhatsappError(Messages.sessionNotFound(sessionId));
181+
const oldPhone = to;
182+
to = phoneToJid({ to, isGroup });
183+
const isRegistered = await isExist({
184+
sessionId,
185+
to,
186+
isGroup,
187+
});
188+
if (!isRegistered) {
189+
throw new WhatsappError(`${oldPhone} is not registered on Whatsapp`);
190+
}
191+
if (!media) {
192+
throw new WhatsappError(`Invalid Media`);
193+
}
194+
195+
return await session.sendMessage(
196+
to,
197+
{
198+
audio:
199+
typeof media == "string"
200+
? {
201+
url: media,
202+
}
203+
: media,
204+
ptt: true,
205+
},
206+
{
207+
quoted: props.answering,
208+
}
209+
);
210+
};
211+
212+
export const sendSticker = async ({
213+
sessionId,
214+
to,
215+
isGroup,
216+
media,
217+
...props
218+
}: SendMediaTypes): Promise<proto.WebMessageInfo | undefined> => {
219+
const session = getSession(sessionId);
220+
if (!session) throw new WhatsappError(Messages.sessionNotFound(sessionId));
221+
const oldPhone = to;
222+
to = phoneToJid({ to, isGroup });
223+
const isRegistered = await isExist({
224+
sessionId,
225+
to,
226+
isGroup,
227+
});
228+
if (!isRegistered) {
229+
throw new WhatsappError(`${oldPhone} is not registered on Whatsapp`);
230+
}
231+
if (!media) {
232+
throw new WhatsappError(`Invalid Media`);
233+
}
234+
235+
return await session.sendMessage(
236+
to,
237+
{
238+
sticker:
239+
typeof media == "string"
240+
? {
241+
url: media,
242+
}
243+
: media,
244+
},
245+
{
246+
quoted: props.answering,
247+
}
248+
);
249+
};
250+
168251
/**
169252
* Give typing effect to target
170253
*

src/Socket/index.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@ import makeWASocket, {
44
fetchLatestBaileysVersion,
55
useMultiFileAuthState,
66
WASocket,
7+
WAMessageUpdate,
78
} from "@whiskeysockets/baileys";
89
import pino from "pino";
910
import path from "path";
1011
import { Boom } from "@hapi/boom";
1112
import fs from "fs";
12-
import type { MessageReceived, StartSessionParams } from "../Types";
13+
import type {
14+
MessageReceived,
15+
MessageUpdated,
16+
StartSessionParams,
17+
} from "../Types";
1318
import { CALLBACK_KEY, CREDENTIALS, Messages } from "../Defaults";
1419
import {
1520
saveDocumentHandler,
1621
saveImageHandler,
1722
saveVideoHandler,
1823
} from "../Utils/save-media";
1924
import { WhatsappError } from "../Error";
25+
import { parseMessageStatusCodeToReadable } from "../Utils/message-status";
2026

2127
const sessions: Map<string, WASocket> = new Map();
2228

@@ -87,6 +93,15 @@ export const startSession = async (
8793
if (events["creds.update"]) {
8894
await saveCreds();
8995
}
96+
if (events["messages.update"]) {
97+
const msg = events["messages.update"][0];
98+
const data: MessageUpdated = {
99+
sessionId: sessionId,
100+
messageStatus: parseMessageStatusCodeToReadable(msg.update.status!),
101+
...msg,
102+
};
103+
callback.get(CALLBACK_KEY.ON_MESSAGE_UPDATED)?.(sessionId, data);
104+
}
90105
if (events["messages.upsert"]) {
91106
const msg = events["messages.upsert"]
92107
.messages?.[0] as unknown as MessageReceived;
@@ -197,3 +212,7 @@ export const onDisconnected = (listener: (sessionId: string) => any) => {
197212
export const onConnecting = (listener: (sessionId: string) => any) => {
198213
callback.set(CALLBACK_KEY.ON_CONNECTING, listener);
199214
};
215+
216+
export const onMessageUpdate = (listener: (data: MessageUpdated) => any) => {
217+
callback.set(CALLBACK_KEY.ON_MESSAGE_UPDATED, listener);
218+
};

src/Types/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { proto } from "@whiskeysockets/baileys";
1+
import { WAMessageUpdate, proto } from "@whiskeysockets/baileys";
22

33
export interface SendMessageTypes {
44
to: string | number;
@@ -48,3 +48,14 @@ export interface StartSessionParams {
4848
*/
4949
printQR: boolean;
5050
}
51+
52+
export type MessageUpdated = WAMessageUpdate & {
53+
sessionId: string;
54+
messageStatus:
55+
| "error"
56+
| "pending"
57+
| "server"
58+
| "delivered"
59+
| "read"
60+
| "played";
61+
};

src/Utils/message-status.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { proto } from "@whiskeysockets/baileys";
2+
import { MessageUpdated } from "../Types";
3+
4+
export const parseMessageStatusCodeToReadable = (
5+
code: proto.WebMessageInfo.Status
6+
): MessageUpdated["messageStatus"] => {
7+
if (code == proto.WebMessageInfo.Status.PENDING) return "pending";
8+
if (code == proto.WebMessageInfo.Status.SERVER_ACK) return "server";
9+
if (code == proto.WebMessageInfo.Status.DELIVERY_ACK) return "delivered";
10+
if (code == proto.WebMessageInfo.Status.READ) return "read";
11+
if (code == proto.WebMessageInfo.Status.PLAYED) return "played";
12+
13+
return "error";
14+
};

0 commit comments

Comments
 (0)