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 { export class Mentions {
everyOne: boolean; everyOne?: boolean;
mentioned: string[]; mentioned?: string[];
} }
export class Options { export class Options {
@ -44,8 +44,8 @@ export class Metadata {
delay?: number; delay?: number;
quoted?: Quoted; quoted?: Quoted;
linkPreview?: boolean; linkPreview?: boolean;
everyOne: boolean; everyOne?: boolean;
mentioned: string[]; mentioned?: string[];
encoding?: boolean; encoding?: boolean;
} }

View File

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

View File

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

View File

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

View File

@ -1,7 +1,24 @@
import { JSONSchema7, JSONSchema7Definition } from 'json-schema'; import { JSONSchema7, JSONSchema7Definition } from 'json-schema';
import { v4 } from 'uuid'; 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 = { const numberDefinition: JSONSchema7Definition = {
type: 'string', type: 'string',

View File

@ -1,7 +1,24 @@
import { JSONSchema7 } from 'json-schema'; import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid'; 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 = { export const createGroupSchema: JSONSchema7 = {
$id: v4(), $id: v4(),

View File

@ -2,7 +2,26 @@ import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid'; import { v4 } from 'uuid';
import { Integration } from '../api/types/wa.types'; 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 = { export const instanceSchema: JSONSchema7 = {
$id: v4(), $id: v4(),

View File

@ -1,7 +1,24 @@
import { JSONSchema7, JSONSchema7Definition } from 'json-schema'; import { JSONSchema7, JSONSchema7Definition } from 'json-schema';
import { v4 } from 'uuid'; 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 = { const numberDefinition: JSONSchema7Definition = {
type: 'string', type: 'string',

View File

@ -1,7 +1,24 @@
import { JSONSchema7, JSONSchema7Definition } from 'json-schema'; import { JSONSchema7, JSONSchema7Definition } from 'json-schema';
import { v4 } from 'uuid'; 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 = { const numberDefinition: JSONSchema7Definition = {
type: 'string', type: 'string',

View File

@ -1,7 +1,24 @@
import { JSONSchema7 } from 'json-schema'; import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid'; 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 = { export const proxySchema: JSONSchema7 = {
$id: v4(), $id: v4(),

View File

@ -1,7 +1,24 @@
import { JSONSchema7 } from 'json-schema'; import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid'; 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 = { export const settingsSchema: JSONSchema7 = {
$id: v4(), $id: v4(),

View File

@ -1,5 +1,3 @@
import { JSONSchema7 } from 'json-schema';
// Integrations Schema // Integrations Schema
// TODO: rever todas as integrações e garantir o funcionamento perfeito // TODO: rever todas as integrações e garantir o funcionamento perfeito
export * from '../api/integrations/chatwoot/validate/chatwoot.schema'; export * from '../api/integrations/chatwoot/validate/chatwoot.schema';
@ -18,25 +16,6 @@ export * from './settings.schema';
export * from './webhook.schema'; export * from './webhook.schema';
export * from './websocket.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 = [ export const Events = [
'APPLICATION_STARTUP', 'APPLICATION_STARTUP',
'QRCODE_UPDATED', 'QRCODE_UPDATED',

View File

@ -1,7 +1,26 @@
import { JSONSchema7 } from 'json-schema'; import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid'; 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 = { export const webhookSchema: JSONSchema7 = {
$id: v4(), $id: v4(),

View File

@ -1,7 +1,26 @@
import { JSONSchema7 } from 'json-schema'; import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid'; 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 = { export const websocketSchema: JSONSchema7 = {
$id: v4(), $id: v4(),