integrations: adjusts in payloads

This commit is contained in:
Davidson Gomes 2024-06-07 19:16:51 -03:00
parent 2cb6bdb38b
commit cbb5d1d048
14 changed files with 305 additions and 129 deletions

View File

@ -6,8 +6,8 @@ export class Quoted {
}
export class Mentions {
everyOne: boolean;
mentioned: string[];
everyOne?: boolean;
mentioned?: string[];
}
export class Options {
@ -44,8 +44,8 @@ export class Metadata {
delay?: number;
quoted?: Quoted;
linkPreview?: boolean;
everyOne: boolean;
mentioned: string[];
everyOne?: boolean;
mentioned?: string[];
encoding?: boolean;
}

View File

@ -972,14 +972,9 @@ export class ChatwootService {
if (type === 'audio') {
const data: SendAudioDto = {
number: number,
audioMessage: {
audio: media,
},
options: {
delay: 1200,
presence: 'recording',
...options,
},
quoted: options?.quoted,
};
const messageSent = await waInstance?.audioWhatsapp(data, true);
@ -993,20 +988,15 @@ export class ChatwootService {
const data: SendMediaDto = {
number: number,
mediaMessage: {
mediatype: type as any,
fileName: fileName,
media: media,
},
options: {
delay: 1200,
presence: 'composing',
...options,
},
quoted: options?.quoted,
};
if (caption) {
data.mediaMessage.caption = caption;
data.caption = caption;
}
const messageSent = await waInstance?.mediaMessage(data, true);
@ -1232,14 +1222,9 @@ export class ChatwootService {
} else {
const data: SendTextDto = {
number: chatId,
textMessage: {
text: formatText,
},
options: {
delay: 1200,
presence: 'composing',
quoted: await this.getQuotedMessage(body, instance),
},
};
let messageSent: any;
@ -1350,13 +1335,8 @@ export class ChatwootService {
if (body.message_type === 'template' && body.event === 'message_created') {
const data: SendTextDto = {
number: chatId,
textMessage: {
text: body.content.replace(/\\\r\n|\\\n|\n/g, '\n'),
},
options: {
delay: 1200,
presence: 'composing',
},
};
await waInstance?.textMessage(data);

View File

@ -683,13 +683,8 @@ export class TypebotService {
if (unknownMessage) {
this.waMonitor.waInstances[instance.instanceName].textMessage({
number: remoteJid.split('@')[0],
options: {
delay: delayMessage || 1000,
presence: 'composing',
},
textMessage: {
text: unknownMessage,
},
});
}
return;
@ -782,13 +777,8 @@ export class TypebotService {
if (unknownMessage) {
this.waMonitor.waInstances[instance.instanceName].textMessage({
number: remoteJid.split('@')[0],
options: {
delay: delayMessage || 1000,
presence: 'composing',
},
textMessage: {
text: unknownMessage,
},
});
}
return;
@ -874,13 +864,8 @@ export class TypebotService {
if (unknownMessage) {
this.waMonitor.waInstances[instance.instanceName].textMessage({
number: remoteJid.split('@')[0],
options: {
delay: delayMessage || 1000,
presence: 'composing',
},
textMessage: {
text: unknownMessage,
},
});
}
return;

View File

@ -838,9 +838,18 @@ export class BusinessStartupService extends ChannelStartupService {
const res = await this.sendMessageWithTyping(
data.number,
{
conversation: data.textMessage.text,
conversation: data.text,
},
{
delay: data?.delay,
presence: 'composing',
quoted: data?.quoted,
linkPreview: data?.linkPreview,
mentions: {
everyOne: data?.everyOne,
mentioned: data?.mentioned,
},
},
data?.options,
isChatwoot,
);
return res;
@ -915,9 +924,23 @@ export class BusinessStartupService extends ChannelStartupService {
}
public async mediaMessage(data: SendMediaDto, isChatwoot = false) {
const message = await this.prepareMediaMessage(data.mediaMessage);
const message = await this.prepareMediaMessage(data);
return await this.sendMessageWithTyping(data.number, { ...message }, data?.options, isChatwoot);
return await this.sendMessageWithTyping(
data.number,
{ ...message },
{
delay: data?.delay,
presence: 'composing',
quoted: data?.quoted,
linkPreview: data?.linkPreview,
mentions: {
everyOne: data?.everyOne,
mentioned: data?.mentioned,
},
},
isChatwoot,
);
}
public async processAudio(audio: string, number: string) {
@ -949,22 +972,27 @@ export class BusinessStartupService extends ChannelStartupService {
}
public async audioWhatsapp(data: SendAudioDto, isChatwoot = false) {
const message = await this.processAudio(data.audioMessage.audio, data.number);
const message = await this.processAudio(data.audio, data.number);
return await this.sendMessageWithTyping(data.number, { ...message }, data?.options, isChatwoot);
return await this.sendMessageWithTyping(
data.number,
{ ...message },
{
delay: data?.delay,
presence: 'composing',
quoted: data?.quoted,
linkPreview: data?.linkPreview,
mentions: {
everyOne: data?.everyOne,
mentioned: data?.mentioned,
},
},
isChatwoot,
);
}
public async buttonMessage(data: SendButtonDto) {
const embeddedMedia: any = {};
let mediatype = 'TEXT';
if (data.buttonMessage?.mediaMessage) {
mediatype = data.buttonMessage.mediaMessage?.mediatype.toUpperCase() ?? 'TEXT';
embeddedMedia.mediaKey = mediatype.toLowerCase() + 'Message';
const generate = await this.prepareMediaMessage(data.buttonMessage.mediaMessage);
embeddedMedia.message = generate.message[embeddedMedia.mediaKey];
embeddedMedia.contentText = `*${data.buttonMessage.title}*\n\n${data.buttonMessage.description}`;
}
const btnItems = {
text: data.buttonMessage.buttons.map((btn) => btn.buttonText),
@ -990,7 +1018,16 @@ export class BusinessStartupService extends ChannelStartupService {
}),
[embeddedMedia?.mediaKey]: embeddedMedia?.message,
},
data?.options,
{
delay: data?.delay,
presence: 'composing',
quoted: data?.quoted,
linkPreview: data?.linkPreview,
mentions: {
everyOne: data?.everyOne,
mentioned: data?.mentioned,
},
},
);
}
@ -999,19 +1036,28 @@ export class BusinessStartupService extends ChannelStartupService {
data.number,
{
locationMessage: {
degreesLatitude: data.locationMessage.latitude,
degreesLongitude: data.locationMessage.longitude,
name: data.locationMessage?.name,
address: data.locationMessage?.address,
degreesLatitude: data.latitude,
degreesLongitude: data.longitude,
name: data?.name,
address: data?.address,
},
},
{
delay: data?.delay,
presence: 'composing',
quoted: data?.quoted,
linkPreview: data?.linkPreview,
mentions: {
everyOne: data?.everyOne,
mentioned: data?.mentioned,
},
},
data?.options,
);
}
public async listMessage(data: SendListDto) {
const sectionsItems = {
title: data.listMessage.sections.map((list) => list.title),
title: data.sections.map((list) => list.title),
};
if (!arrayUnique(sectionsItems.title)) {
@ -1021,11 +1067,11 @@ export class BusinessStartupService extends ChannelStartupService {
return await this.sendMessageWithTyping(
data.number,
{
title: data.listMessage.title,
text: data.listMessage.description,
footerText: data.listMessage?.footerText,
buttonText: data.listMessage?.buttonText,
sections: data.listMessage.sections.map((section) => {
title: data.title,
text: data.description,
footerText: data?.footerText,
buttonText: data?.buttonText,
sections: data.sections.map((section) => {
return {
title: section.title,
rows: section.rows.map((row) => {
@ -1038,7 +1084,16 @@ export class BusinessStartupService extends ChannelStartupService {
};
}),
},
data?.options,
{
delay: data?.delay,
presence: 'composing',
quoted: data?.quoted,
linkPreview: data?.linkPreview,
mentions: {
everyOne: data?.everyOne,
mentioned: data?.mentioned,
},
},
);
}
@ -1052,7 +1107,16 @@ export class BusinessStartupService extends ChannelStartupService {
components: data.components,
},
},
data?.options,
{
delay: data?.delay,
presence: 'composing',
quoted: data?.quoted,
linkPreview: data?.linkPreview,
mentions: {
everyOne: data?.everyOne,
mentioned: data?.mentioned,
},
},
isChatwoot,
);
return res;
@ -1085,15 +1149,15 @@ export class BusinessStartupService extends ChannelStartupService {
return result;
};
if (data.contactMessage.length === 1) {
message.contactMessage = {
displayName: data.contactMessage[0].fullName,
vcard: vcard(data.contactMessage[0]),
if (data.contact.length === 1) {
message.contact = {
displayName: data.contact[0].fullName,
vcard: vcard(data.contact[0]),
};
} else {
message.contactsArrayMessage = {
displayName: `${data.contactMessage.length} contacts`,
contacts: data.contactMessage.map((contact) => {
displayName: `${data.contact.length} contacts`,
contacts: data.contact.map((contact) => {
return {
displayName: contact.fullName,
vcard: vcard(contact),
@ -1104,7 +1168,7 @@ export class BusinessStartupService extends ChannelStartupService {
return await this.sendMessageWithTyping(
data.number,
{
contacts: data.contactMessage.map((contact) => {
contacts: data.contact.map((contact) => {
return {
name: { formatted_name: contact.fullName, first_name: contact.fullName },
phones: [{ phone: contact.phoneNumber }],
@ -1115,15 +1179,24 @@ export class BusinessStartupService extends ChannelStartupService {
}),
message,
},
data?.options,
{
delay: data?.delay,
presence: 'composing',
quoted: data?.quoted,
linkPreview: data?.linkPreview,
mentions: {
everyOne: data?.everyOne,
mentioned: data?.mentioned,
},
},
);
}
public async reactionMessage(data: SendReactionDto) {
return await this.sendMessageWithTyping(data.reactionMessage.key.remoteJid, {
return await this.sendMessageWithTyping(data.key.remoteJid, {
reactionMessage: {
key: data.reactionMessage.key,
text: data.reactionMessage.reaction,
key: data.key,
text: data.reaction,
},
});
}

View File

@ -1,7 +1,24 @@
import { JSONSchema7, JSONSchema7Definition } from 'json-schema';
import { v4 } from 'uuid';
import { isNotEmpty } from './validate.schema';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
};
};
const numberDefinition: JSONSchema7Definition = {
type: 'string',

View File

@ -1,7 +1,24 @@
import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid';
import { isNotEmpty } from './validate.schema';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
};
};
export const createGroupSchema: JSONSchema7 = {
$id: v4(),

View File

@ -2,7 +2,26 @@ import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid';
import { Integration } from '../api/types/wa.types';
import { Events, isNotEmpty } from './validate.schema';
import { Events } from './validate.schema';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
};
};
export const instanceSchema: JSONSchema7 = {
$id: v4(),

View File

@ -1,7 +1,24 @@
import { JSONSchema7, JSONSchema7Definition } from 'json-schema';
import { v4 } from 'uuid';
import { isNotEmpty } from './validate.schema';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
};
};
const numberDefinition: JSONSchema7Definition = {
type: 'string',

View File

@ -1,7 +1,24 @@
import { JSONSchema7, JSONSchema7Definition } from 'json-schema';
import { v4 } from 'uuid';
import { isNotEmpty } from './validate.schema';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
};
};
const numberDefinition: JSONSchema7Definition = {
type: 'string',

View File

@ -1,7 +1,24 @@
import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid';
import { isNotEmpty } from './validate.schema';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
};
};
export const proxySchema: JSONSchema7 = {
$id: v4(),

View File

@ -1,7 +1,24 @@
import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid';
import { isNotEmpty } from './validate.schema';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
};
};
export const settingsSchema: JSONSchema7 = {
$id: v4(),

View File

@ -1,5 +1,3 @@
import { JSONSchema7 } from 'json-schema';
// Integrations Schema
// TODO: rever todas as integrações e garantir o funcionamento perfeito
export * from '../api/integrations/chatwoot/validate/chatwoot.schema';
@ -18,25 +16,6 @@ export * from './settings.schema';
export * from './webhook.schema';
export * from './websocket.schema';
export const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
};
};
export const Events = [
'APPLICATION_STARTUP',
'QRCODE_UPDATED',

View File

@ -1,7 +1,26 @@
import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid';
import { Events, isNotEmpty } from './validate.schema';
import { Events } from './validate.schema';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
};
};
export const webhookSchema: JSONSchema7 = {
$id: v4(),

View File

@ -1,7 +1,26 @@
import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid';
import { Events, isNotEmpty } from './validate.schema';
import { Events } from './validate.schema';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
};
};
export const websocketSchema: JSONSchema7 = {
$id: v4(),