@@ -31,10 +31,12 @@ import {
3131import { SetPresenceDto } from '@api/dto/instance.dto' ;
3232import { HandleLabelDto , LabelDto } from '@api/dto/label.dto' ;
3333import {
34+ Button ,
3435 ContactMessage ,
3536 MediaMessage ,
3637 Options ,
3738 SendAudioDto ,
39+ SendButtonsDto ,
3840 SendContactDto ,
3941 SendListDto ,
4042 SendLocationDto ,
@@ -45,6 +47,7 @@ import {
4547 SendStickerDto ,
4648 SendTextDto ,
4749 StatusMessage ,
50+ TypeButton ,
4851} from '@api/dto/sendMessage.dto' ;
4952import { ProviderFiles } from '@api/provider/sessions' ;
5053import { PrismaRepository } from '@api/repository/repository.service' ;
@@ -2111,8 +2114,97 @@ export class BaileysStartupService extends ChannelStartupService {
21112114 ) ;
21122115 }
21132116
2114- public async buttonMessage ( ) {
2115- throw new BadRequestException ( 'Method not available on WhatsApp Baileys' ) ;
2117+ private toJSONString ( button : Button ) : string {
2118+ const toString = ( obj : any ) => JSON . stringify ( obj ) ;
2119+
2120+ const json = {
2121+ call : ( ) => toString ( { display_text : button . displayText , phone_number : button . phoneNumber } ) ,
2122+ reply : ( ) => toString ( { display_text : button . displayText , id : button . id } ) ,
2123+ copy : ( ) => toString ( { display_text : button . displayText , copy_code : button . copyCode } ) ,
2124+ url : ( ) =>
2125+ toString ( {
2126+ display_text : button . displayText ,
2127+ url : button . url ,
2128+ merchant_url : button . url ,
2129+ } ) ,
2130+ } ;
2131+
2132+ return json [ button . type ] ?.( ) || '' ;
2133+ }
2134+
2135+ private readonly mapType = new Map < TypeButton , string > ( [
2136+ [ 'reply' , 'quick_reply' ] ,
2137+ [ 'copy' , 'cta_copy' ] ,
2138+ [ 'url' , 'cta_url' ] ,
2139+ [ 'call' , 'cta_call' ] ,
2140+ ] ) ;
2141+
2142+ public async buttonMessage ( data : SendButtonsDto ) {
2143+ const generate = await ( async ( ) => {
2144+ if ( data ?. thumbnailUrl ) {
2145+ return await this . prepareMediaMessage ( {
2146+ mediatype : 'image' ,
2147+ media : data . thumbnailUrl ,
2148+ } ) ;
2149+ }
2150+ } ) ( ) ;
2151+
2152+ const buttons = data . buttons . map ( ( value ) => {
2153+ return {
2154+ name : this . mapType . get ( value . type ) ,
2155+ buttonParamsJson : this . toJSONString ( value ) ,
2156+ } ;
2157+ } ) ;
2158+
2159+ const message : proto . IMessage = {
2160+ viewOnceMessage : {
2161+ message : {
2162+ messageContextInfo : {
2163+ deviceListMetadata : { } ,
2164+ deviceListMetadataVersion : 2 ,
2165+ } ,
2166+ interactiveMessage : {
2167+ body : {
2168+ text : ( ( ) => {
2169+ let t = '*' + data . title + '*' ;
2170+ if ( data ?. description ) {
2171+ t += '\n\n' ;
2172+ t += data . description ;
2173+ t += '\n' ;
2174+ }
2175+ return t ;
2176+ } ) ( ) ,
2177+ } ,
2178+ footer : {
2179+ text : data ?. footer ,
2180+ } ,
2181+ header : ( ( ) => {
2182+ if ( generate ?. message ?. imageMessage ) {
2183+ return {
2184+ hasMediaAttachment : ! ! generate . message . imageMessage ,
2185+ imageMessage : generate . message . imageMessage ,
2186+ } ;
2187+ }
2188+ } ) ( ) ,
2189+ nativeFlowMessage : {
2190+ buttons : buttons ,
2191+ messageParamsJson : JSON . stringify ( {
2192+ from : 'api' ,
2193+ templateId : v4 ( ) ,
2194+ } ) ,
2195+ } ,
2196+ } ,
2197+ } ,
2198+ } ,
2199+ } ;
2200+
2201+ return await this . sendMessageWithTyping ( data . number , message , {
2202+ delay : data ?. delay ,
2203+ presence : 'composing' ,
2204+ quoted : data ?. quoted ,
2205+ mentionsEveryOne : data ?. mentionsEveryOne ,
2206+ mentioned : data ?. mentioned ,
2207+ } ) ;
21162208 }
21172209
21182210 public async listMessage ( data : SendListDto ) {
0 commit comments