@@ -41,77 +41,136 @@ export class InstanceController {
4141 chatwoot_url,
4242 chatwoot_sign_msg,
4343 } : InstanceDto ) {
44- this . logger . verbose ( 'requested createInstance from ' + instanceName + ' instance' ) ;
44+ try {
45+ this . logger . verbose ( 'requested createInstance from ' + instanceName + ' instance' ) ;
4546
46- if ( instanceName !== instanceName . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] / g, '' ) ) {
47- throw new BadRequestException (
48- 'The instance name must be lowercase and without special characters' ,
47+ if ( instanceName !== instanceName . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] / g, '' ) ) {
48+ throw new BadRequestException (
49+ 'The instance name must be lowercase and without special characters' ,
50+ ) ;
51+ }
52+
53+ this . logger . verbose ( 'checking duplicate token' ) ;
54+ await this . authService . checkDuplicateToken ( token ) ;
55+
56+ this . logger . verbose ( 'creating instance' ) ;
57+ const instance = new WAStartupService (
58+ this . configService ,
59+ this . eventEmitter ,
60+ this . repository ,
61+ this . cache ,
4962 ) ;
50- }
63+ instance . instanceName = instanceName
64+ . toLowerCase ( )
65+ . replace ( / [ ^ a - z 0 - 9 ] / g, '' )
66+ . replace ( ' ' , '' ) ;
5167
52- this . logger . verbose ( 'checking duplicate token' ) ;
53- await this . authService . checkDuplicateToken ( token ) ;
54-
55- this . logger . verbose ( 'creating instance' ) ;
56- const instance = new WAStartupService (
57- this . configService ,
58- this . eventEmitter ,
59- this . repository ,
60- this . cache ,
61- ) ;
62- instance . instanceName = instanceName
63- . toLowerCase ( )
64- . replace ( / [ ^ a - z 0 - 9 ] / g, '' )
65- . replace ( ' ' , '' ) ;
66-
67- this . logger . verbose ( 'instance: ' + instance . instanceName + ' created' ) ;
68-
69- this . waMonitor . waInstances [ instance . instanceName ] = instance ;
70- this . waMonitor . delInstanceTime ( instance . instanceName ) ;
71-
72- this . logger . verbose ( 'generating hash' ) ;
73- const hash = await this . authService . generateHash (
74- {
75- instanceName : instance . instanceName ,
76- } ,
77- token ,
78- ) ;
68+ this . logger . verbose ( 'instance: ' + instance . instanceName + ' created' ) ;
69+
70+ this . waMonitor . waInstances [ instance . instanceName ] = instance ;
71+ this . waMonitor . delInstanceTime ( instance . instanceName ) ;
72+
73+ this . logger . verbose ( 'generating hash' ) ;
74+ const hash = await this . authService . generateHash (
75+ {
76+ instanceName : instance . instanceName ,
77+ } ,
78+ token ,
79+ ) ;
80+
81+ this . logger . verbose ( 'hash: ' + hash + ' generated' ) ;
82+
83+ let getEvents : string [ ] ;
84+
85+ if ( webhook ) {
86+ if ( ! isURL ( webhook , { require_tld : false } ) ) {
87+ throw new BadRequestException ( 'Invalid "url" property in webhook' ) ;
88+ }
7989
80- this . logger . verbose ( 'hash: ' + hash + ' generated' ) ;
90+ this . logger . verbose ( 'creating webhook' ) ;
91+ try {
92+ this . webhookService . create ( instance , {
93+ enabled : true ,
94+ url : webhook ,
95+ events,
96+ webhook_by_events,
97+ } ) ;
8198
82- let getEvents : string [ ] ;
99+ getEvents = ( await this . webhookService . find ( instance ) ) . events ;
100+ } catch ( error ) {
101+ this . logger . log ( error ) ;
102+ }
103+ }
104+
105+ if ( ! chatwoot_account_id || ! chatwoot_token || ! chatwoot_url ) {
106+ let getQrcode : wa . QrCode ;
107+
108+ if ( qrcode ) {
109+ this . logger . verbose ( 'creating qrcode' ) ;
110+ await instance . connectToWhatsapp ( number ) ;
111+ await delay ( 5000 ) ;
112+ getQrcode = instance . qrCode ;
113+ }
114+
115+ const result = {
116+ instance : {
117+ instanceName : instance . instanceName ,
118+ status : 'created' ,
119+ } ,
120+ hash,
121+ webhook,
122+ webhook_by_events,
123+ events : getEvents ,
124+ qrcode : getQrcode ,
125+ } ;
126+
127+ this . logger . verbose ( 'instance created' ) ;
128+ this . logger . verbose ( result ) ;
129+
130+ return result ;
131+ }
132+
133+ if ( ! chatwoot_account_id ) {
134+ throw new BadRequestException ( 'account_id is required' ) ;
135+ }
83136
84- if ( webhook ) {
85- if ( ! isURL ( webhook , { require_tld : false } ) ) {
86- throw new BadRequestException ( 'Invalid "url" property in webhook' ) ;
137+ if ( ! chatwoot_token ) {
138+ throw new BadRequestException ( 'token is required' ) ;
87139 }
88140
89- this . logger . verbose ( 'creating webhook' ) ;
141+ if ( ! chatwoot_url ) {
142+ throw new BadRequestException ( 'url is required' ) ;
143+ }
144+
145+ if ( ! isURL ( chatwoot_url , { require_tld : false } ) ) {
146+ throw new BadRequestException ( 'Invalid "url" property in chatwoot' ) ;
147+ }
148+
149+ const urlServer = this . configService . get < HttpServer > ( 'SERVER' ) . URL ;
150+
90151 try {
91- this . webhookService . create ( instance , {
152+ this . chatwootService . create ( instance , {
92153 enabled : true ,
93- url : webhook ,
94- events,
95- webhook_by_events,
154+ account_id : chatwoot_account_id ,
155+ token : chatwoot_token ,
156+ url : chatwoot_url ,
157+ sign_msg : chatwoot_sign_msg || false ,
158+ name_inbox : instance . instanceName ,
159+ number,
96160 } ) ;
97161
98- getEvents = ( await this . webhookService . find ( instance ) ) . events ;
162+ this . chatwootService . initInstanceChatwoot (
163+ instance ,
164+ instance . instanceName ,
165+ `${ urlServer } /chatwoot/webhook/${ instance . instanceName } ` ,
166+ qrcode ,
167+ number ,
168+ ) ;
99169 } catch ( error ) {
100170 this . logger . log ( error ) ;
101171 }
102- }
103172
104- if ( ! chatwoot_account_id || ! chatwoot_token || ! chatwoot_url ) {
105- let getQrcode : wa . QrCode ;
106-
107- if ( qrcode ) {
108- this . logger . verbose ( 'creating qrcode' ) ;
109- await instance . connectToWhatsapp ( number ) ;
110- await delay ( 5000 ) ;
111- getQrcode = instance . qrCode ;
112- }
113-
114- const result = {
173+ return {
115174 instance : {
116175 instanceName : instance . instanceName ,
117176 status : 'created' ,
@@ -120,75 +179,21 @@ export class InstanceController {
120179 webhook,
121180 webhook_by_events,
122181 events : getEvents ,
123- qrcode : getQrcode ,
182+ chatwoot : {
183+ enabled : true ,
184+ account_id : chatwoot_account_id ,
185+ token : chatwoot_token ,
186+ url : chatwoot_url ,
187+ sign_msg : chatwoot_sign_msg || false ,
188+ number,
189+ name_inbox : instance . instanceName ,
190+ webhook_url : `${ urlServer } /chatwoot/webhook/${ instance . instanceName } ` ,
191+ } ,
124192 } ;
125-
126- this . logger . verbose ( 'instance created' ) ;
127- this . logger . verbose ( result ) ;
128-
129- return result ;
130- }
131-
132- if ( ! chatwoot_account_id ) {
133- throw new BadRequestException ( 'account_id is required' ) ;
134- }
135-
136- if ( ! chatwoot_token ) {
137- throw new BadRequestException ( 'token is required' ) ;
138- }
139-
140- if ( ! chatwoot_url ) {
141- throw new BadRequestException ( 'url is required' ) ;
142- }
143-
144- if ( ! isURL ( chatwoot_url , { require_tld : false } ) ) {
145- throw new BadRequestException ( 'Invalid "url" property in chatwoot' ) ;
146- }
147-
148- const urlServer = this . configService . get < HttpServer > ( 'SERVER' ) . URL ;
149-
150- try {
151- this . chatwootService . create ( instance , {
152- enabled : true ,
153- account_id : chatwoot_account_id ,
154- token : chatwoot_token ,
155- url : chatwoot_url ,
156- sign_msg : chatwoot_sign_msg || false ,
157- name_inbox : instance . instanceName ,
158- number,
159- } ) ;
160-
161- this . chatwootService . initInstanceChatwoot (
162- instance ,
163- instance . instanceName ,
164- `${ urlServer } /chatwoot/webhook/${ instance . instanceName } ` ,
165- qrcode ,
166- number ,
167- ) ;
168193 } catch ( error ) {
169- this . logger . log ( error ) ;
194+ console . log ( error ) ;
195+ return { error : true , message : error . toString ( ) } ;
170196 }
171-
172- return {
173- instance : {
174- instanceName : instance . instanceName ,
175- status : 'created' ,
176- } ,
177- hash,
178- webhook,
179- webhook_by_events,
180- events : getEvents ,
181- chatwoot : {
182- enabled : true ,
183- account_id : chatwoot_account_id ,
184- token : chatwoot_token ,
185- url : chatwoot_url ,
186- sign_msg : chatwoot_sign_msg || false ,
187- number,
188- name_inbox : instance . instanceName ,
189- webhook_url : `${ urlServer } /chatwoot/webhook/${ instance . instanceName } ` ,
190- } ,
191- } ;
192197 }
193198
194199 public async connectToWhatsapp ( { instanceName, number = null } : InstanceDto ) {
0 commit comments