Skip to content

Commit 8622e1e

Browse files
committed
feat: automation chatwoot
1 parent 3e13ae9 commit 8622e1e

File tree

5 files changed

+141
-43
lines changed

5 files changed

+141
-43
lines changed

Docker/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
SERVER_URL='<url>' # ex.: http://localhost:3333
2+
13
CORS_ORIGIN='*' # Or separate by commas - ex.: 'yourdomain1.com, yourdomain2.com'
24
CORS_METHODS='POST,GET,PUT,DELETE'
35
CORS_CREDENTIALS=true

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
"dependencies": {
4343
"@adiwajshing/keyed-db": "^0.2.4",
4444
"@ffmpeg-installer/ffmpeg": "^1.1.0",
45-
"@figuro/chatwoot-sdk": "^1.1.14",
4645
"@hapi/boom": "^10.0.1",
4746
"@whiskeysockets/baileys": "github:vphelipe/WhiskeySockets-Baileys#master",
47+
"@figuro/chatwoot-sdk": "^1.1.14",
4848
"axios": "^1.3.5",
4949
"class-validator": "^0.13.2",
5050
"compression": "^1.7.4",

src/whatsapp/controllers/instance.controller.ts

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -79,30 +79,30 @@ export class InstanceController {
7979

8080
this.logger.verbose('hash: ' + hash + ' generated');
8181

82+
let getEvents: string[];
83+
84+
if (webhook) {
85+
this.logger.verbose('creating webhook');
86+
try {
87+
this.webhookService.create(instance, {
88+
enabled: true,
89+
url: webhook,
90+
events,
91+
webhook_by_events,
92+
});
93+
94+
getEvents = (await this.webhookService.find(instance)).events;
95+
} catch (error) {
96+
this.logger.log(error);
97+
}
98+
}
99+
82100
if (
83101
!chatwoot_account_id ||
84102
!chatwoot_token ||
85103
!chatwoot_url ||
86104
!chatwoot_sign_msg
87105
) {
88-
let getEvents: string[];
89-
90-
if (webhook) {
91-
this.logger.verbose('creating webhook');
92-
try {
93-
this.webhookService.create(instance, {
94-
enabled: true,
95-
url: webhook,
96-
events,
97-
webhook_by_events,
98-
});
99-
100-
getEvents = (await this.webhookService.find(instance)).events;
101-
} catch (error) {
102-
this.logger.log(error);
103-
}
104-
}
105-
106106
this.logger.verbose('instance created');
107107
this.logger.verbose({
108108
instance: {
@@ -141,6 +141,8 @@ export class InstanceController {
141141
throw new BadRequestException('sign_msg is required');
142142
}
143143

144+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
145+
144146
try {
145147
this.chatwootService.create(instance, {
146148
enabled: true,
@@ -150,12 +152,17 @@ export class InstanceController {
150152
sign_msg: chatwoot_sign_msg,
151153
name_inbox: instance.instanceName,
152154
});
155+
156+
this.chatwootService.initInstanceChatwoot(
157+
instance,
158+
instance.instanceName,
159+
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
160+
qrcode,
161+
);
153162
} catch (error) {
154163
this.logger.log(error);
155164
}
156165

157-
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
158-
159166
return {
160167
instance: {
161168
instanceName: instance.instanceName,
@@ -202,30 +209,30 @@ export class InstanceController {
202209

203210
this.logger.verbose('hash: ' + hash + ' generated');
204211

212+
let getEvents: string[];
213+
214+
if (webhook) {
215+
this.logger.verbose('creating webhook');
216+
try {
217+
this.webhookService.create(instance, {
218+
enabled: true,
219+
url: webhook,
220+
events,
221+
webhook_by_events,
222+
});
223+
224+
getEvents = (await this.webhookService.find(instance)).events;
225+
} catch (error) {
226+
this.logger.log(error);
227+
}
228+
}
229+
205230
if (
206231
!chatwoot_account_id ||
207232
!chatwoot_token ||
208233
!chatwoot_url ||
209234
!chatwoot_sign_msg
210235
) {
211-
let getEvents: string[];
212-
213-
if (webhook) {
214-
this.logger.verbose('creating webhook');
215-
try {
216-
this.webhookService.create(instance, {
217-
enabled: true,
218-
url: webhook,
219-
events,
220-
webhook_by_events,
221-
});
222-
223-
getEvents = (await this.webhookService.find(instance)).events;
224-
} catch (error) {
225-
this.logger.log(error);
226-
}
227-
}
228-
229236
let getQrcode: wa.QrCode;
230237

231238
if (qrcode) {
@@ -277,6 +284,8 @@ export class InstanceController {
277284
throw new BadRequestException('sign_msg is required');
278285
}
279286

287+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
288+
280289
try {
281290
this.chatwootService.create(instance, {
282291
enabled: true,
@@ -286,18 +295,26 @@ export class InstanceController {
286295
sign_msg: chatwoot_sign_msg,
287296
name_inbox: instance.instanceName,
288297
});
298+
299+
this.chatwootService.initInstanceChatwoot(
300+
instance,
301+
instance.instanceName,
302+
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
303+
qrcode,
304+
);
289305
} catch (error) {
290306
this.logger.log(error);
291307
}
292308

293-
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
294-
295309
return {
296310
instance: {
297311
instanceName: instance.instanceName,
298312
status: 'created',
299313
},
300314
hash,
315+
webhook,
316+
webhook_by_events,
317+
events: getEvents,
301318
chatwoot: {
302319
enabled: true,
303320
account_id: chatwoot_account_id,

src/whatsapp/services/chatwoot.service.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,87 @@ export class ChatwootService {
117117
return contact;
118118
}
119119

120+
public async initInstanceChatwoot(
121+
instance: InstanceDto,
122+
inboxName: string,
123+
webhookUrl: string,
124+
qrcode: boolean,
125+
) {
126+
const client = await this.clientCw(instance);
127+
128+
if (!client) {
129+
throw new Error('client not found');
130+
}
131+
132+
const findInbox: any = await client.inboxes.list({
133+
accountId: this.provider.account_id,
134+
});
135+
136+
const checkDuplicate = findInbox.payload
137+
.map((inbox) => inbox.name)
138+
.includes(inboxName);
139+
140+
let inboxId: number;
141+
142+
if (!checkDuplicate) {
143+
const data = {
144+
type: 'api',
145+
webhook_url: webhookUrl,
146+
};
147+
148+
const inbox = await client.inboxes.create({
149+
accountId: this.provider.account_id,
150+
data: {
151+
name: inboxName,
152+
channel: data as any,
153+
},
154+
});
155+
156+
if (!inbox) {
157+
return null;
158+
}
159+
160+
inboxId = inbox.id;
161+
} else {
162+
const inbox = findInbox.payload.find((inbox) => inbox.name === inboxName);
163+
164+
inboxId = inbox.id;
165+
}
166+
167+
const contact =
168+
(await this.findContact(instance, '123456')) ||
169+
((await this.createContact(
170+
instance,
171+
'123456',
172+
inboxId,
173+
false,
174+
'EvolutionAPI',
175+
)) as any);
176+
177+
const contactId = contact.id || contact.payload.contact.id;
178+
179+
if (qrcode) {
180+
const conversation = await client.conversations.create({
181+
accountId: this.provider.account_id,
182+
data: {
183+
contact_id: contactId.toString(),
184+
inbox_id: inboxId.toString(),
185+
},
186+
});
187+
188+
await client.messages.create({
189+
accountId: this.provider.account_id,
190+
conversationId: conversation.id,
191+
data: {
192+
content: '/iniciar',
193+
message_type: 'outgoing',
194+
},
195+
});
196+
}
197+
198+
return true;
199+
}
200+
120201
public async createContact(
121202
instance: InstanceDto,
122203
phoneNumber: string,

src/whatsapp/services/monitor.service.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ export class WAMonitoringService {
129129
profileName: (await value.getProfileName()) || 'not loaded',
130130
profilePictureUrl: value.profilePictureUrl,
131131
status: (await value.getProfileStatus()) || '',
132-
chatwoot,
133132
},
134133
});
135134
}
@@ -161,7 +160,6 @@ export class WAMonitoringService {
161160
instance: {
162161
instanceName: key,
163162
status: value.connectionStatus.state,
164-
chatwoot,
165163
},
166164
});
167165
}

0 commit comments

Comments
 (0)