mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-18 19:32:21 -06:00
Merge pull request #2296 from caiobleggi/main
Some checks failed
Some checks failed
feat(channel): add support for @newsletter in sendMessage and findChannels
This commit is contained in:
@@ -113,4 +113,8 @@ export class ChatController {
|
|||||||
public async blockUser({ instanceName }: InstanceDto, data: BlockUserDto) {
|
public async blockUser({ instanceName }: InstanceDto, data: BlockUserDto) {
|
||||||
return await this.waMonitor.waInstances[instanceName].blockUser(data);
|
return await this.waMonitor.waInstances[instanceName].blockUser(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async fetchChannels({ instanceName }: InstanceDto, query: Query<Contact>) {
|
||||||
|
return await this.waMonitor.waInstances[instanceName].fetchChannels(query);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3511,9 +3511,24 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
users: { number: string; jid: string; name?: string }[];
|
users: { number: string; jid: string; name?: string }[];
|
||||||
} = { groups: [], broadcast: [], users: [] };
|
} = { groups: [], broadcast: [], users: [] };
|
||||||
|
|
||||||
|
const onWhatsapp: OnWhatsAppDto[] = [];
|
||||||
|
|
||||||
data.numbers.forEach((number) => {
|
data.numbers.forEach((number) => {
|
||||||
const jid = createJid(number);
|
const jid = createJid(number);
|
||||||
|
|
||||||
|
if (isJidNewsletter(jid)) {
|
||||||
|
onWhatsapp.push(
|
||||||
|
new OnWhatsAppDto(
|
||||||
|
jid,
|
||||||
|
true, // Newsletters are always valid
|
||||||
|
number,
|
||||||
|
undefined, // Can be fetched later if needed
|
||||||
|
'newsletter', // Indicate it's a newsletter type
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (isJidGroup(jid)) {
|
if (isJidGroup(jid)) {
|
||||||
jids.groups.push({ number, jid });
|
jids.groups.push({ number, jid });
|
||||||
} else if (jid === 'status@broadcast') {
|
} else if (jid === 'status@broadcast') {
|
||||||
@@ -3523,8 +3538,6 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const onWhatsapp: OnWhatsAppDto[] = [];
|
|
||||||
|
|
||||||
// BROADCAST
|
// BROADCAST
|
||||||
onWhatsapp.push(...jids.broadcast.map(({ jid, number }) => new OnWhatsAppDto(jid, false, number)));
|
onWhatsapp.push(...jids.broadcast.map(({ jid, number }) => new OnWhatsAppDto(jid, false, number)));
|
||||||
|
|
||||||
@@ -4700,6 +4713,10 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isJidNewsletter(message.key.remoteJid) && message.key.fromMe) {
|
||||||
|
messageRaw.status = status[3]; // DELIVERED MESSAGE TO NEWSLETTER CHANNEL
|
||||||
|
}
|
||||||
|
|
||||||
return messageRaw;
|
return messageRaw;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -5119,4 +5136,51 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
public async fetchChannels(query: Query<Contact>) {
|
||||||
|
const page = Number((query as any)?.page ?? 1);
|
||||||
|
const limit = Number((query as any)?.limit ?? (query as any)?.rows ?? 50);
|
||||||
|
const skip = (page - 1) * limit;
|
||||||
|
|
||||||
|
const messages = await this.prismaRepository.message.findMany({
|
||||||
|
where: {
|
||||||
|
instanceId: this.instanceId,
|
||||||
|
AND: [{ key: { path: ['remoteJid'], not: null } }],
|
||||||
|
},
|
||||||
|
orderBy: { messageTimestamp: 'desc' },
|
||||||
|
select: {
|
||||||
|
key: true,
|
||||||
|
messageTimestamp: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const channelMap = new Map<string, { remoteJid: string; pushName: undefined; lastMessageTimestamp: number }>();
|
||||||
|
|
||||||
|
for (const msg of messages) {
|
||||||
|
const key = msg.key as any;
|
||||||
|
const remoteJid = key?.remoteJid as string | undefined;
|
||||||
|
if (!remoteJid || !isJidNewsletter(remoteJid)) continue;
|
||||||
|
|
||||||
|
if (!channelMap.has(remoteJid)) {
|
||||||
|
channelMap.set(remoteJid, {
|
||||||
|
remoteJid,
|
||||||
|
pushName: undefined, // Push name is never stored for channels, so we set it as undefined
|
||||||
|
lastMessageTimestamp: msg.messageTimestamp,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const allChannels = Array.from(channelMap.values());
|
||||||
|
|
||||||
|
const total = allChannels.length;
|
||||||
|
const pages = Math.ceil(total / limit);
|
||||||
|
const records = allChannels.slice(skip, skip + limit);
|
||||||
|
|
||||||
|
return {
|
||||||
|
total,
|
||||||
|
pages,
|
||||||
|
currentPage: page,
|
||||||
|
limit,
|
||||||
|
records,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -281,6 +281,16 @@ export class ChatRouter extends RouterBroker {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return res.status(HttpStatus.CREATED).json(response);
|
return res.status(HttpStatus.CREATED).json(response);
|
||||||
|
})
|
||||||
|
.post(this.routerPath('findChannels'), ...guards, async (req, res) => {
|
||||||
|
const response = await this.dataValidate({
|
||||||
|
request: req,
|
||||||
|
schema: contactValidateSchema,
|
||||||
|
ClassRef: Query<Contact>,
|
||||||
|
execute: (instance, query) => chatController.fetchChannels(instance, query as any),
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(HttpStatus.OK).json(response);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,12 @@ function formatBRNumber(jid: string) {
|
|||||||
export function createJid(number: string): string {
|
export function createJid(number: string): string {
|
||||||
number = number.replace(/:\d+/, '');
|
number = number.replace(/:\d+/, '');
|
||||||
|
|
||||||
if (number.includes('@g.us') || number.includes('@s.whatsapp.net') || number.includes('@lid')) {
|
if (
|
||||||
|
number.includes('@g.us') ||
|
||||||
|
number.includes('@s.whatsapp.net') ||
|
||||||
|
number.includes('@lid') ||
|
||||||
|
number.includes('@newsletter')
|
||||||
|
) {
|
||||||
return number;
|
return number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user