Skip to content

Commit d4ee8b0

Browse files
authored
Include MessageType inside messages in Webhook
Include MessageType inside messages in Webhook
2 parents 75728e9 + 35d2179 commit d4ee8b0

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/shared/utils.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ConnectionStatus, InstanceData, ProxyAgent, WebhookPayload } from './ty
66
import path from "path";
77
import UserConfig from "../infra/config/env"
88
import { Worker } from 'worker_threads';
9-
import { jidNormalizedUser } from "@whiskeysockets/baileys";
9+
import { getContentType, jidNormalizedUser } from "@whiskeysockets/baileys";
1010

1111

1212
export async function removeInstancePath(instancePath: string){
@@ -59,6 +59,9 @@ function serializeData(data: any): any {
5959
}
6060

6161
export async function trySendWebhook(event: string, instance: InstanceData, data: any[]) {
62+
63+
const enrichedData = enrichMessagesWithType(data);
64+
6265
const payload = {
6366
event,
6467
instance: {
@@ -68,7 +71,7 @@ export async function trySendWebhook(event: string, instance: InstanceData, data
6871
profilePictureUrl: instance.profilePictureUrl,
6972
instanceJid: jidNormalizedUser(instance.socket?.user?.id) || null
7073
},
71-
data: serializeData(data),
74+
data: serializeData(enrichedData),
7275
targetUrl: UserConfig.webhookUrl
7376
};
7477

@@ -89,7 +92,44 @@ export async function trySendWebhook(event: string, instance: InstanceData, data
8992
await saveWebhookEvent(payload);
9093
worker.terminate();
9194
});
95+
}
96+
97+
function enrichMessagesWithType(data: any) {
9298

99+
// Caso seja um objeto com `.messages`
100+
if (data && Array.isArray(data.messages)) {
101+
data.messages = data.messages.map((m: any) => ({
102+
...m,
103+
messageType: getSafeMessageType(m)
104+
}));
105+
return data;
106+
}
107+
108+
// Caso seja um array contendo objetos, algum deles podendo ter `.messages`
109+
if (Array.isArray(data)) {
110+
return data.map(item => {
111+
if (item && Array.isArray(item.messages)) {
112+
return {
113+
...item,
114+
messages: item.messages.map((m: any) => ({
115+
...m,
116+
messageType: getSafeMessageType(m)
117+
}))
118+
};
119+
}
120+
return item;
121+
});
122+
}
123+
124+
return data;
125+
}
126+
127+
function getSafeMessageType(message: any): string | null {
128+
try {
129+
return getContentType(message?.message) || null;
130+
} catch {
131+
return null;
132+
}
93133
}
94134

95135
async function ensureDir() {

0 commit comments

Comments
 (0)