Skip to content

Commit eb83d89

Browse files
committed
feat: Show webhook_url for chatwoot
1 parent b4a9941 commit eb83d89

File tree

8 files changed

+83
-26
lines changed

8 files changed

+83
-26
lines changed

src/config/env.config.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { join } from 'path';
44
import { SRC_DIR } from './path.config';
55
import { isBooleanString } from 'class-validator';
66

7-
export type HttpServer = { TYPE: 'http' | 'https'; PORT: number };
7+
export type HttpServer = { TYPE: 'http' | 'https'; PORT: number; URL: string };
88

99
export type HttpMethods = 'POST' | 'GET' | 'PUT' | 'DELETE';
1010
export type Cors = {
@@ -98,9 +98,9 @@ export type Instance = {
9898
NAME: string;
9999
WEBHOOK_URL: string;
100100
MODE: string;
101-
CHATWOOT_ACCOUNT_ID: string;
102-
CHATWOOT_TOKEN: string;
103-
CHATWOOT_URL: string;
101+
CHATWOOT_ACCOUNT_ID?: string;
102+
CHATWOOT_TOKEN?: string;
103+
CHATWOOT_URL?: string;
104104
};
105105
export type Auth = {
106106
API_KEY: ApiKey;
@@ -159,6 +159,7 @@ export class ConfigService {
159159
if (process.env?.DOCKER_ENV === 'true') {
160160
this.env.SERVER.TYPE = 'http';
161161
this.env.SERVER.PORT = 8080;
162+
this.env.SERVER.URL = `http://localhost:${this.env.SERVER.PORT}`;
162163
}
163164
}
164165

@@ -173,6 +174,7 @@ export class ConfigService {
173174
SERVER: {
174175
TYPE: process.env.SERVER_TYPE as 'http' | 'https',
175176
PORT: Number.parseInt(process.env.SERVER_PORT),
177+
URL: process.env.SERVER_URL,
176178
},
177179
CORS: {
178180
ORIGIN: process.env.CORS_ORIGIN.split(','),
@@ -278,9 +280,10 @@ export class ConfigService {
278280
NAME: process.env.AUTHENTICATION_INSTANCE_NAME,
279281
WEBHOOK_URL: process.env.AUTHENTICATION_INSTANCE_WEBHOOK_URL,
280282
MODE: process.env.AUTHENTICATION_INSTANCE_MODE,
281-
CHATWOOT_ACCOUNT_ID: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_ACCOUNT_ID,
282-
CHATWOOT_TOKEN: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_TOKEN,
283-
CHATWOOT_URL: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_URL,
283+
CHATWOOT_ACCOUNT_ID:
284+
process.env.AUTHENTICATION_INSTANCE_CHATWOOT_ACCOUNT_ID || '',
285+
CHATWOOT_TOKEN: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_TOKEN || '',
286+
CHATWOOT_URL: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_URL || '',
284287
},
285288
},
286289
};

src/dev-env.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
SERVER:
99
TYPE: http # https
1010
PORT: 8080 # 443
11+
URL: localhost
1112

1213
CORS:
1314
ORIGIN:

src/whatsapp/controllers/chatwoot.controller.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import { ChatwootDto } from '../dto/chatwoot.dto';
55
import { ChatwootService } from '../services/chatwoot.service';
66
import { Logger } from '../../config/logger.config';
77
import { waMonitor } from '../whatsapp.module';
8+
import { ConfigService, HttpServer } from '../../config/env.config';
89

910
const logger = new Logger('ChatwootController');
1011

1112
export class ChatwootController {
12-
constructor(private readonly chatwootService: ChatwootService) {}
13+
constructor(
14+
private readonly chatwootService: ChatwootService,
15+
private readonly configService: ConfigService,
16+
) {}
1317

1418
public async createChatwoot(instance: InstanceDto, data: ChatwootDto) {
1519
logger.verbose(
@@ -46,21 +50,25 @@ export class ChatwootController {
4650

4751
const result = this.chatwootService.create(instance, data);
4852

53+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
54+
4955
const response = {
5056
...result,
51-
webhook_url: `/chatwoot/webhook/${instance.instanceName}`,
57+
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
5258
};
5359

5460
return response;
5561
}
5662

5763
public async findChatwoot(instance: InstanceDto) {
5864
logger.verbose('requested findChatwoot from ' + instance.instanceName + ' instance');
59-
const result = this.chatwootService.find(instance);
65+
const result = await this.chatwootService.find(instance);
66+
67+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
6068

6169
const response = {
6270
...result,
63-
webhook_url: `/chatwoot/webhook/${instance.instanceName}`,
71+
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
6472
};
6573

6674
return response;

src/whatsapp/controllers/instance.controller.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { delay } from '@whiskeysockets/baileys';
22
import EventEmitter2 from 'eventemitter2';
3-
import { Auth, ConfigService } from '../../config/env.config';
3+
import { Auth, ConfigService, HttpServer } from '../../config/env.config';
44
import { BadRequestException, InternalServerErrorException } from '../../exceptions';
55
import { InstanceDto } from '../dto/instance.dto';
66
import { RepositoryBroker } from '../repository/repository.manager';
@@ -154,6 +154,8 @@ export class InstanceController {
154154
this.logger.log(error);
155155
}
156156

157+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
158+
157159
return {
158160
instance: {
159161
instanceName: instance.instanceName,
@@ -167,7 +169,7 @@ export class InstanceController {
167169
url: chatwoot_url,
168170
sign_msg: chatwoot_sign_msg,
169171
name_inbox: instance.instanceName,
170-
webhook_url: `/chatwoot/webhook/${instance.instanceName}`,
172+
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
171173
},
172174
};
173175
} else {
@@ -288,6 +290,8 @@ export class InstanceController {
288290
this.logger.log(error);
289291
}
290292

293+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
294+
291295
return {
292296
instance: {
293297
instanceName: instance.instanceName,
@@ -301,7 +305,7 @@ export class InstanceController {
301305
url: chatwoot_url,
302306
sign_msg: chatwoot_sign_msg,
303307
name_inbox: instance.instanceName,
304-
webhook_url: `/chatwoot/webhook/${instance.instanceName}`,
308+
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
305309
},
306310
};
307311
}

src/whatsapp/services/chatwoot.service.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,26 @@ export class ChatwootService {
4242
}
4343

4444
private async getProvider(instance: InstanceDto) {
45-
const provider = await this.waMonitor.waInstances[
46-
instance.instanceName
47-
].findChatwoot();
45+
try {
46+
const provider = await this.waMonitor.waInstances[
47+
instance.instanceName
48+
].findChatwoot();
4849

49-
if (!provider) {
50+
if (!provider) {
51+
return null;
52+
}
53+
54+
return provider;
55+
} catch (error) {
5056
return null;
5157
}
52-
53-
return provider;
5458
}
5559

5660
private async clientCw(instance: InstanceDto) {
5761
const provider = await this.getProvider(instance);
5862

5963
if (!provider) {
60-
throw new Error('provider not found');
64+
this.logger.error('provider not found');
6165
}
6266

6367
this.provider = provider;
@@ -78,7 +82,7 @@ export class ChatwootService {
7882
this.logger.verbose('create chatwoot: ' + instance.instanceName);
7983
this.waMonitor.waInstances[instance.instanceName].setChatwoot(data);
8084

81-
return { chatwoot: { ...instance, chatwoot: data } };
85+
return data;
8286
}
8387

8488
public async find(instance: InstanceDto): Promise<ChatwootDto> {
@@ -583,6 +587,21 @@ export class ChatwootService {
583587
}
584588
}
585589

590+
if (body.message_type === 'template' && body.content_type === 'input_csat') {
591+
const data: SendTextDto = {
592+
number: chatId,
593+
textMessage: {
594+
text: body.content,
595+
},
596+
options: {
597+
delay: 1200,
598+
presence: 'composing',
599+
},
600+
};
601+
602+
await waInstance?.textMessage(data);
603+
}
604+
586605
return { message: 'bot' };
587606
} catch (error) {
588607
console.log(error);

src/whatsapp/services/monitor.service.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ConfigService,
1010
Database,
1111
DelInstance,
12+
HttpServer,
1213
Redis,
1314
} from '../../config/env.config';
1415
import { RepositoryBroker } from '../repository/repository.manager';
@@ -83,6 +84,19 @@ export class WAMonitoringService {
8384
for await (const [key, value] of Object.entries(this.waInstances)) {
8485
if (value) {
8586
this.logger.verbose('get instance info: ' + key);
87+
let chatwoot: any;
88+
89+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
90+
91+
const findChatwoot = await this.waInstances[key].findChatwoot();
92+
93+
if (findChatwoot.enabled) {
94+
chatwoot = {
95+
...findChatwoot,
96+
webhook_url: `${urlServer}/chatwoot/webhook/${key}`,
97+
};
98+
}
99+
86100
if (value.connectionStatus.state === 'open') {
87101
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
88102
let apikey: string;
@@ -101,6 +115,7 @@ export class WAMonitoringService {
101115
profilePictureUrl: value.profilePictureUrl,
102116
status: (await value.getProfileStatus()) || '',
103117
apikey,
118+
chatwoot,
104119
},
105120
});
106121
} else {
@@ -114,6 +129,7 @@ export class WAMonitoringService {
114129
profileName: (await value.getProfileName()) || 'not loaded',
115130
profilePictureUrl: value.profilePictureUrl,
116131
status: (await value.getProfileStatus()) || '',
132+
chatwoot,
117133
},
118134
});
119135
}
@@ -134,6 +150,7 @@ export class WAMonitoringService {
134150
instanceName: key,
135151
status: value.connectionStatus.state,
136152
apikey,
153+
chatwoot,
137154
},
138155
});
139156
} else {
@@ -144,6 +161,7 @@ export class WAMonitoringService {
144161
instance: {
145162
instanceName: key,
146163
status: value.connectionStatus.state,
164+
chatwoot,
147165
},
148166
});
149167
}

src/whatsapp/services/whatsapp.service.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ import makeWASocket, {
3030
WAMessageUpdate,
3131
WASocket,
3232
getAggregateVotesInPollMessage,
33-
Browsers,
3433
} from '@whiskeysockets/baileys';
3534
import {
3635
Auth,
3736
CleanStoreConf,
3837
ConfigService,
3938
ConfigSessionPhone,
4039
Database,
40+
HttpServer,
4141
QrCode,
4242
Redis,
4343
Webhook,
@@ -343,6 +343,7 @@ export class WAStartupService {
343343

344344
public async sendDataWebhook<T = any>(event: Events, data: T, local = true) {
345345
const webhookGlobal = this.configService.get<Webhook>('WEBHOOK');
346+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
346347
const webhookLocal = this.localWebhook.events;
347348
const we = event.replace(/[\.-]/gm, '_').toUpperCase();
348349
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
@@ -367,6 +368,7 @@ export class WAStartupService {
367368
instance: this.instance.name,
368369
data,
369370
destination: this.localWebhook.url,
371+
urlServer,
370372
});
371373
}
372374

@@ -378,6 +380,7 @@ export class WAStartupService {
378380
instance: this.instance.name,
379381
data,
380382
destination: this.localWebhook.url,
383+
urlServer,
381384
});
382385
}
383386
} catch (error) {
@@ -425,6 +428,7 @@ export class WAStartupService {
425428
instance: this.instance.name,
426429
data,
427430
destination: localUrl,
431+
urlServer,
428432
});
429433
}
430434

