Skip to content

Commit

Permalink
Merge pull request #18 from mimamch/dev-3.3.0
Browse files Browse the repository at this point in the history
v.3.3.0
  • Loading branch information
mimamch authored Aug 28, 2023
2 parents 39061ca + 34caa4b commit e19afb8
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wa-multi-session",
"version": "3.2.1",
"version": "3.3.0",
"description": "Multi Session Whatsapp Library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
26 changes: 23 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ const send = await whatsapp.sendDocument({
});
```

Send Voice Note

```ts
const filename = "myaudio.mp3";
const audio = fs.readFileSync(filename); // return Buffer
const send = await whatsapp.sendVoiceNote({
sessionId: "session1",
to: "6281234567890",
media: audio,
});
```

Read a Message

```ts
Expand Down Expand Up @@ -211,15 +223,23 @@ whatsapp.setCredentialsDir("my_custom_dir");

## Change Log

v3.2.1 July 2023 (LATEST)
### v3.3 September 2023 (LATEST)

What's New:

- Send Voice Note
- Send Sticker
- onMessageUpdate (message ack status)

### v3.2.1 July 2023

- Add error class named: WhatsappError

v3.1.2 July 2023
### v3.1.2 July 2023

- Add send document message

v3.0.0 June 2023
### v3.0.0 June 2023

- Fix Logout Issue
- Switching into [@whiskeysockets/baileys](https://github.com/WhiskeySockets/Baileys)
Expand Down
1 change: 1 addition & 0 deletions src/Defaults/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum CALLBACK_KEY {
ON_CONNECTED = "on-connected",
ON_DISCONNECTED = "on-disconnected",
ON_CONNECTING = "on-connecting",
ON_MESSAGE_UPDATED = "on-message-updated",
}

export abstract class Messages {
Expand Down
91 changes: 87 additions & 4 deletions src/Messaging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ export const sendDocument = async ({
filename,
...props
}: SendMediaTypes & {
media: Buffer;
filename: string;
}): Promise<proto.WebMessageInfo | undefined> => {
const session = getSession(sessionId);
Expand All @@ -142,8 +141,8 @@ export const sendDocument = async ({
if (!isRegistered) {
throw new WhatsappError(`${oldPhone} is not registered on Whatsapp`);
}
if (!media || !Buffer.isBuffer(media)) {
throw new WhatsappError(`Media File must be Buffer`);
if (!media) {
throw new WhatsappError(`Invalid Media`);
}

const mimetype = mime.getType(filename);
Expand All @@ -155,7 +154,12 @@ export const sendDocument = async ({
to,
{
fileName: filename,
document: media,
document:
typeof media == "string"
? {
url: media,
}
: media,
mimetype: mimetype,
caption: text,
},
Expand All @@ -165,6 +169,85 @@ export const sendDocument = async ({
);
};

export const sendVoiceNote = async ({
sessionId,
to,
isGroup = false,
media,
...props
}: Omit<SendMediaTypes, "text">): Promise<proto.WebMessageInfo | undefined> => {
const session = getSession(sessionId);
if (!session) throw new WhatsappError(Messages.sessionNotFound(sessionId));
const oldPhone = to;
to = phoneToJid({ to, isGroup });
const isRegistered = await isExist({
sessionId,
to,
isGroup,
});
if (!isRegistered) {
throw new WhatsappError(`${oldPhone} is not registered on Whatsapp`);
}
if (!media) {
throw new WhatsappError(`Invalid Media`);
}

return await session.sendMessage(
to,
{
audio:
typeof media == "string"
? {
url: media,
}
: media,
ptt: true,
},
{
quoted: props.answering,
}
);
};

export const sendSticker = async ({
sessionId,
to,
isGroup,
media,
...props
}: SendMediaTypes): Promise<proto.WebMessageInfo | undefined> => {
const session = getSession(sessionId);
if (!session) throw new WhatsappError(Messages.sessionNotFound(sessionId));
const oldPhone = to;
to = phoneToJid({ to, isGroup });
const isRegistered = await isExist({
sessionId,
to,
isGroup,
});
if (!isRegistered) {
throw new WhatsappError(`${oldPhone} is not registered on Whatsapp`);
}
if (!media) {
throw new WhatsappError(`Invalid Media`);
}

return await session.sendMessage(
to,
{
sticker:
typeof media == "string"
? {
url: media,
}
: media,
},
{
quoted: props.answering,
}
);
};

/**
* Give typing effect to target
*
Expand Down
21 changes: 20 additions & 1 deletion src/Socket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ import makeWASocket, {
fetchLatestBaileysVersion,
useMultiFileAuthState,
WASocket,
WAMessageUpdate,
} from "@whiskeysockets/baileys";
import pino from "pino";
import path from "path";
import { Boom } from "@hapi/boom";
import fs from "fs";
import type { MessageReceived, StartSessionParams } from "../Types";
import type {
MessageReceived,
MessageUpdated,
StartSessionParams,
} from "../Types";
import { CALLBACK_KEY, CREDENTIALS, Messages } from "../Defaults";
import {
saveDocumentHandler,
saveImageHandler,
saveVideoHandler,
} from "../Utils/save-media";
import { WhatsappError } from "../Error";
import { parseMessageStatusCodeToReadable } from "../Utils/message-status";

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

Expand Down Expand Up @@ -87,6 +93,15 @@ export const startSession = async (
if (events["creds.update"]) {
await saveCreds();
}
if (events["messages.update"]) {
const msg = events["messages.update"][0];
const data: MessageUpdated = {
sessionId: sessionId,
messageStatus: parseMessageStatusCodeToReadable(msg.update.status!),
...msg,
};
callback.get(CALLBACK_KEY.ON_MESSAGE_UPDATED)?.(sessionId, data);
}
if (events["messages.upsert"]) {
const msg = events["messages.upsert"]
.messages?.[0] as unknown as MessageReceived;
Expand Down Expand Up @@ -197,3 +212,7 @@ export const onDisconnected = (listener: (sessionId: string) => any) => {
export const onConnecting = (listener: (sessionId: string) => any) => {
callback.set(CALLBACK_KEY.ON_CONNECTING, listener);
};

export const onMessageUpdate = (listener: (data: MessageUpdated) => any) => {
callback.set(CALLBACK_KEY.ON_MESSAGE_UPDATED, listener);
};
13 changes: 12 additions & 1 deletion src/Types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { proto } from "@whiskeysockets/baileys";
import { WAMessageUpdate, proto } from "@whiskeysockets/baileys";

export interface SendMessageTypes {
to: string | number;
Expand Down Expand Up @@ -48,3 +48,14 @@ export interface StartSessionParams {
*/
printQR: boolean;
}

export type MessageUpdated = WAMessageUpdate & {
sessionId: string;
messageStatus:
| "error"
| "pending"
| "server"
| "delivered"
| "read"
| "played";
};
14 changes: 14 additions & 0 deletions src/Utils/message-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { proto } from "@whiskeysockets/baileys";
import { MessageUpdated } from "../Types";

export const parseMessageStatusCodeToReadable = (
code: proto.WebMessageInfo.Status
): MessageUpdated["messageStatus"] => {
if (code == proto.WebMessageInfo.Status.PENDING) return "pending";
if (code == proto.WebMessageInfo.Status.SERVER_ACK) return "server";
if (code == proto.WebMessageInfo.Status.DELIVERY_ACK) return "delivered";
if (code == proto.WebMessageInfo.Status.READ) return "read";
if (code == proto.WebMessageInfo.Status.PLAYED) return "played";

return "error";
};

0 comments on commit e19afb8

Please sign in to comment.