Skip to content

Commit fff420b

Browse files
committed
fix: command to create new instances set to /new_instance:<NAME>:<NUMBER>
1 parent 7103a95 commit fff420b

3 files changed

Lines changed: 61 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Fixed issue where it was not possible to open a conversation when sent at first by me on my cell phone in chatwoot
1717
* Now it only updates the contact name if it is the same as the phone number in chatwoot
1818
* Now accepts all chatwoot inbox templates
19+
* Command to create new instances set to /new_instance:<NAME>:<NUMBER>
1920

2021
### Integrations
2122

src/whatsapp/controllers/chatwoot.controller.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,10 @@ export class ChatwootController {
9494

9595
return chatwootService.receiveWebhook(instance, data);
9696
}
97+
98+
public async newInstance(data: any) {
99+
const chatwootService = new ChatwootService(waMonitor, this.configService);
100+
101+
return chatwootService.newInstance(data);
102+
}
97103
}

src/whatsapp/services/chatwoot.service.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { SendAudioDto } from '../dto/sendMessage.dto';
1313
import { SendMediaDto } from '../dto/sendMessage.dto';
1414
import { ROOT_DIR } from '../../config/path.config';
1515
import { ConfigService, HttpServer } from '../../config/env.config';
16-
import { delay } from '@whiskeysockets/baileys';
1716

1817
export class ChatwootService {
1918
private messageCacheFile: string;
@@ -1017,7 +1016,7 @@ export class ChatwootService {
10171016
await waInstance?.client?.ws?.close();
10181017
}
10191018

1020-
if (command.includes('#inbox_whatsapp')) {
1019+
if (command.includes('new_instance')) {
10211020
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
10221021
const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY;
10231022

@@ -1030,6 +1029,10 @@ export class ChatwootService {
10301029
chatwoot_sign_msg: this.provider.sign_msg,
10311030
};
10321031

1032+
if (command.split(':')[2]) {
1033+
data['number'] = command.split(':')[2];
1034+
}
1035+
10331036
const config = {
10341037
method: 'post',
10351038
maxBodyLength: Infinity,
@@ -1585,4 +1588,53 @@ export class ChatwootService {
15851588
this.logger.error(error);
15861589
}
15871590
}
1591+
1592+
public async newInstance(data: any) {
1593+
try {
1594+
const instanceName = data.instanceName;
1595+
const qrcode = true;
1596+
const number = data.number;
1597+
const accountId = data.accountId;
1598+
const chatwootToken = data.token;
1599+
const chatwootUrl = data.url;
1600+
const signMsg = true;
1601+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
1602+
const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY;
1603+
1604+
console.log('data: ', data);
1605+
1606+
const requestData = {
1607+
instanceName,
1608+
qrcode,
1609+
chatwoot_account_id: accountId,
1610+
chatwoot_token: chatwootToken,
1611+
chatwoot_url: chatwootUrl,
1612+
chatwoot_sign_msg: signMsg,
1613+
};
1614+
1615+
if (number) {
1616+
requestData['number'] = number;
1617+
}
1618+
1619+
console.log('requestData: ', requestData);
1620+
1621+
const config = {
1622+
method: 'post',
1623+
maxBodyLength: Infinity,
1624+
url: `${urlServer}/instance/create`,
1625+
headers: {
1626+
'Content-Type': 'application/json',
1627+
apikey: apiKey,
1628+
},
1629+
data: requestData,
1630+
};
1631+
1632+
// await axios.request(config);
1633+
1634+
return true;
1635+
} catch (error) {
1636+
this.logger.error(error);
1637+
return null;
1638+
}
1639+
}
15881640
}

0 commit comments

Comments
 (0)