mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 09:51:24 -06:00
feat: send list and buttons
This commit is contained in:
parent
da77af5bd1
commit
bc751bbb6b
@ -1,5 +1,6 @@
|
|||||||
import { NumberBusiness } from '@api/dto/chat.dto';
|
import { NumberBusiness } from '@api/dto/chat.dto';
|
||||||
import {
|
import {
|
||||||
|
Button,
|
||||||
ContactMessage,
|
ContactMessage,
|
||||||
MediaMessage,
|
MediaMessage,
|
||||||
Options,
|
Options,
|
||||||
@ -12,6 +13,7 @@ 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';
|
||||||
@ -24,12 +26,14 @@ 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(
|
||||||
@ -1108,42 +1112,97 @@ export class BusinessStartupService extends ChannelStartupService {
|
|||||||
return audioSent;
|
return audioSent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async buttonMessage(data: SendButtonsDto) {
|
private toJSONString(button: Button): string {
|
||||||
const embeddedMedia: any = {};
|
const toString = (obj: any) => JSON.stringify(obj);
|
||||||
|
|
||||||
const btnItems = {
|
const json = {
|
||||||
text: data.buttons.map((btn) => btn.text),
|
call: () => toString({ display_text: button.displayText, phone_number: button.phoneNumber }),
|
||||||
ids: data.buttons.map((btn) => btn.id),
|
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,
|
||||||
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!arrayUnique(btnItems.text) || !arrayUnique(btnItems.ids)) {
|
return json[button.type]?.() || '';
|
||||||
throw new BadRequestException('Button texts cannot be repeated', 'Button IDs cannot be repeated.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return await this.sendMessageWithTyping(
|
private readonly mapType = new Map<TypeButton, string>([
|
||||||
data.number,
|
['reply', 'quick_reply'],
|
||||||
{
|
['copy', 'cta_copy'],
|
||||||
text: !embeddedMedia?.mediaKey ? data.title : undefined,
|
['url', 'cta_url'],
|
||||||
buttons: data.buttons.map((button) => {
|
['call', 'cta_call'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
public async buttonMessage(data: SendButtonsDto) {
|
||||||
|
const generate = await (async () => {
|
||||||
|
if (data?.thumbnailUrl) {
|
||||||
|
return await this.prepareMediaMessage({
|
||||||
|
mediatype: 'image',
|
||||||
|
media: data.thumbnailUrl,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
const buttons = data.buttons.map((value) => {
|
||||||
return {
|
return {
|
||||||
type: 'reply',
|
name: this.mapType.get(value.type),
|
||||||
reply: {
|
buttonParamsJson: this.toJSONString(value),
|
||||||
title: button.text,
|
};
|
||||||
id: button.id,
|
});
|
||||||
|
|
||||||
|
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(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}),
|
|
||||||
[embeddedMedia?.mediaKey]: embeddedMedia?.message,
|
return await this.sendMessageWithTyping(data.number, message, {
|
||||||
},
|
|
||||||
{
|
|
||||||
delay: data?.delay,
|
delay: data?.delay,
|
||||||
presence: 'composing',
|
presence: 'composing',
|
||||||
quoted: data?.quoted,
|
quoted: data?.quoted,
|
||||||
linkPreview: data?.linkPreview,
|
|
||||||
mentionsEveryOne: data?.mentionsEveryOne,
|
mentionsEveryOne: data?.mentionsEveryOne,
|
||||||
mentioned: data?.mentioned,
|
mentioned: data?.mentioned,
|
||||||
},
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async locationMessage(data: SendLocationDto) {
|
public async locationMessage(data: SendLocationDto) {
|
||||||
|
Loading…
Reference in New Issue
Block a user