Repository navigation
Expand file tree
/
Copy pathsendMessage.controller.ts
More file actions
171 lines (147 loc) · 5.82 KB
/
Copy pathsendMessage.controller.ts
File metadata and controls
171 lines (147 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { InstanceDto } from '@api/dto/instance.dto';
import {
MessageCapabilitiesDto,
SendAudioDto,
SendButtonsDto,
SendCarouselDto,
SendContactDto,
SendListDto,
SendLocationDto,
SendMediaDto,
SendPollDto,
SendPtvDto,
SendReactionDto,
SendStatusDto,
SendStickerDto,
SendTemplateDto,
SendTextDto,
} from '@api/dto/sendMessage.dto';
import { WAMonitoringService } from '@api/services/monitor.service';
import { Integration } from '@api/types/wa.types';
import { BadRequestException } from '@exceptions';
import { isBase64, isURL } from 'class-validator';
import emojiRegex from 'emoji-regex';
const regex = emojiRegex();
function isEmoji(str: string) {
if (str === '') return true;
const match = str.match(regex);
return match?.length === 1 && match[0] === str;
}
export class SendMessageController {
constructor(private readonly waMonitor: WAMonitoringService) {}
public async getCapabilities({ instanceName }: InstanceDto): Promise<MessageCapabilitiesDto> {
const integration = await this.waMonitor.waInstances[instanceName].integration;
const supportedMessageTypes = this.resolveSupportedMessageTypes(integration);
const supportsInboundCatalogOrders = integration === Integration.WHATSAPP_BAILEYS;
return {
integration,
supportedMessageTypes,
chatCommerce: {
outboundProduct: false,
outboundMultiProduct: false,
inboundOrderNormalization: supportsInboundCatalogOrders,
officialApiRequired: true,
reason:
'Inbound catalog orders are normalized for Chatwoot on Baileys. Outbound catalog messages still require the official WhatsApp Business API.',
},
};
}
public async sendTemplate({ instanceName }: InstanceDto, data: SendTemplateDto) {
return await this.waMonitor.waInstances[instanceName].templateMessage(data);
}
public async sendText({ instanceName }: InstanceDto, data: SendTextDto) {
return await this.waMonitor.waInstances[instanceName].textMessage(data);
}
public async sendMedia({ instanceName }: InstanceDto, data: SendMediaDto, file?: any) {
if (isBase64(data?.media) && !data?.fileName && data?.mediatype === 'document') {
throw new BadRequestException('For base64 the file name must be informed.');
}
if (file || isURL(data?.media) || isBase64(data?.media)) {
return await this.waMonitor.waInstances[instanceName].mediaMessage(data, file);
}
throw new BadRequestException('Owned media must be a url or base64');
}
public async sendPtv({ instanceName }: InstanceDto, data: SendPtvDto, file?: any) {
if (file || isURL(data?.video) || isBase64(data?.video)) {
return await this.waMonitor.waInstances[instanceName].ptvMessage(data, file);
}
throw new BadRequestException('Owned media must be a url or base64');
}
public async sendSticker({ instanceName }: InstanceDto, data: SendStickerDto, file?: any) {
if (file || isURL(data.sticker) || isBase64(data.sticker)) {
return await this.waMonitor.waInstances[instanceName].mediaSticker(data, file);
}
throw new BadRequestException('Owned media must be a url or base64');
}
public async sendWhatsAppAudio({ instanceName }: InstanceDto, data: SendAudioDto, file?: any) {
if (file?.buffer || isURL(data.audio) || isBase64(data.audio)) {
// Si file existe y tiene buffer, o si es una URL o Base64, continúa
return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data, file);
} else {
console.error('El archivo no tiene buffer o el audio no es una URL o Base64 válida');
throw new BadRequestException('Owned media must be a url, base64, or valid file with buffer');
}
}
public async sendButtons({ instanceName }: InstanceDto, data: SendButtonsDto) {
return await this.waMonitor.waInstances[instanceName].buttonMessage(data);
}
public async sendLocation({ instanceName }: InstanceDto, data: SendLocationDto) {
return await this.waMonitor.waInstances[instanceName].locationMessage(data);
}
public async sendList({ instanceName }: InstanceDto, data: SendListDto) {
return await this.waMonitor.waInstances[instanceName].listMessage(data);
}
public async sendCarousel({ instanceName }: InstanceDto, data: SendCarouselDto) {
return await this.waMonitor.waInstances[instanceName].carouselMessage(data);
}
public async sendContact({ instanceName }: InstanceDto, data: SendContactDto) {
return await this.waMonitor.waInstances[instanceName].contactMessage(data);
}
public async sendReaction({ instanceName }: InstanceDto, data: SendReactionDto) {
if (!isEmoji(data.reaction)) {
throw new BadRequestException('Reaction must be a single emoji or empty string');
}
return await this.waMonitor.waInstances[instanceName].reactionMessage(data);
}
public async sendPoll({ instanceName }: InstanceDto, data: SendPollDto) {
return await this.waMonitor.waInstances[instanceName].pollMessage(data);
}
public async sendStatus({ instanceName }: InstanceDto, data: SendStatusDto, file?: any) {
return await this.waMonitor.waInstances[instanceName].statusMessage(data, file);
}
private resolveSupportedMessageTypes(integration: string): string[] {
if (integration === Integration.EVOLUTION) {
return ['TEXT', 'MEDIA', 'AUDIO', 'INTERACTIVE_BUTTONS'];
}
if (integration === Integration.WHATSAPP_BUSINESS) {
return [
'TEXT',
'MEDIA',
'AUDIO',
'STICKER',
'TEMPLATE',
'LOCATION',
'CONTACT',
'REACTION',
'POLL',
'STATUS',
'INTERACTIVE_BUTTONS',
'INTERACTIVE_LIST',
];
}
return [
'TEXT',
'MEDIA',
'AUDIO',
'STICKER',
'LOCATION',
'CONTACT',
'REACTION',
'POLL',
'STATUS',
'INTERACTIVE_BUTTONS',
'INTERACTIVE_LIST',
'CATALOG_ORDER',
];
}
}