Skip to content

Commit 0cb87e6

Browse files
committed
feat: Created automation for creating instances in the chatwoot bot with the command #inbox_whatsapp:<INSTANCE_NAME>
1 parent 71a3e75 commit 0cb87e6

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Added messages.delete event
66
* Added restart instance endpoint
7+
* Created automation for creating instances in the chatwoot bot with the command #inbox_whatsapp:<INSTANCE_NAME>
78

89
### Fixed
910

src/whatsapp/controllers/chatwoot.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class ChatwootController {
9090
logger.verbose(
9191
'requested receiveWebhook from ' + instance.instanceName + ' instance',
9292
);
93-
const chatwootService = new ChatwootService(waMonitor);
93+
const chatwootService = new ChatwootService(waMonitor, this.configService);
9494

9595
return chatwootService.receiveWebhook(instance, data);
9696
}

src/whatsapp/services/chatwoot.service.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import mimeTypes from 'mime-types';
1212
import { SendAudioDto } from '../dto/sendMessage.dto';
1313
import { SendMediaDto } from '../dto/sendMessage.dto';
1414
import { ROOT_DIR } from '../../config/path.config';
15+
import { ConfigService, HttpServer } from '../../config/env.config';
1516

1617
export class ChatwootService {
1718
private messageCacheFile: string;
@@ -21,7 +22,10 @@ export class ChatwootService {
2122

2223
private provider: any;
2324

24-
constructor(private readonly waMonitor: WAMonitoringService) {
25+
constructor(
26+
private readonly waMonitor: WAMonitoringService,
27+
private readonly configService: ConfigService,
28+
) {
2529
this.messageCache = new Set();
2630
}
2731

@@ -981,6 +985,39 @@ export class ChatwootService {
981985
await waInstance?.client?.logout('Log out instance: ' + instance.instanceName);
982986
await waInstance?.client?.ws?.close();
983987
}
988+
989+
if (command.includes('#inbox_whatsapp')) {
990+
console.log('command include #inbox_whatsapp');
991+
992+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
993+
const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY;
994+
995+
console.log('url server: ' + urlServer);
996+
console.log('api key: ' + apiKey);
997+
const data = {
998+
instanceName: command.split(':')[1],
999+
qrcode: true,
1000+
chatwoot_account_id: this.provider.account_id,
1001+
chatwoot_token: this.provider.token,
1002+
chatwoot_url: this.provider.url,
1003+
chatwoot_sign_msg: this.provider.sign_msg,
1004+
};
1005+
1006+
const config = {
1007+
method: 'post',
1008+
maxBodyLength: Infinity,
1009+
url: `${urlServer}/instance/create`,
1010+
headers: {
1011+
'Content-Type': 'application/json',
1012+
apikey: apiKey,
1013+
},
1014+
data: data,
1015+
};
1016+
1017+
const { data: response } = await axios.request(config);
1018+
1019+
console.log(response);
1020+
}
9841021
}
9851022

9861023
if (

src/whatsapp/services/whatsapp.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class WAStartupService {
149149
private endSession = false;
150150
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
151151

152-
private chatwootService = new ChatwootService(waMonitor);
152+
private chatwootService = new ChatwootService(waMonitor, this.configService);
153153

154154
public set instanceName(name: string) {
155155
this.logger.verbose(`Initializing instance '${name}'`);

src/whatsapp/whatsapp.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const webhookService = new WebhookService(waMonitor);
7272

7373
export const webhookController = new WebhookController(webhookService);
7474

75-
const chatwootService = new ChatwootService(waMonitor);
75+
const chatwootService = new ChatwootService(waMonitor, configService);
7676

7777
export const chatwootController = new ChatwootController(chatwootService, configService);
7878

0 commit comments

Comments
 (0)