11import { NumberBusiness } from '@api/dto/chat.dto' ;
22import {
3- Button ,
43 ContactMessage ,
54 MediaMessage ,
65 Options ,
@@ -13,7 +12,6 @@ import {
1312 SendReactionDto ,
1413 SendTemplateDto ,
1514 SendTextDto ,
16- TypeButton ,
1715} from '@api/dto/sendMessage.dto' ;
1816import * as s3Service from '@api/integrations/storage/s3/libs/minio.server' ;
1917import { ProviderFiles } from '@api/provider/sessions' ;
@@ -26,14 +24,12 @@ import { Chatwoot, ConfigService, Database, Openai, S3, WaBusiness } from '@conf
2624import { BadRequestException , InternalServerErrorException } from '@exceptions' ;
2725import { status } from '@utils/renderStatus' ;
2826import axios from 'axios' ;
29- import { proto } from 'baileys' ;
3027import { arrayUnique , isURL } from 'class-validator' ;
3128import EventEmitter2 from 'eventemitter2' ;
3229import FormData from 'form-data' ;
3330import { createReadStream } from 'fs' ;
3431import mime from 'mime' ;
3532import { join } from 'path' ;
36- import { v4 } from 'uuid' ;
3733
3834export class BusinessStartupService extends ChannelStartupService {
3935 constructor (
@@ -1112,97 +1108,42 @@ export class BusinessStartupService extends ChannelStartupService {
11121108 return audioSent ;
11131109 }
11141110
1115- private toJSONString ( button : Button ) : string {
1116- const toString = ( obj : any ) => JSON . stringify ( obj ) ;
1111+ public async buttonMessage ( data : SendButtonsDto ) {
1112+ const embeddedMedia : any = { } ;
11171113
1118- const json = {
1119- call : ( ) => toString ( { display_text : button . displayText , phone_number : button . phoneNumber } ) ,
1120- reply : ( ) => toString ( { display_text : button . displayText , id : button . id } ) ,
1121- copy : ( ) => toString ( { display_text : button . displayText , copy_code : button . copyCode } ) ,
1122- url : ( ) =>
1123- toString ( {
1124- display_text : button . displayText ,
1125- url : button . url ,
1126- merchant_url : button . url ,
1127- } ) ,
1114+ const btnItems = {
1115+ text : data . buttons . map ( ( btn ) => btn . displayText ) ,
1116+ ids : data . buttons . map ( ( btn ) => btn . id ) ,
11281117 } ;
11291118
1130- return json [ button . type ] ?.( ) || '' ;
1131- }
1132-
1133- private readonly mapType = new Map < TypeButton , string > ( [
1134- [ 'reply' , 'quick_reply' ] ,
1135- [ 'copy' , 'cta_copy' ] ,
1136- [ 'url' , 'cta_url' ] ,
1137- [ 'call' , 'cta_call' ] ,
1138- ] ) ;
1139-
1140- public async buttonMessage ( data : SendButtonsDto ) {
1141- const generate = await ( async ( ) => {
1142- if ( data ?. thumbnailUrl ) {
1143- return await this . prepareMediaMessage ( {
1144- mediatype : 'image' ,
1145- media : data . thumbnailUrl ,
1146- } ) ;
1147- }
1148- } ) ( ) ;
1149-
1150- const buttons = data . buttons . map ( ( value ) => {
1151- return {
1152- name : this . mapType . get ( value . type ) ,
1153- buttonParamsJson : this . toJSONString ( value ) ,
1154- } ;
1155- } ) ;
1119+ if ( ! arrayUnique ( btnItems . text ) || ! arrayUnique ( btnItems . ids ) ) {
1120+ throw new BadRequestException ( 'Button texts cannot be repeated' , 'Button IDs cannot be repeated.' ) ;
1121+ }
11561122
1157- const message : proto . IMessage = {
1158- viewOnceMessage : {
1159- message : {
1160- messageContextInfo : {
1161- deviceListMetadata : { } ,
1162- deviceListMetadataVersion : 2 ,
1163- } ,
1164- interactiveMessage : {
1165- body : {
1166- text : ( ( ) => {
1167- let t = '*' + data . title + '*' ;
1168- if ( data ?. description ) {
1169- t += '\n\n' ;
1170- t += data . description ;
1171- t += '\n' ;
1172- }
1173- return t ;
1174- } ) ( ) ,
1175- } ,
1176- footer : {
1177- text : data ?. footer ,
1178- } ,
1179- header : ( ( ) => {
1180- if ( generate ?. message ?. imageMessage ) {
1181- return {
1182- hasMediaAttachment : ! ! generate . message . imageMessage ,
1183- imageMessage : generate . message . imageMessage ,
1184- } ;
1185- }
1186- } ) ( ) ,
1187- nativeFlowMessage : {
1188- buttons : buttons ,
1189- messageParamsJson : JSON . stringify ( {
1190- from : 'api' ,
1191- templateId : v4 ( ) ,
1192- } ) ,
1123+ return await this . sendMessageWithTyping (
1124+ data . number ,
1125+ {
1126+ text : ! embeddedMedia ?. mediaKey ? data . title : undefined ,
1127+ buttons : data . buttons . map ( ( button ) => {
1128+ return {
1129+ type : 'reply' ,
1130+ reply : {
1131+ title : button . displayText ,
1132+ id : button . id ,
11931133 } ,
1194- } ,
1195- } ,
1134+ } ;
1135+ } ) ,
1136+ [ embeddedMedia ?. mediaKey ] : embeddedMedia ?. message ,
11961137 } ,
1197- } ;
1198-
1199- return await this . sendMessageWithTyping ( data . number , message , {
1200- delay : data ?. delay ,
1201- presence : 'composing' ,
1202- quoted : data ?. quoted ,
1203- mentionsEveryOne : data ?. mentionsEveryOne ,
1204- mentioned : data ?. mentioned ,
1205- } ) ;
1138+ {
1139+ delay : data ?. delay ,
1140+ presence : 'composing' ,
1141+ quoted : data ?. quoted ,
1142+ linkPreview : data ?. linkPreview ,
1143+ mentionsEveryOne : data ?. mentionsEveryOne ,
1144+ mentioned : data ?. mentioned ,
1145+ } ,
1146+ ) ;
12061147 }
12071148
12081149 public async locationMessage ( data : SendLocationDto ) {
0 commit comments