mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-26 07:07:45 -06:00
feat: Added group invitation sending and Added webhook configuration per event in the individual instance registration
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
||||
GroupInvite,
|
||||
GroupJid,
|
||||
GroupPictureDto,
|
||||
GroupSendInvite,
|
||||
GroupSubjectDto,
|
||||
GroupToggleEphemeralDto,
|
||||
GroupUpdateParticipantDto,
|
||||
@@ -56,10 +57,8 @@ export class GroupController {
|
||||
return await this.waMonitor.waInstances[instance.instanceName].inviteInfo(inviteCode);
|
||||
}
|
||||
|
||||
public async acceptInvite(instance: InstanceDto, inviteCode: GroupInvite) {
|
||||
return await this.waMonitor.waInstances[instance.instanceName].acceptInvite(
|
||||
inviteCode,
|
||||
);
|
||||
public async sendInvite(instance: InstanceDto, data: GroupSendInvite) {
|
||||
return await this.waMonitor.waInstances[instance.instanceName].sendInvite(data);
|
||||
}
|
||||
|
||||
public async revokeInviteCode(instance: InstanceDto, groupJid: GroupJid) {
|
||||
|
||||
@@ -26,6 +26,7 @@ export class InstanceController {
|
||||
public async createInstance({
|
||||
instanceName,
|
||||
webhook,
|
||||
webhook_by_events,
|
||||
events,
|
||||
qrcode,
|
||||
token,
|
||||
@@ -60,7 +61,12 @@ export class InstanceController {
|
||||
|
||||
if (webhook) {
|
||||
try {
|
||||
this.webhookService.create(instance, { enabled: true, url: webhook, events });
|
||||
this.webhookService.create(instance, {
|
||||
enabled: true,
|
||||
url: webhook,
|
||||
events,
|
||||
webhook_by_events,
|
||||
});
|
||||
|
||||
getEvents = (await this.webhookService.find(instance)).events;
|
||||
} catch (error) {
|
||||
@@ -98,7 +104,12 @@ export class InstanceController {
|
||||
|
||||
if (webhook) {
|
||||
try {
|
||||
this.webhookService.create(instance, { enabled: true, url: webhook, events });
|
||||
this.webhookService.create(instance, {
|
||||
enabled: true,
|
||||
url: webhook,
|
||||
events,
|
||||
webhook_by_events,
|
||||
});
|
||||
|
||||
getEvents = (await this.webhookService.find(instance)).events;
|
||||
} catch (error) {
|
||||
@@ -121,6 +132,7 @@ export class InstanceController {
|
||||
},
|
||||
hash,
|
||||
webhook,
|
||||
webhook_by_events,
|
||||
events: getEvents,
|
||||
qrcode: getQrcode,
|
||||
};
|
||||
|
||||
@@ -27,6 +27,12 @@ export class GroupInvite {
|
||||
inviteCode: string;
|
||||
}
|
||||
|
||||
export class GroupSendInvite {
|
||||
groupJid: string;
|
||||
description: string;
|
||||
numbers: string[];
|
||||
}
|
||||
|
||||
export class GroupUpdateParticipantDto extends GroupJid {
|
||||
action: 'add' | 'remove' | 'promote' | 'demote';
|
||||
participants: string[];
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export class InstanceDto {
|
||||
instanceName: string;
|
||||
webhook?: string;
|
||||
webhook_by_events?: boolean;
|
||||
events?: string[];
|
||||
qrcode?: boolean;
|
||||
token?: string;
|
||||
|
||||
@@ -2,4 +2,5 @@ export class WebhookDto {
|
||||
enabled?: boolean;
|
||||
url?: string;
|
||||
events?: string[];
|
||||
webhook_by_events?: boolean;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ export class WebhookRaw {
|
||||
url?: string;
|
||||
enabled?: boolean;
|
||||
events?: string[];
|
||||
webhook_by_events?: boolean;
|
||||
}
|
||||
|
||||
const webhookSchema = new Schema<WebhookRaw>({
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
updateGroupSubjectSchema,
|
||||
updateGroupDescriptionSchema,
|
||||
groupInviteSchema,
|
||||
groupSendInviteSchema,
|
||||
} from '../../validate/validate.schema';
|
||||
import { RouterBroker } from '../abstract/abstract.router';
|
||||
import {
|
||||
@@ -21,6 +22,7 @@ import {
|
||||
GroupUpdateParticipantDto,
|
||||
GroupUpdateSettingDto,
|
||||
GroupToggleEphemeralDto,
|
||||
GroupSendInvite,
|
||||
} from '../dto/group.dto';
|
||||
import { groupController } from '../whatsapp.module';
|
||||
import { HttpStatus } from './index.router';
|
||||
@@ -120,12 +122,12 @@ export class GroupRouter extends RouterBroker {
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.get(this.routerPath('acceptInvite'), ...guards, async (req, res) => {
|
||||
const response = await this.inviteCodeValidate<GroupInvite>({
|
||||
.post(this.routerPath('sendInvite'), ...guards, async (req, res) => {
|
||||
const response = await this.groupNoValidate<GroupSendInvite>({
|
||||
request: req,
|
||||
schema: groupInviteSchema,
|
||||
ClassRef: GroupInvite,
|
||||
execute: (instance, data) => groupController.acceptInvite(instance, data),
|
||||
schema: groupSendInviteSchema,
|
||||
ClassRef: GroupSendInvite,
|
||||
execute: (instance, data) => groupController.sendInvite(instance, data),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
|
||||
@@ -104,6 +104,7 @@ import {
|
||||
GroupToggleEphemeralDto,
|
||||
GroupSubjectDto,
|
||||
GroupDescriptionDto,
|
||||
GroupSendInvite,
|
||||
} from '../dto/group.dto';
|
||||
import { MessageUpQuery } from '../repository/messageUp.repository';
|
||||
import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-db';
|
||||
@@ -204,6 +205,7 @@ export class WAStartupService {
|
||||
this.localWebhook.url = data?.url;
|
||||
this.localWebhook.enabled = data?.enabled;
|
||||
this.localWebhook.events = data?.events;
|
||||
this.localWebhook.webhook_by_events = data?.webhook_by_events;
|
||||
}
|
||||
|
||||
public async setWebhook(data: WebhookRaw) {
|
||||
@@ -224,11 +226,9 @@ export class WAStartupService {
|
||||
|
||||
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
|
||||
if (local && instance.MODE !== 'container') {
|
||||
const { WEBHOOK_BY_EVENTS } = instance;
|
||||
|
||||
let baseURL;
|
||||
|
||||
if (WEBHOOK_BY_EVENTS) {
|
||||
if (this.localWebhook.webhook_by_events) {
|
||||
baseURL = `${this.localWebhook.url}/${transformedWe}`;
|
||||
} else {
|
||||
baseURL = this.localWebhook.url;
|
||||
@@ -484,7 +484,7 @@ export class WAStartupService {
|
||||
|
||||
const { version } = await fetchLatestBaileysVersion();
|
||||
const session = this.configService.get<ConfigSessionPhone>('CONFIG_SESSION_PHONE');
|
||||
const browser: WABrowserDescription = [session.CLIENT, 'Chrome', release()];
|
||||
const browser: WABrowserDescription = [session.CLIENT, session.NAME, release()];
|
||||
|
||||
const socketConfig: UserFacingSocketConfig = {
|
||||
auth: {
|
||||
@@ -1773,11 +1773,28 @@ export class WAStartupService {
|
||||
}
|
||||
}
|
||||
|
||||
public async acceptInvite(id: GroupInvite) {
|
||||
public async sendInvite(id: GroupSendInvite) {
|
||||
try {
|
||||
return await this.client.groupAcceptInvite(id.inviteCode);
|
||||
const inviteCode = await this.inviteCode({ groupJid: id.groupJid });
|
||||
const inviteUrl = inviteCode.inviteUrl;
|
||||
const numbers = id.numbers.map((number) => this.createJid(number));
|
||||
const description = id.description ?? '';
|
||||
|
||||
const msg = `${description}\n${inviteUrl}`;
|
||||
|
||||
const message = {
|
||||
linkPreview: {
|
||||
text: msg,
|
||||
},
|
||||
};
|
||||
|
||||
for await (const number of numbers) {
|
||||
await this.sendMessageWithTyping(number, message);
|
||||
}
|
||||
|
||||
return { send: true, inviteUrl };
|
||||
} catch (error) {
|
||||
throw new NotFoundException('No invite info', id.inviteCode);
|
||||
throw new NotFoundException('No send invite');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1831,7 +1848,7 @@ export class WAStartupService {
|
||||
update.groupJid,
|
||||
update.expiration,
|
||||
);
|
||||
return { toggleEphemeral: toggleEphemeral };
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
throw new BadRequestException('Error updating setting', error.toString());
|
||||
}
|
||||
|
||||
@@ -34,7 +34,12 @@ export declare namespace wa {
|
||||
profilePictureUrl?: string;
|
||||
};
|
||||
|
||||
export type LocalWebHook = { enabled?: boolean; url?: string; events?: string[] };
|
||||
export type LocalWebHook = {
|
||||
enabled?: boolean;
|
||||
url?: string;
|
||||
events?: string[];
|
||||
webhook_by_events?: boolean;
|
||||
};
|
||||
|
||||
export type StateConnection = {
|
||||
instance?: string;
|
||||
|
||||
Reference in New Issue
Block a user