mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-22 20:12:02 -06:00
feat(endpoint): add setPresence endpoint
This commit is contained in:
parent
5f5db2011e
commit
e58f1d778e
@ -161,6 +161,18 @@ export const presenceSchema: JSONSchema7 = {
|
||||
required: ['options', 'number'],
|
||||
};
|
||||
|
||||
export const presenceOnlySchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
presence: {
|
||||
type: 'string',
|
||||
enum: ['unavailable', 'available', 'composing', 'recording', 'paused'],
|
||||
},
|
||||
},
|
||||
required: ['presence'],
|
||||
};
|
||||
|
||||
export const pollMessageSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
|
@ -11,6 +11,7 @@ import {
|
||||
ProfileStatusDto,
|
||||
ReadMessageDto,
|
||||
SendPresenceDto,
|
||||
SetPresenceDto,
|
||||
UpdateMessageDto,
|
||||
WhatsAppNumberDto,
|
||||
} from '../dto/chat.dto';
|
||||
@ -85,6 +86,11 @@ export class ChatController {
|
||||
return await this.waMonitor.waInstances[instanceName].sendPresence(data);
|
||||
}
|
||||
|
||||
public async setPresence({ instanceName }: InstanceDto, data: SetPresenceDto) {
|
||||
logger.verbose('requested sendPresence from ' + instanceName + ' instance');
|
||||
return await this.waMonitor.waInstances[instanceName].setPresence(data);
|
||||
}
|
||||
|
||||
public async fetchPrivacySettings({ instanceName }: InstanceDto) {
|
||||
logger.verbose('requested fetchPrivacySettings from ' + instanceName + ' instance');
|
||||
return await this.waMonitor.waInstances[instanceName].fetchPrivacySettings();
|
||||
|
@ -110,6 +110,10 @@ export class SendPresenceDto extends Metadata {
|
||||
};
|
||||
}
|
||||
|
||||
export class SetPresenceDto {
|
||||
presence: WAPresence;
|
||||
}
|
||||
|
||||
export class UpdateMessageDto extends Metadata {
|
||||
number: string;
|
||||
key: proto.IMessageKey;
|
||||
|
@ -9,6 +9,7 @@ import {
|
||||
messageUpSchema,
|
||||
messageValidateSchema,
|
||||
presenceSchema,
|
||||
presenceOnlySchema,
|
||||
privacySettingsSchema,
|
||||
profileNameSchema,
|
||||
profilePictureSchema,
|
||||
@ -31,6 +32,7 @@ import {
|
||||
ProfileStatusDto,
|
||||
ReadMessageDto,
|
||||
SendPresenceDto,
|
||||
SetPresenceDto,
|
||||
UpdateMessageDto,
|
||||
WhatsAppNumberDto,
|
||||
} from '../dto/chat.dto';
|
||||
@ -250,6 +252,22 @@ export class ChatRouter extends RouterBroker {
|
||||
|
||||
return res.status(HttpStatus.CREATED).json(response);
|
||||
})
|
||||
.post(this.routerPath('setPresence'), ...guards, async (req, res) => {
|
||||
logger.verbose('request received in setPresence');
|
||||
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: presenceOnlySchema,
|
||||
ClassRef: SetPresenceDto,
|
||||
execute: (instance, data) => chatController.setPresence(instance, data),
|
||||
});
|
||||
|
||||
return res.status(HttpStatus.CREATED).json(response);
|
||||
})
|
||||
// Profile routes
|
||||
.get(this.routerPath('fetchPrivacySettings'), ...guards, async (req, res) => {
|
||||
logger.verbose('request received in fetchPrivacySettings');
|
||||
|
@ -72,6 +72,7 @@ import {
|
||||
PrivacySettingDto,
|
||||
ReadMessageDto,
|
||||
SendPresenceDto,
|
||||
SetPresenceDto,
|
||||
UpdateMessageDto,
|
||||
WhatsAppNumberDto,
|
||||
} from '../dto/chat.dto';
|
||||
@ -1830,7 +1831,6 @@ export class BaileysStartupService extends WAStartupService {
|
||||
}
|
||||
|
||||
// Instance Controller
|
||||
|
||||
public async sendPresence(data: SendPresenceDto) {
|
||||
try {
|
||||
const { number } = data;
|
||||
@ -1863,6 +1863,17 @@ export class BaileysStartupService extends WAStartupService {
|
||||
}
|
||||
}
|
||||
|
||||
// Presence Controller
|
||||
public async setPresence(data: SetPresenceDto) {
|
||||
try {
|
||||
await this.client.sendPresenceUpdate(data.presence);
|
||||
this.logger.verbose('Sending presence update: ' + data.presence);
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
throw new BadRequestException(error.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// Send Message Controller
|
||||
public async textMessage(data: SendTextDto, isChatwoot = false) {
|
||||
this.logger.verbose('Sending text message');
|
||||
|
@ -1185,6 +1185,9 @@ export class BusinessStartupService extends WAStartupService {
|
||||
public async sendPresence() {
|
||||
throw new BadRequestException('Method not available on WhatsApp Business API');
|
||||
}
|
||||
public async setPresence() {
|
||||
throw new BadRequestException('Method not available on WhatsApp Business API');
|
||||
}
|
||||
public async fetchPrivacySettings() {
|
||||
throw new BadRequestException('Method not available on WhatsApp Business API');
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user