Skip to content

Commit da6f6d7

Browse files
committed
message: fix send media types
1 parent e93a1e9 commit da6f6d7

File tree

8 files changed

+81
-343
lines changed

8 files changed

+81
-343
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "evolution-api-sdk",
33
"private": false,
4-
"version": "0.6.11",
4+
"version": "0.6.12",
55
"description": "Unofficial SDK for the Evolution Whatsapp API v2",
66
"main": "lib/index.js",
77
"types": "lib/index.d.ts",

src/modules/messages/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class MessagesModule {
4545
options: Image.ImageMessageOptions,
4646
methodOptions?: MethodOptions
4747
): Promise<Image.ImageMessageResponse> {
48+
options.mediatype = "image";
4849
const response = await this.api.post(Routes.Message.SendMedia, {
4950
body: options,
5051
...methodOptions,
@@ -62,6 +63,7 @@ export class MessagesModule {
6263
options: Video.VideoMessageOptions,
6364
methodOptions?: MethodOptions
6465
): Promise<Video.VideoMessageResponse> {
66+
options.mediatype = "video";
6567
const response = await this.api.post(Routes.Message.SendMedia, {
6668
body: options,
6769
...methodOptions,
@@ -79,6 +81,7 @@ export class MessagesModule {
7981
options: Document.DocumentMessageOptions,
8082
methodOptions?: MethodOptions
8183
): Promise<Document.DocumentMessageResponse> {
84+
options.mediatype = "document";
8285
const response = await this.api.post(Routes.Message.SendMedia, {
8386
body: options,
8487
...methodOptions,
@@ -96,6 +99,7 @@ export class MessagesModule {
9699
options: Audio.AudioMessageOptions,
97100
methodOptions?: MethodOptions
98101
): Promise<Audio.AudioMessageResponse> {
102+
options.mediatype = "audio";
99103
const response = await this.api.post(Routes.Message.SendMedia, {
100104
body: options,
101105
...methodOptions,
Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,12 @@
11
// Pure TypeScript interfaces for better IDE support and performance
2-
import { BaseMessageOptions } from "./base";
2+
import { MediaMessageOptions, MediaMessageResponse } from "./media";
33

44
// Request interfaces
5-
export interface AudioMessageOptions extends BaseMessageOptions {
6-
audio: string;
7-
linkPreview: boolean;
8-
mentionsEveryOne: boolean;
9-
mentioned: string[];
10-
quoted: {
11-
key: {
12-
id: string;
13-
};
14-
message: {
15-
conversation: string;
16-
};
17-
};
18-
}
5+
export interface AudioMessageOptions extends MediaMessageOptions {}
196

207
// Response interfaces
21-
export interface AudioMessageResponse {
22-
key: {
23-
remoteJid: string;
24-
fromMe: boolean;
25-
id: string;
26-
};
27-
message: {
28-
audioMessage: {
29-
url: string;
30-
mimetype: string;
31-
fileSha256: string;
32-
fileLength: string;
33-
seconds: number;
34-
ptt: boolean;
35-
mediaKey: string;
36-
fileEncSha256: string;
37-
directPath: string;
38-
mediaKeyTimestamp: string;
39-
};
8+
export interface AudioMessageResponse extends MediaMessageResponse {
9+
message: MediaMessageResponse["message"] & {
10+
audioMessage: MediaMessageResponse["message"]["audioMessage"];
4011
};
41-
messageTimestamp: string;
42-
status: "PENDING" | "SUCCESS" | "FAILED";
4312
}
Lines changed: 6 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,12 @@
11
// Pure TypeScript interfaces for better IDE support and performance
2-
import type { Media } from "@/schemas/common";
3-
import { Jid, MessageId } from "@/types/tags";
4-
import { phoneNumberFromJid } from "@/utils/phone-numer-from-jid";
5-
import { BaseMessageOptions } from "./base";
6-
7-
// Raw response interface from API
8-
export interface DocumentMessageResponseRaw {
9-
key: {
10-
remoteJid: string;
11-
id: string;
12-
};
13-
message: {
14-
documentMessage: {
15-
url: string;
16-
mimetype?: string;
17-
fileSha256: string;
18-
fileLength: number | string;
19-
mediaKey: string;
20-
caption?: string;
21-
fileName: string;
22-
fileEncSha256: string;
23-
directPath: string;
24-
mediaKeyTimestamp: number | string;
25-
};
26-
};
27-
messageTimestamp: string | Date;
28-
}
2+
import { MediaMessageOptions, MediaMessageResponse } from "./media";
293

304
// Request interfaces
31-
export interface DocumentMessageOptions extends BaseMessageOptions {
32-
/**
33-
* Document URL or file in base64
34-
*/
35-
document: Media;
36-
/**
37-
* Caption to send with document
38-
*/
39-
caption?: string;
40-
/**
41-
* Document mimetype
42-
*/
43-
mimetype?: string;
44-
/**
45-
* Name of the file
46-
*/
47-
fileName?: string;
48-
}
49-
50-
export interface DocumentMessageBody extends BaseMessageOptions {
51-
media: Media;
52-
mediatype: "document";
53-
caption?: string;
54-
mimetype?: string;
55-
fileName?: string;
56-
}
5+
export interface DocumentMessageOptions extends MediaMessageOptions {}
576

587
// Response interfaces
59-
export interface DocumentMessageResponse {
60-
receiver: {
61-
phoneNumber: string;
62-
jid: Jid;
63-
};
64-
media: {
65-
url: string;
66-
caption?: string;
67-
mimetype?: string;
68-
length: number;
69-
sha256: string;
70-
fileName: string;
71-
encryptedSha256: string;
72-
directPath: string;
73-
key: string;
74-
keyTimestamp: Date;
75-
};
76-
id: MessageId;
77-
timestamp: Date;
8+
export interface DocumentMessageResponse extends MediaMessageResponse {
9+
message: MediaMessageResponse["message"] & {
10+
documentMessage: MediaMessageResponse["message"]["documentMessage"];
11+
};
7812
}
79-
80-
// Transform functions
81-
export const DocumentMessageBodyTransform = (
82-
{ document, ...data }: DocumentMessageOptions
83-
): DocumentMessageBody => ({
84-
...data,
85-
media: document,
86-
mediatype: "document",
87-
});
88-
89-
export const DocumentMessageResponseTransform = (data: DocumentMessageResponseRaw): DocumentMessageResponse => ({
90-
receiver: {
91-
phoneNumber: phoneNumberFromJid(data.key.remoteJid),
92-
jid: Jid(data.key.remoteJid),
93-
},
94-
media: {
95-
url: data.message.documentMessage.url,
96-
caption: data.message.documentMessage.caption,
97-
mimetype: data.message.documentMessage.mimetype,
98-
length: Number(data.message.documentMessage.fileLength),
99-
sha256: data.message.documentMessage.fileSha256,
100-
fileName: data.message.documentMessage.fileName,
101-
encryptedSha256: data.message.documentMessage.fileEncSha256,
102-
directPath: data.message.documentMessage.directPath,
103-
key: data.message.documentMessage.mediaKey,
104-
keyTimestamp: new Date(Number(data.message.documentMessage.mediaKeyTimestamp)),
105-
},
106-
id: MessageId(data.key.id),
107-
timestamp: new Date(data.messageTimestamp),
108-
});
109-
110-
// Backward compatibility aliases
111-
export const BodySchema = { parse: DocumentMessageBodyTransform };
112-
export const ResponseSchema = { parse: DocumentMessageResponseTransform };
Lines changed: 6 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,12 @@
11
// Pure TypeScript interfaces for better IDE support and performance
2-
import type { Media } from "@/schemas/common";
3-
import { Jid, MessageId } from "@/types/tags";
4-
import { phoneNumberFromJid } from "@/utils/phone-numer-from-jid";
5-
import { BaseMessageOptions } from "./base";
6-
7-
// Raw response interface from API
8-
export interface ImageMessageResponseRaw {
9-
key: {
10-
remoteJid: string;
11-
id: string;
12-
};
13-
message: {
14-
imageMessage: {
15-
url: string;
16-
mimetype?: string;
17-
fileSha256: string;
18-
fileLength: number | string;
19-
height: number;
20-
width: number;
21-
mediaKey: string;
22-
caption?: string;
23-
fileEncSha256: string;
24-
directPath: string;
25-
mediaKeyTimestamp: number | string;
26-
};
27-
};
28-
messageType: "image";
29-
messageTimestamp: string | Date;
30-
}
2+
import { MediaMessageOptions, MediaMessageResponse } from "./media";
313

324
// Request interfaces
33-
export interface ImageMessageOptions extends BaseMessageOptions {
34-
/**
35-
* Image URL or file in base64
36-
*/
37-
image: Media;
38-
/**
39-
* Caption to send with image
40-
*/
41-
caption?: string;
42-
/**
43-
* Image mimetype
44-
*/
45-
mimetype?: string;
46-
}
47-
48-
export interface ImageMessageBody extends BaseMessageOptions {
49-
media: Media;
50-
mediatype: "image";
51-
caption?: string;
52-
mimetype?: string;
53-
}
5+
export interface ImageMessageOptions extends MediaMessageOptions {}
546

557
// Response interfaces
56-
export interface ImageMessageResponse {
57-
receiver: {
58-
phoneNumber: string;
59-
jid: Jid;
60-
};
61-
media: {
62-
url: string;
63-
caption?: string;
64-
mimetype?: string;
65-
length: number;
66-
height: number;
67-
width: number;
68-
sha256: string;
69-
encryptedSha256: string;
70-
directPath: string;
71-
key: string;
72-
keyTimestamp: Date;
73-
};
74-
id: MessageId;
75-
timestamp: Date;
8+
export interface ImageMessageResponse extends MediaMessageResponse {
9+
message: MediaMessageResponse["message"] & {
10+
imageMessage: MediaMessageResponse["message"]["imageMessage"];
11+
};
7612
}
77-
78-
// Transform functions
79-
export const ImageMessageBodyTransform = (
80-
{ image, ...data }: ImageMessageOptions
81-
): ImageMessageBody => ({ ...data, media: image, mediatype: "image" });
82-
83-
export const ImageMessageResponseTransform = (data: ImageMessageResponseRaw): ImageMessageResponse => ({
84-
receiver: {
85-
phoneNumber: phoneNumberFromJid(data.key.remoteJid),
86-
jid: Jid(data.key.remoteJid),
87-
},
88-
media: {
89-
url: data.message.imageMessage.url,
90-
caption: data.message.imageMessage.caption,
91-
mimetype: data.message.imageMessage.mimetype,
92-
length: Number(data.message.imageMessage.fileLength),
93-
height: data.message.imageMessage.height,
94-
width: data.message.imageMessage.width,
95-
sha256: data.message.imageMessage.fileSha256,
96-
encryptedSha256: data.message.imageMessage.fileEncSha256,
97-
directPath: data.message.imageMessage.directPath,
98-
key: data.message.imageMessage.mediaKey,
99-
keyTimestamp: new Date(Number(data.message.imageMessage.mediaKeyTimestamp)),
100-
},
101-
id: MessageId(data.key.id),
102-
timestamp: new Date(data.messageTimestamp),
103-
});
104-
105-
// Backward compatibility aliases
106-
export const BodySchema = { parse: ImageMessageBodyTransform };
107-
export const ResponseSchema = { parse: ImageMessageResponseTransform };
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Media } from "@/schemas/common";
2+
import { BaseMessageOptions } from "./base";
3+
4+
// Request interfaces
5+
export interface MediaMessageOptions extends BaseMessageOptions {
6+
mediatype: "audio" | "image" | "video" | "document";
7+
mimetype?: string;
8+
caption?: string;
9+
media: Media;
10+
fileName?: string;
11+
linkPreview?: boolean;
12+
mentionsEveryOne?: boolean;
13+
mentioned?: string[];
14+
quoted?: {
15+
key: {
16+
id: string;
17+
};
18+
message: {
19+
conversation: string;
20+
};
21+
};
22+
}
23+
24+
// Response interfaces
25+
export interface MediaMessageResponse {
26+
key: {
27+
remoteJid: string;
28+
fromMe: boolean;
29+
id: string;
30+
};
31+
message: {
32+
[T in
33+
| "audioMessage"
34+
| "imageMessage"
35+
| "videoMessage"
36+
| "documentMessage"]: {
37+
url: string;
38+
mimetype: string;
39+
fileSha256: string;
40+
fileLength: string;
41+
seconds: number;
42+
ptt: boolean;
43+
mediaKey: string;
44+
fileEncSha256: string;
45+
directPath: string;
46+
mediaKeyTimestamp: string;
47+
};
48+
};
49+
messageTimestamp: string;
50+
status: "PENDING" | "SUCCESS" | "FAILED";
51+
}

0 commit comments

Comments
 (0)