@@ -13,6 +13,7 @@ import { SendAudioDto } from '../dto/sendMessage.dto';
1313import { SendMediaDto } from '../dto/sendMessage.dto' ;
1414import { ROOT_DIR } from '../../config/path.config' ;
1515import { ConfigService , HttpServer } from '../../config/env.config' ;
16+ import { type } from 'os' ;
1617
1718export class ChatwootService {
1819 private messageCacheFile : string ;
@@ -230,12 +231,19 @@ export class ChatwootService {
230231
231232 if ( qrcode ) {
232233 this . logger . verbose ( 'create conversation in chatwoot' ) ;
234+ const data = {
235+ contact_id : contactId . toString ( ) ,
236+ inbox_id : inboxId . toString ( ) ,
237+ } ;
238+
239+ if ( this . provider . conversation_pending ) {
240+ data [ 'status' ] = 'pending' ;
241+ }
242+
243+ console . log ( 'this.provider' , this . provider ) ;
233244 const conversation = await client . conversations . create ( {
234245 accountId : this . provider . account_id ,
235- data : {
236- contact_id : contactId . toString ( ) ,
237- inbox_id : inboxId . toString ( ) ,
238- } ,
246+ data,
239247 } ) ;
240248
241249 if ( ! conversation ) {
@@ -520,24 +528,39 @@ export class ChatwootService {
520528 } ) ) as any ;
521529
522530 if ( contactConversations ) {
531+ let conversation : any ;
532+ if ( this . provider . reopen_conversation ) {
533+ conversation = contactConversations . payload . find (
534+ ( conversation ) => conversation . inbox_id == filterInbox . id ,
535+ ) ;
536+ } else {
537+ conversation = contactConversations . payload . find (
538+ ( conversation ) =>
539+ conversation . status !== 'resolved' &&
540+ conversation . inbox_id == filterInbox . id ,
541+ ) ;
542+ }
523543 this . logger . verbose ( 'return conversation if exists' ) ;
524- const conversation = contactConversations . payload . find (
525- ( conversation ) =>
526- conversation . status !== 'resolved' && conversation . inbox_id == filterInbox . id ,
527- ) ;
544+
528545 if ( conversation ) {
529546 this . logger . verbose ( 'conversation found' ) ;
530547 return conversation . id ;
531548 }
532549 }
533550
534551 this . logger . verbose ( 'create conversation in chatwoot' ) ;
552+ const data = {
553+ contact_id : contactId . toString ( ) ,
554+ inbox_id : filterInbox . id . toString ( ) ,
555+ } ;
556+
557+ if ( this . provider . conversation_pending ) {
558+ data [ 'status' ] = 'pending' ;
559+ }
560+
535561 const conversation = await client . conversations . create ( {
536562 accountId : this . provider . account_id ,
537- data : {
538- contact_id : `${ contactId } ` ,
539- inbox_id : `${ filterInbox . id } ` ,
540- } ,
563+ data,
541564 } ) ;
542565
543566 if ( ! conversation ) {
@@ -1125,12 +1148,12 @@ export class ChatwootService {
11251148 }
11261149
11271150 if ( body . message_type === 'template' && body . event === 'message_created' ) {
1128- this . logger . verbose ( 'check if is csat ' ) ;
1151+ this . logger . verbose ( 'check if is template ' ) ;
11291152
11301153 const data : SendTextDto = {
11311154 number : chatId ,
11321155 textMessage : {
1133- text : body . content ,
1156+ text : body . content . replace ( / \\ \r \n | \\ \n | \n / g , '\n' ) ,
11341157 } ,
11351158 options : {
11361159 delay : 1200 ,
@@ -1186,6 +1209,11 @@ export class ChatwootService {
11861209 audioMessage : msg . audioMessage ?. caption ,
11871210 contactMessage : msg . contactMessage ?. vcard ,
11881211 contactsArrayMessage : msg . contactsArrayMessage ,
1212+ locationMessage : ! msg . protocolMessage
1213+ ? msg . locationMessage ?. degreesLatitude +
1214+ ',' +
1215+ msg . locationMessage ?. degreesLongitude
1216+ : undefined ,
11891217 } ;
11901218
11911219 this . logger . verbose ( 'type message: ' + types ) ;
@@ -1199,6 +1227,20 @@ export class ChatwootService {
11991227
12001228 const result = typeKey ? types [ typeKey ] : undefined ;
12011229
1230+ if ( typeKey === 'locationMessage' ) {
1231+ const [ latitude , longitude ] = result . split ( ',' ) ;
1232+
1233+ const formattedLocation = `**Location:**
1234+ **latitude:** ${ latitude }
1235+ **longitude:** ${ longitude }
1236+ https://www.google.com/maps/search/?api=1&query=${ latitude } ,${ longitude }
1237+ ` ;
1238+
1239+ this . logger . verbose ( 'message content: ' + formattedLocation ) ;
1240+
1241+ return formattedLocation ;
1242+ }
1243+
12021244 if ( typeKey === 'contactMessage' ) {
12031245 const vCardData = result . split ( '\n' ) ;
12041246 const contactInfo = { } ;
0 commit comments