mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 01:41:24 -06:00
fix: send buttons cloud api oficial
This commit is contained in:
parent
84451ba3e0
commit
5401ecd2c4
@ -1,6 +1,5 @@
|
|||||||
import { NumberBusiness } from '@api/dto/chat.dto';
|
import { NumberBusiness } from '@api/dto/chat.dto';
|
||||||
import {
|
import {
|
||||||
Button,
|
|
||||||
ContactMessage,
|
ContactMessage,
|
||||||
MediaMessage,
|
MediaMessage,
|
||||||
Options,
|
Options,
|
||||||
@ -13,7 +12,6 @@ import {
|
|||||||
SendReactionDto,
|
SendReactionDto,
|
||||||
SendTemplateDto,
|
SendTemplateDto,
|
||||||
SendTextDto,
|
SendTextDto,
|
||||||
TypeButton,
|
|
||||||
} from '@api/dto/sendMessage.dto';
|
} from '@api/dto/sendMessage.dto';
|
||||||
import * as s3Service from '@api/integrations/storage/s3/libs/minio.server';
|
import * as s3Service from '@api/integrations/storage/s3/libs/minio.server';
|
||||||
import { ProviderFiles } from '@api/provider/sessions';
|
import { ProviderFiles } from '@api/provider/sessions';
|
||||||
@ -26,14 +24,12 @@ import { Chatwoot, ConfigService, Database, Openai, S3, WaBusiness } from '@conf
|
|||||||
import { BadRequestException, InternalServerErrorException } from '@exceptions';
|
import { BadRequestException, InternalServerErrorException } from '@exceptions';
|
||||||
import { status } from '@utils/renderStatus';
|
import { status } from '@utils/renderStatus';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { proto } from 'baileys';
|
|
||||||
import { arrayUnique, isURL } from 'class-validator';
|
import { arrayUnique, isURL } from 'class-validator';
|
||||||
import EventEmitter2 from 'eventemitter2';
|
import EventEmitter2 from 'eventemitter2';
|
||||||
import FormData from 'form-data';
|
import FormData from 'form-data';
|
||||||
import { createReadStream } from 'fs';
|
import { createReadStream } from 'fs';
|
||||||
import mime from 'mime';
|
import mime from 'mime';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { v4 } from 'uuid';
|
|
||||||
|
|
||||||
export class BusinessStartupService extends ChannelStartupService {
|
export class BusinessStartupService extends ChannelStartupService {
|
||||||
constructor(
|
constructor(
|
||||||
@ -1112,97 +1108,42 @@ export class BusinessStartupService extends ChannelStartupService {
|
|||||||
return audioSent;
|
return audioSent;
|
||||||
}
|
}
|
||||||
|
|
||||||
private toJSONString(button: Button): string {
|
|
||||||
const toString = (obj: any) => JSON.stringify(obj);
|
|
||||||
|
|
||||||
const json = {
|
|
||||||
call: () => toString({ display_text: button.displayText, phone_number: button.phoneNumber }),
|
|
||||||
reply: () => toString({ display_text: button.displayText, id: button.id }),
|
|
||||||
copy: () => toString({ display_text: button.displayText, copy_code: button.copyCode }),
|
|
||||||
url: () =>
|
|
||||||
toString({
|
|
||||||
display_text: button.displayText,
|
|
||||||
url: button.url,
|
|
||||||
merchant_url: button.url,
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
|
|
||||||
return json[button.type]?.() || '';
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly mapType = new Map<TypeButton, string>([
|
|
||||||
['reply', 'quick_reply'],
|
|
||||||
['copy', 'cta_copy'],
|
|
||||||
['url', 'cta_url'],
|
|
||||||
['call', 'cta_call'],
|
|
||||||
]);
|
|
||||||
|
|
||||||
public async buttonMessage(data: SendButtonsDto) {
|
public async buttonMessage(data: SendButtonsDto) {
|
||||||
const generate = await (async () => {
|
const embeddedMedia: any = {};
|
||||||
if (data?.thumbnailUrl) {
|
|
||||||
return await this.prepareMediaMessage({
|
|
||||||
mediatype: 'image',
|
|
||||||
media: data.thumbnailUrl,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
const buttons = data.buttons.map((value) => {
|
const btnItems = {
|
||||||
return {
|
text: data.buttons.map((btn) => btn.displayText),
|
||||||
name: this.mapType.get(value.type),
|
ids: data.buttons.map((btn) => btn.id),
|
||||||
buttonParamsJson: this.toJSONString(value),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const message: proto.IMessage = {
|
|
||||||
viewOnceMessage: {
|
|
||||||
message: {
|
|
||||||
messageContextInfo: {
|
|
||||||
deviceListMetadata: {},
|
|
||||||
deviceListMetadataVersion: 2,
|
|
||||||
},
|
|
||||||
interactiveMessage: {
|
|
||||||
body: {
|
|
||||||
text: (() => {
|
|
||||||
let t = '*' + data.title + '*';
|
|
||||||
if (data?.description) {
|
|
||||||
t += '\n\n';
|
|
||||||
t += data.description;
|
|
||||||
t += '\n';
|
|
||||||
}
|
|
||||||
return t;
|
|
||||||
})(),
|
|
||||||
},
|
|
||||||
footer: {
|
|
||||||
text: data?.footer,
|
|
||||||
},
|
|
||||||
header: (() => {
|
|
||||||
if (generate?.message?.imageMessage) {
|
|
||||||
return {
|
|
||||||
hasMediaAttachment: !!generate.message.imageMessage,
|
|
||||||
imageMessage: generate.message.imageMessage,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
})(),
|
|
||||||
nativeFlowMessage: {
|
|
||||||
buttons: buttons,
|
|
||||||
messageParamsJson: JSON.stringify({
|
|
||||||
from: 'api',
|
|
||||||
templateId: v4(),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return await this.sendMessageWithTyping(data.number, message, {
|
if (!arrayUnique(btnItems.text) || !arrayUnique(btnItems.ids)) {
|
||||||
delay: data?.delay,
|
throw new BadRequestException('Button texts cannot be repeated', 'Button IDs cannot be repeated.');
|
||||||
presence: 'composing',
|
}
|
||||||
quoted: data?.quoted,
|
|
||||||
mentionsEveryOne: data?.mentionsEveryOne,
|
return await this.sendMessageWithTyping(
|
||||||
mentioned: data?.mentioned,
|
data.number,
|
||||||
});
|
{
|
||||||
|
text: !embeddedMedia?.mediaKey ? data.title : undefined,
|
||||||
|
buttons: data.buttons.map((button) => {
|
||||||
|
return {
|
||||||
|
type: 'reply',
|
||||||
|
reply: {
|
||||||
|
title: button.displayText,
|
||||||
|
id: button.id,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
[embeddedMedia?.mediaKey]: embeddedMedia?.message,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
delay: data?.delay,
|
||||||
|
presence: 'composing',
|
||||||
|
quoted: data?.quoted,
|
||||||
|
linkPreview: data?.linkPreview,
|
||||||
|
mentionsEveryOne: data?.mentionsEveryOne,
|
||||||
|
mentioned: data?.mentioned,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async locationMessage(data: SendLocationDto) {
|
public async locationMessage(data: SendLocationDto) {
|
||||||
|
@ -2807,8 +2807,6 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log(JSON.stringify(message));
|
|
||||||
|
|
||||||
return await this.sendMessageWithTyping(data.number, message, {
|
return await this.sendMessageWithTyping(data.number, message, {
|
||||||
delay: data?.delay,
|
delay: data?.delay,
|
||||||
presence: 'composing',
|
presence: 'composing',
|
||||||
|
Loading…
Reference in New Issue
Block a user