@@ -2,6 +2,8 @@ import { Client, ev, SimpleListener, ChatId, Message, Contact } from '../..';
22import { app , cliFlags } from '../server' ;
33import { Request , Response } from "express" ;
44import { default as axios } from 'axios'
5+ import { default as FormData } from 'form-data'
6+ import mime from 'mime-types' ;
57
68export type expressMiddleware = ( req : Request , res : Response ) => Promise < Response < any , Record < string , any > > >
79
@@ -10,6 +12,8 @@ export const chatwootMiddleware: (cliConfig: cliFlags, client: Client) => expres
1012 const processMesssage = async ( ) => {
1113 const promises = [ ] ;
1214 const { body } = req
15+ if ( ! body ) return ;
16+ if ( ! body . conversation ) return ;
1317 const m = body . conversation . messages [ 0 ] ;
1418 const contact = ( body . conversation . meta . sender . phone_number || "" ) . replace ( '+' , '' )
1519 if (
@@ -69,15 +73,16 @@ export const setupChatwootOutgoingMessageHandler: (cliConfig: cliFlags, client:
6973 const [ accountId , inboxId ] = u . match ( / \/ ( a p p | ( a p i \/ v 1 ) ) \/ a c c o u n t s \/ \d * \/ i n b o x \/ \d * / g) [ 0 ] . split ( '/' ) . filter ( Number )
7074 // const accountId = u.match(/accounts\/\d*/g) && u.match(/accounts\/\d*/g)[0].replace('accounts/', '')
7175 const resolvedInbox = inboxId || u . match ( / i n b o x e s \/ \d * / g) && u . match ( / i n b o x e s \/ \d * / g) [ 0 ] . replace ( 'inboxes/' , '' )
72- const cwReq = ( path , method , data ?: any ) => {
76+ const cwReq = ( path , method , data ?: any , _headers ?: any ) => {
7377 const url = `${ origin } /api/v1/accounts/${ accountId } /${ path } ` . replace ( 'app.bentonow.com' , 'chat.bentonow.com' )
74- console . log ( url , method , data )
78+ // console.log(url,method,data)
7579 return axios ( {
7680 method,
7781 data,
7882 url,
7983 headers : {
80- api_access_token
84+ api_access_token,
85+ ..._headers
8186 }
8287 } )
8388}
@@ -114,7 +119,7 @@ export const setupChatwootOutgoingMessageHandler: (cliConfig: cliFlags, client:
114119 const getContactConversation = async ( number : string ) => {
115120 try {
116121 const { data } = await cwReq ( `contacts/${ contactReg [ number ] } /conversations` , 'get' ) ;
117- return data . payload [ 0 ] ;
122+ return data . payload . sort ( ( a , b ) => a . id - b . id ) [ 0 ] ;
118123 } catch ( error ) {
119124 return ;
120125 }
@@ -159,6 +164,26 @@ export const setupChatwootOutgoingMessageHandler: (cliConfig: cliFlags, client:
159164 }
160165 }
161166
167+ const sendAttachmentMessage = async ( content , contactId , message : Message ) => {
168+ // decrypt message
169+ const file = await client . decryptMedia ( message )
170+ let formData = new FormData ( ) ;
171+ formData . append ( 'attachments[]' , Buffer . from ( file . split ( ',' ) [ 1 ] , 'base64' ) , {
172+ knownLength : 1 ,
173+ filename : `${ message . t } .${ mime . extension ( message . mimetype ) } ` ,
174+ contentType : ( file . match ( / [ ^ : \s * ] \w + \/ [ \w - + \d . ] + (? = [ ; | ] ) / ) || [ "application/octet-stream" ] ) [ 0 ]
175+ } ) ;
176+ formData . append ( 'content' , content )
177+ formData . append ( 'message_type' , 'incoming' )
178+ try {
179+ const { data } = await cwReq ( `conversations/${ convoReg [ contactId ] } /messages` , 'post' , formData , formData . getHeaders ( ) ) ;
180+ return data ;
181+ } catch ( error ) {
182+ return ;
183+ }
184+ }
185+
186+
162187
163188 // const inboxId = s.match(/conversations\/\d*/g) && s.match(/conversations\/\d*/g)[0].replace('conversations/','')
164189 /**
@@ -195,6 +220,7 @@ export const setupChatwootOutgoingMessageHandler: (cliConfig: cliFlags, client:
195220 * Does the conversation exist in
196221 */
197222 let text = message . body ;
223+ let hasAttachments = false ;
198224 switch ( message . type ) {
199225 case 'location' :
200226 text = `${ message . lat } ,${ message . lng } ` ;
@@ -207,12 +233,18 @@ export const setupChatwootOutgoingMessageHandler: (cliConfig: cliFlags, client:
207233 case 'audio' :
208234 case 'ptt' :
209235 case 'video' :
210- if ( message . cloudUrl ) text = `FILE:\t${ message . cloudUrl } \n\nMESSAGE:\t${ message . text } ` ;
236+ if ( message . cloudUrl ) {
237+ text = `FILE:\t${ message . cloudUrl } \n\nMESSAGE:\t${ message . text } ` ;
238+ } else {
239+ text = message . text ;
240+ hasAttachments = true ;
241+ }
211242 break ;
212243 default :
213244 text = message . body || "__UNHANDLED__" ;
214245 break ;
215246 }
216- await sendConversationMessage ( text , message . from , message )
247+ if ( hasAttachments ) await sendAttachmentMessage ( text , message . from , message )
248+ else await sendConversationMessage ( text , message . from , message )
217249 } )
218250}
0 commit comments