@@ -100,6 +100,13 @@ export class TypebotService {
100100 const url = data . url ;
101101 const typebot = data . typebot ;
102102 const variables = data . variables ;
103+ const findTypebot = await this . find ( instance ) ;
104+ const sessions = ( findTypebot . sessions as Session [ ] ) ?? [ ] ;
105+ const expire = findTypebot . expire ;
106+ const keyword_finish = findTypebot . keyword_finish ;
107+ const delay_message = findTypebot . delay_message ;
108+ const unknown_message = findTypebot . unknown_message ;
109+ const listening_from_me = findTypebot . listening_from_me ;
103110
104111 const prefilledVariables = {
105112 remoteJid : remoteJid ,
@@ -109,42 +116,47 @@ export class TypebotService {
109116 prefilledVariables [ variable . name ] = variable . value ;
110117 } ) ;
111118
112- const id = Math . floor ( Math . random ( ) * 10000000000 ) . toString ( ) ;
113-
114- const reqData = {
115- sessionId : id ,
116- startParams : {
117- typebot : data . typebot ,
118- prefilledVariables : prefilledVariables ,
119- } ,
120- } ;
121-
122- const request = await axios . post ( data . url + '/api/v1/sendMessage' , reqData ) ;
123-
124- await this . sendWAMessage (
125- instance ,
126- remoteJid ,
127- request . data . messages ,
128- request . data . input ,
129- request . data . clientSideActions ,
130- ) ;
131-
132- this . waMonitor . waInstances [ instance . instanceName ] . sendDataWebhook ( Events . TYPEBOT_START , {
133- remoteJid : remoteJid ,
119+ const response = await this . createNewSession ( instance , {
134120 url : url ,
135121 typebot : typebot ,
136- variables : variables ,
137- sessionId : id ,
122+ remoteJid : remoteJid ,
123+ expire : expire ,
124+ keyword_finish : keyword_finish ,
125+ delay_message : delay_message ,
126+ unknown_message : unknown_message ,
127+ listening_from_me : listening_from_me ,
128+ sessions : sessions ,
129+ prefilledVariables : prefilledVariables ,
138130 } ) ;
139131
132+ if ( response . sessionId ) {
133+ await this . sendWAMessage (
134+ instance ,
135+ remoteJid ,
136+ response . messages ,
137+ response . input ,
138+ response . clientSideActions ,
139+ ) ;
140+
141+ this . waMonitor . waInstances [ instance . instanceName ] . sendDataWebhook ( Events . TYPEBOT_START , {
142+ remoteJid : remoteJid ,
143+ url : url ,
144+ typebot : typebot ,
145+ prefilledVariables : prefilledVariables ,
146+ sessionId : `${ response . sessionId } ` ,
147+ } ) ;
148+ } else {
149+ throw new Error ( "Session ID not found in response" ) ;
150+ }
151+
140152 return {
141153 typebot : {
142154 ...instance ,
143155 typebot : {
144156 url : url ,
145157 remoteJid : remoteJid ,
146158 typebot : typebot ,
147- variables : variables ,
159+ prefilledVariables : prefilledVariables ,
148160 } ,
149161 } ,
150162 } ;
@@ -194,8 +206,9 @@ export class TypebotService {
194206 typebot : data . typebot ,
195207 prefilledVariables : {
196208 remoteJid : data . remoteJid ,
197- pushName : data . pushName ,
209+ pushName : data . pushName || 'Default Name' ,
198210 instanceName : instance . instanceName ,
211+ ...data . prefilledVariables ,
199212 } ,
200213 } ,
201214 } ;
@@ -209,6 +222,12 @@ export class TypebotService {
209222 status : 'opened' ,
210223 createdAt : Date . now ( ) ,
211224 updateAt : Date . now ( ) ,
225+ prefilledVariables : {
226+ ...data . prefilledVariables ,
227+ remoteJid : data . remoteJid ,
228+ pushName : data . pushName || 'Default Name' ,
229+ instanceName : instance . instanceName ,
230+ }
212231 } ) ;
213232
214233 const typebotData = {
@@ -390,6 +409,7 @@ export class TypebotService {
390409 const listening_from_me = findTypebot . listening_from_me ;
391410
392411 const session = sessions . find ( ( session ) => session . remoteJid === remoteJid ) ;
412+ session . prefilledVariables . pushName = msg . pushName ;
393413
394414 if ( session && expire && expire > 0 ) {
395415 const now = Date . now ( ) ;
0 commit comments