oex2003/evolution-api
0
1import { InstanceDto } from '@api/dto/instance.dto';2import {3 SendAudioDto,4 SendButtonsDto,5 SendContactDto,6 SendListDto,7 SendLocationDto,8 SendMediaDto,9 SendPollDto,10 SendPtvDto,11 SendReactionDto,12 SendStatusDto,13 SendStickerDto,14 SendTemplateDto,15 SendTextDto,16} from '@api/dto/sendMessage.dto';17import { WAMonitoringService } from '@api/services/monitor.service';18import { BadRequestException } from '@exceptions';19import { isBase64, isURL } from 'class-validator';20import emojiRegex from 'emoji-regex';21 22const regex = emojiRegex();23 24function isEmoji(str: string) {25 if (str === '') return true;26 27 const match = str.match(regex);28 return match?.length === 1 && match[0] === str;29}30 31export class SendMessageController {32 constructor(private readonly waMonitor: WAMonitoringService) {}33 34 public async sendTemplate({ instanceName }: InstanceDto, data: SendTemplateDto) {35 return await this.waMonitor.waInstances[instanceName].templateMessage(data);36 }37 38 public async sendText({ instanceName }: InstanceDto, data: SendTextDto) {39 return await this.waMonitor.waInstances[instanceName].textMessage(data);40 }41 42 public async sendMedia({ instanceName }: InstanceDto, data: SendMediaDto, file?: any) {43 if (isBase64(data?.media) && !data?.fileName && data?.mediatype === 'document') {44 throw new BadRequestException('For base64 the file name must be informed.');45 }46 47 if (file || isURL(data?.media) || isBase64(data?.media)) {48 return await this.waMonitor.waInstances[instanceName].mediaMessage(data, file);49 }50 throw new BadRequestException('Owned media must be a url or base64');51 }52 53 public async sendPtv({ instanceName }: InstanceDto, data: SendPtvDto, file?: any) {54 if (file || isURL(data?.video) || isBase64(data?.video)) {55 return await this.waMonitor.waInstances[instanceName].ptvMessage(data, file);56 }57 throw new BadRequestException('Owned media must be a url or base64');58 }59 60 public async sendSticker({ instanceName }: InstanceDto, data: SendStickerDto, file?: any) {61 if (file || isURL(data.sticker) || isBase64(data.sticker)) {62 return await this.waMonitor.waInstances[instanceName].mediaSticker(data, file);63 }64 throw new BadRequestException('Owned media must be a url or base64');65 }66 67 public async sendWhatsAppAudio({ instanceName }: InstanceDto, data: SendAudioDto, file?: any) {68 if (file?.buffer || isURL(data.audio) || isBase64(data.audio)) {69 // Si file existe y tiene buffer, o si es una URL o Base64, continúa70 return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data, file);71 } else {72 console.error('El archivo no tiene buffer o el audio no es una URL o Base64 válida');73 throw new BadRequestException('Owned media must be a url, base64, or valid file with buffer');74 }75 }76 77 public async sendButtons({ instanceName }: InstanceDto, data: SendButtonsDto) {78 return await this.waMonitor.waInstances[instanceName].buttonMessage(data);79 }80 81 public async sendLocation({ instanceName }: InstanceDto, data: SendLocationDto) {82 return await this.waMonitor.waInstances[instanceName].locationMessage(data);83 }84 85 public async sendList({ instanceName }: InstanceDto, data: SendListDto) {86 return await this.waMonitor.waInstances[instanceName].listMessage(data);87 }88 89 public async sendContact({ instanceName }: InstanceDto, data: SendContactDto) {90 return await this.waMonitor.waInstances[instanceName].contactMessage(data);91 }92 93 public async sendReaction({ instanceName }: InstanceDto, data: SendReactionDto) {94 if (!isEmoji(data.reaction)) {95 throw new BadRequestException('Reaction must be a single emoji or empty string');96 }97 return await this.waMonitor.waInstances[instanceName].reactionMessage(data);98 }99 100 public async sendPoll({ instanceName }: InstanceDto, data: SendPollDto) {101 return await this.waMonitor.waInstances[instanceName].pollMessage(data);102 }103 104 public async sendStatus({ instanceName }: InstanceDto, data: SendStatusDto, file?: any) {105 return await this.waMonitor.waInstances[instanceName].statusMessage(data, file);106 }107}108 