Skip to content

Commit ae99ec7

Browse files
committed
feat(whatsapp): implement fetchLatestWaWebVersion utility and update version fetching logic
- Added a new utility function `fetchLatestWaWebVersion` to retrieve the latest WhatsApp Web version. - Updated the Baileys service and router to utilize the new function instead of the deprecated `fetchLatestBaileysVersion`, ensuring accurate version information is fetched for WhatsApp integration. - This change enhances the reliability of version management in the application.
1 parent 22c379a commit ae99ec7

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import { Boom } from '@hapi/boom';
8080
import { createId as cuid } from '@paralleldrive/cuid2';
8181
import { Instance, Message } from '@prisma/client';
8282
import { createJid } from '@utils/createJid';
83+
import { fetchLatestWaWebVersion } from '@utils/fetchLatestWaWebVersion';
8384
import { makeProxyAgent } from '@utils/makeProxyAgent';
8485
import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache';
8586
import { status } from '@utils/renderStatus';
@@ -99,7 +100,6 @@ import makeWASocket, {
99100
delay,
100101
DisconnectReason,
101102
downloadMediaMessage,
102-
fetchLatestBaileysVersion,
103103
generateWAMessageFromContent,
104104
getAggregateVotesInPollMessage,
105105
GetCatalogOptions,
@@ -548,7 +548,7 @@ export class BaileysStartupService extends ChannelStartupService {
548548
version = session.VERSION.split('.');
549549
log = `Baileys version env: ${version}`;
550550
} else {
551-
const baileysVersion = await fetchLatestBaileysVersion();
551+
const baileysVersion = await fetchLatestWaWebVersion({});
552552
version = baileysVersion.version;
553553
log = `Baileys version: ${version}`;
554554
}

src/api/routes/index.router.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ChatbotRouter } from '@api/integrations/chatbot/chatbot.router';
66
import { EventRouter } from '@api/integrations/event/event.router';
77
import { StorageRouter } from '@api/integrations/storage/storage.router';
88
import { configService } from '@config/env.config';
9-
import { fetchLatestBaileysVersion } from 'baileys';
9+
import { fetchLatestWaWebVersion } from '@utils/fetchLatestWaWebVersion';
1010
import { Router } from 'express';
1111
import fs from 'fs';
1212
import mimeTypes from 'mime-types';
@@ -70,7 +70,7 @@ router
7070
manager: !serverConfig.DISABLE_MANAGER ? `${req.protocol}://${req.get('host')}/manager` : undefined,
7171
documentation: `https://doc.evolution-api.com`,
7272
whatsappWebVersion:
73-
process.env.CONFIG_SESSION_PHONE_VERSION || (await fetchLatestBaileysVersion()).version.join('.'),
73+
process.env.CONFIG_SESSION_PHONE_VERSION || (await fetchLatestWaWebVersion({})).version.join('.'),
7474
});
7575
})
7676
.post('/verify-creds', authGuard['apikey'], async (req, res) => {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import axios, { AxiosRequestConfig } from 'axios';
2+
import { fetchLatestBaileysVersion, WAVersion } from 'baileys';
3+
4+
export const fetchLatestWaWebVersion = async (options: AxiosRequestConfig<{}>) => {
5+
try {
6+
const { data } = await axios.get('https://web.whatsapp.com/sw.js', {
7+
...options,
8+
responseType: 'json',
9+
});
10+
11+
const regex = /\\?"client_revision\\?":\s*(\d+)/;
12+
const match = data.match(regex);
13+
14+
if (!match?.[1]) {
15+
return {
16+
version: (await fetchLatestBaileysVersion()).version as WAVersion,
17+
isLatest: false,
18+
error: {
19+
message: 'Could not find client revision in the fetched content',
20+
},
21+
};
22+
}
23+
24+
const clientRevision = match[1];
25+
26+
return {
27+
version: [2, 3000, +clientRevision] as WAVersion,
28+
isLatest: true,
29+
};
30+
} catch (error) {
31+
return {
32+
version: (await fetchLatestBaileysVersion()).version as WAVersion,
33+
isLatest: false,
34+
error,
35+
};
36+
}
37+
};

0 commit comments

Comments
 (0)