@@ -436,6 +440,7 @@ export class WAStartupService {
436440
instance: this.instance.name,
437441
data,
438442
destination: localUrl,
443+
urlServer,
439444
});
440445
}
441446
} catch (error) {
@@ -719,8 +724,7 @@ export class WAStartupService {
719724
const { version } = await fetchLatestBaileysVersion();
720725
this.logger.verbose('Baileys version: ' + version);
721726
const session = this.configService.get<ConfigSessionPhone>('CONFIG_SESSION_PHONE');
722-
// const browser: WABrowserDescription = [session.CLIENT, session.NAME, release()];
723-
const browser: WABrowserDescription = Browsers.appropriate(session.CLIENT);
727+
const browser: WABrowserDescription = [session.CLIENT, session.NAME, release()];
724728
this.logger.verbose('Browser: ' + JSON.stringify(browser));
725729

726730
const socketConfig: UserFacingSocketConfig = {

src/whatsapp/whatsapp.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const webhookController = new WebhookController(webhookService);
7474

7575
const chatwootService = new ChatwootService(waMonitor);
7676

77-
export const chatwootController = new ChatwootController(chatwootService);
77+
export const chatwootController = new ChatwootController(chatwootService, configService);
7878

7979
export const instanceController = new InstanceController(
8080
waMonitor,

0 commit comments

Comments
 (0)