Skip to content

Commit 4451875

Browse files
DavidsonGomesligge
authored andcommitted
Merge pull request EvolutionAPI#2296 from caiobleggi/main
feat(channel): add support for @newsletter in sendMessage and findChannels
1 parent 62eed89 commit 4451875

4 files changed

Lines changed: 86 additions & 3 deletions

File tree

src/api/controllers/chat.controller.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,8 @@ export class ChatController {
113113
public async blockUser({ instanceName }: InstanceDto, data: BlockUserDto) {
114114
return await this.waMonitor.waInstances[instanceName].blockUser(data);
115115
}
116+
117+
public async fetchChannels({ instanceName }: InstanceDto, query: Query<Contact>) {
118+
return await this.waMonitor.waInstances[instanceName].fetchChannels(query);
119+
}
116120
}

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

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3573,9 +3573,24 @@ export class BaileysStartupService extends ChannelStartupService {
35733573
users: { number: string; jid: string; name?: string }[];
35743574
} = { groups: [], broadcast: [], users: [] };
35753575

3576+
const onWhatsapp: OnWhatsAppDto[] = [];
3577+
35763578
data.numbers.forEach((number) => {
35773579
const jid = createJid(number);
35783580

3581+
if (isJidNewsletter(jid)) {
3582+
onWhatsapp.push(
3583+
new OnWhatsAppDto(
3584+
jid,
3585+
true, // Newsletters are always valid
3586+
number,
3587+
undefined, // Can be fetched later if needed
3588+
'newsletter', // Indicate it's a newsletter type
3589+
),
3590+
);
3591+
return;
3592+
}
3593+
35793594
if (isJidGroup(jid)) {
35803595
jids.groups.push({ number, jid });
35813596
} else if (jid === 'status@broadcast') {
@@ -3585,8 +3600,6 @@ export class BaileysStartupService extends ChannelStartupService {
35853600
}
35863601
});
35873602

3588-
const onWhatsapp: OnWhatsAppDto[] = [];
3589-
35903603
// BROADCAST
35913604
onWhatsapp.push(...jids.broadcast.map(({ jid, number }) => new OnWhatsAppDto(jid, false, number)));
35923605

@@ -4762,6 +4775,10 @@ export class BaileysStartupService extends ChannelStartupService {
47624775
}
47634776
}
47644777

4778+
if (isJidNewsletter(message.key.remoteJid) && message.key.fromMe) {
4779+
messageRaw.status = status[3]; // DELIVERED MESSAGE TO NEWSLETTER CHANNEL
4780+
}
4781+
47654782
return messageRaw;
47664783
}
47674784

@@ -5181,4 +5198,51 @@ export class BaileysStartupService extends ChannelStartupService {
51815198
},
51825199
};
51835200
}
5201+
public async fetchChannels(query: Query<Contact>) {
5202+
const page = Number((query as any)?.page ?? 1);
5203+
const limit = Number((query as any)?.limit ?? (query as any)?.rows ?? 50);
5204+
const skip = (page - 1) * limit;
5205+
5206+
const messages = await this.prismaRepository.message.findMany({
5207+
where: {
5208+
instanceId: this.instanceId,
5209+
AND: [{ key: { path: ['remoteJid'], not: null } }],
5210+
},
5211+
orderBy: { messageTimestamp: 'desc' },
5212+
select: {
5213+
key: true,
5214+
messageTimestamp: true,
5215+
},
5216+
});
5217+
5218+
const channelMap = new Map<string, { remoteJid: string; pushName: undefined; lastMessageTimestamp: number }>();
5219+
5220+
for (const msg of messages) {
5221+
const key = msg.key as any;
5222+
const remoteJid = key?.remoteJid as string | undefined;
5223+
if (!remoteJid || !isJidNewsletter(remoteJid)) continue;
5224+
5225+
if (!channelMap.has(remoteJid)) {
5226+
channelMap.set(remoteJid, {
5227+
remoteJid,
5228+
pushName: undefined, // Push name is never stored for channels, so we set it as undefined
5229+
lastMessageTimestamp: msg.messageTimestamp,
5230+
});
5231+
}
5232+
}
5233+
5234+
const allChannels = Array.from(channelMap.values());
5235+
5236+
const total = allChannels.length;
5237+
const pages = Math.ceil(total / limit);
5238+
const records = allChannels.slice(skip, skip + limit);
5239+
5240+
return {
5241+
total,
5242+
pages,
5243+
currentPage: page,
5244+
limit,
5245+
records,
5246+
};
5247+
}
51845248
}

src/api/routes/chat.router.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ export class ChatRouter extends RouterBroker {
281281
});
282282

283283
return res.status(HttpStatus.CREATED).json(response);
284+
})
285+
.post(this.routerPath('findChannels'), ...guards, async (req, res) => {
286+
const response = await this.dataValidate({
287+
request: req,
288+
schema: contactValidateSchema,
289+
ClassRef: Query<Contact>,
290+
execute: (instance, query) => chatController.fetchChannels(instance, query as any),
291+
});
292+
293+
return res.status(HttpStatus.OK).json(response);
284294
});
285295
}
286296

src/utils/createJid.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ function formatBRNumber(jid: string) {
3535
export function createJid(number: string): string {
3636
number = number.replace(/:\d+/, '');
3737

38-
if (number.includes('@g.us') || number.includes('@s.whatsapp.net') || number.includes('@lid')) {
38+
if (
39+
number.includes('@g.us') ||
40+
number.includes('@s.whatsapp.net') ||
41+
number.includes('@lid') ||
42+
number.includes('@newsletter')
43+
) {
3944
return number;
4045
}
4146

0 commit comments

Comments
 (0)