Skip to content

Commit

Permalink
Merge pull request #479 from dv336699/develop
Browse files Browse the repository at this point in the history
feat(endpoint): add setPresence endpoint
  • Loading branch information
DavidsonGomes authored Mar 24, 2024
2 parents 70d4eb3 + 5400f31 commit a41d92f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/validate/validate.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 6 additions & 1 deletion src/whatsapp/controllers/instance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ConfigService, HttpServer, WaBusiness } from '../../config/env.config';
import { Logger } from '../../config/logger.config';
import { BadRequestException, InternalServerErrorException } from '../../exceptions';
import { RedisCache } from '../../libs/redis.client';
import { InstanceDto } from '../dto/instance.dto';
import { InstanceDto, SetPresenceDto } from '../dto/instance.dto';
import { RepositoryBroker } from '../repository/repository.manager';
import { AuthService, OldToken } from '../services/auth.service';
import { CacheService } from '../services/cache.service';
Expand Down Expand Up @@ -656,6 +656,11 @@ export class InstanceController {
return this.waMonitor.instanceInfo();
}

public async setPresence({ instanceName }: InstanceDto, data: SetPresenceDto) {
this.logger.verbose('requested sendPresence from ' + instanceName + ' instance');
return await this.waMonitor.waInstances[instanceName].setPresence(data);
}

public async logout({ instanceName }: InstanceDto) {
this.logger.verbose('requested logout from ' + instanceName + ' instance');
const { instance } = await this.connectionState({ instanceName });
Expand Down
6 changes: 6 additions & 0 deletions src/whatsapp/dto/instance.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { WAPresence } from "@whiskeysockets/baileys";

export class InstanceDto {
instanceName: string;
instanceId?: string;
Expand Down Expand Up @@ -40,3 +42,7 @@ export class InstanceDto {
typebot_listening_from_me?: boolean;
proxy?: string;
}

export class SetPresenceDto {
presence: WAPresence;
}
1 change: 1 addition & 0 deletions src/whatsapp/routers/chat.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
messageUpSchema,
messageValidateSchema,
presenceSchema,
presenceOnlySchema,
privacySettingsSchema,
profileNameSchema,
profilePictureSchema,
Expand Down
20 changes: 18 additions & 2 deletions src/whatsapp/routers/instance.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { RequestHandler, Router } from 'express';
import { Auth, ConfigService, Database } from '../../config/env.config';
import { Logger } from '../../config/logger.config';
import { dbserver } from '../../libs/db.connect';
import { instanceNameSchema, oldTokenSchema } from '../../validate/validate.schema';
import {instanceNameSchema, oldTokenSchema, presenceOnlySchema} from '../../validate/validate.schema';
import { RouterBroker } from '../abstract/abstract.router';
import { InstanceDto } from '../dto/instance.dto';
import { InstanceDto, SetPresenceDto } from '../dto/instance.dto';
import { OldToken } from '../services/auth.service';
import { instanceController } from '../whatsapp.module';
import { HttpStatus } from './index.router';
Expand Down Expand Up @@ -98,6 +98,22 @@ export class InstanceRouter extends RouterBroker {

return res.status(HttpStatus.OK).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) => instanceController.setPresence(instance, data),
});

return res.status(HttpStatus.CREATED).json(response);
})
.delete(this.routerPath('logout'), ...guards, async (req, res) => {
logger.verbose('request received in logoutInstances');
logger.verbose('request body: ');
Expand Down
14 changes: 12 additions & 2 deletions src/whatsapp/services/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import {
GroupUpdateParticipantDto,
GroupUpdateSettingDto,
} from '../dto/group.dto';
import { InstanceDto } from '../dto/instance.dto';
import { InstanceDto, SetPresenceDto } from '../dto/instance.dto';
import { HandleLabelDto, LabelDto } from '../dto/label.dto';
import {
ContactMessage,
Expand Down Expand Up @@ -1830,7 +1830,6 @@ export class BaileysStartupService extends WAStartupService {
}

// Instance Controller

public async sendPresence(data: SendPresenceDto) {
try {
const { number } = data;
Expand Down Expand Up @@ -1863,6 +1862,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');
Expand Down
3 changes: 3 additions & 0 deletions src/whatsapp/services/whatsapp.business.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down

0 comments on commit a41d92f

Please sign in to comment.