add send presence router

This commit is contained in:
Gabriel Pastori 2023-12-06 00:31:35 -03:00
parent 4c69b059d4
commit ee0f0f0be0
5 changed files with 76 additions and 0 deletions

View File

@ -149,6 +149,16 @@ export const textMessageSchema: JSONSchema7 = {
required: ['textMessage', 'number'], required: ['textMessage', 'number'],
}; };
export const presenceSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
number: { ...numberDefinition },
options: { ...optionsSchema, required: ['presence', 'delay'] },
},
required: ['options', 'number'],
};
export const pollMessageSchema: JSONSchema7 = { export const pollMessageSchema: JSONSchema7 = {
$id: v4(), $id: v4(),
type: 'object', type: 'object',

View File

@ -11,6 +11,7 @@ import {
SendLocationDto, SendLocationDto,
SendMediaDto, SendMediaDto,
SendPollDto, SendPollDto,
SendPresenceDto,
SendReactionDto, SendReactionDto,
SendStatusDto, SendStatusDto,
SendStickerDto, SendStickerDto,
@ -23,6 +24,11 @@ const logger = new Logger('MessageRouter');
export class SendMessageController { export class SendMessageController {
constructor(private readonly waMonitor: WAMonitoringService) {} constructor(private readonly waMonitor: WAMonitoringService) {}
public async sendPresence({ instanceName }: InstanceDto, data: SendPresenceDto) {
logger.verbose('requested sendPresence from ' + instanceName + ' instance');
return await this.waMonitor.waInstances[instanceName].sendPresence(data);
}
public async sendText({ instanceName }: InstanceDto, data: SendTextDto) { public async sendText({ instanceName }: InstanceDto, data: SendTextDto) {
logger.verbose('requested sendText from ' + instanceName + ' instance'); logger.verbose('requested sendText from ' + instanceName + ' instance');
return await this.waMonitor.waInstances[instanceName].textMessage(data); return await this.waMonitor.waInstances[instanceName].textMessage(data);

View File

@ -46,9 +46,18 @@ class PollMessage {
values: string[]; values: string[];
messageSecret?: Uint8Array; messageSecret?: Uint8Array;
} }
export class SendPresenceDto extends Metadata {
options: {
presence: WAPresence;
delay: number;
};
}
export class SendTextDto extends Metadata { export class SendTextDto extends Metadata {
textMessage: TextMessage; textMessage: TextMessage;
} }
export class SendPresence extends Metadata {
textMessage: TextMessage;
}
export class SendStatusDto extends Metadata { export class SendStatusDto extends Metadata {
statusMessage: StatusMessage; statusMessage: StatusMessage;

View File

@ -9,6 +9,7 @@ import {
locationMessageSchema, locationMessageSchema,
mediaMessageSchema, mediaMessageSchema,
pollMessageSchema, pollMessageSchema,
presenceSchema,
reactionMessageSchema, reactionMessageSchema,
statusMessageSchema, statusMessageSchema,
stickerMessageSchema, stickerMessageSchema,
@ -23,6 +24,7 @@ import {
SendLocationDto, SendLocationDto,
SendMediaDto, SendMediaDto,
SendPollDto, SendPollDto,
SendPresenceDto,
SendReactionDto, SendReactionDto,
SendStatusDto, SendStatusDto,
SendStickerDto, SendStickerDto,
@ -37,6 +39,22 @@ export class MessageRouter extends RouterBroker {
constructor(...guards: RequestHandler[]) { constructor(...guards: RequestHandler[]) {
super(); super();
this.router this.router
.post(this.routerPath('sendPresence'), ...guards, async (req, res) => {
logger.verbose('request received in sendText');
logger.verbose('request body: ');
logger.verbose(req.body);
logger.verbose('request query: ');
logger.verbose(req.query);
const response = await this.dataValidate<null>({
request: req,
schema: presenceSchema,
ClassRef: SendPresenceDto,
execute: (instance, data) => sendMessageController.sendPresence(instance, data),
});
return res.status(HttpStatus.CREATED).json(response);
})
.post(this.routerPath('sendText'), ...guards, async (req, res) => { .post(this.routerPath('sendText'), ...guards, async (req, res) => {
logger.verbose('request received in sendText'); logger.verbose('request received in sendText');
logger.verbose('request body: '); logger.verbose('request body: ');

View File

@ -109,6 +109,7 @@ import {
SendLocationDto, SendLocationDto,
SendMediaDto, SendMediaDto,
SendPollDto, SendPollDto,
SendPresenceDto,
SendReactionDto, SendReactionDto,
SendStatusDto, SendStatusDto,
SendStickerDto, SendStickerDto,
@ -2387,6 +2388,38 @@ export class WAStartupService {
return this.stateConnection; return this.stateConnection;
} }
public async sendPresence(data: SendPresenceDto) {
try {
const { number } = data;
this.logger.verbose(`Check if number "${number}" is WhatsApp`);
const isWA = (await this.whatsappNumber({ numbers: [number] }))?.shift();
this.logger.verbose(`Exists: "${isWA.exists}" | jid: ${isWA.jid}`);
if (!isWA.exists && !isJidGroup(isWA.jid) && !isWA.jid.includes('@broadcast')) {
throw new BadRequestException(isWA);
}
const sender = isWA.jid;
this.logger.verbose('Sending presence');
await this.client.presenceSubscribe(sender);
this.logger.verbose('Subscribing to presence');
await this.client.sendPresenceUpdate(data.options?.presence ?? 'composing', sender);
this.logger.verbose('Sending presence update: ' + data.options?.presence ?? 'composing');
await delay(data.options.delay);
this.logger.verbose('Set delay: ' + data.options.delay);
await this.client.sendPresenceUpdate('paused', sender);
this.logger.verbose('Sending presence update: paused');
} catch (error) {
this.logger.error(error);
throw new BadRequestException(error.toString());
}
}
// Send Message Controller // Send Message Controller
public async textMessage(data: SendTextDto, isChatwoot = false) { public async textMessage(data: SendTextDto, isChatwoot = false) {
this.logger.verbose('Sending text message'); this.logger.verbose('Sending text message');