mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-25 14:47:45 -06:00
feat: Show webhook_url for chatwoot
This commit is contained in:
@@ -5,11 +5,15 @@ import { ChatwootDto } from '../dto/chatwoot.dto';
|
||||
import { ChatwootService } from '../services/chatwoot.service';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { waMonitor } from '../whatsapp.module';
|
||||
import { ConfigService, HttpServer } from '../../config/env.config';
|
||||
|
||||
const logger = new Logger('ChatwootController');
|
||||
|
||||
export class ChatwootController {
|
||||
constructor(private readonly chatwootService: ChatwootService) {}
|
||||
constructor(
|
||||
private readonly chatwootService: ChatwootService,
|
||||
private readonly configService: ConfigService,
|
||||
) {}
|
||||
|
||||
public async createChatwoot(instance: InstanceDto, data: ChatwootDto) {
|
||||
logger.verbose(
|
||||
@@ -46,9 +50,11 @@ export class ChatwootController {
|
||||
|
||||
const result = this.chatwootService.create(instance, data);
|
||||
|
||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||
|
||||
const response = {
|
||||
...result,
|
||||
webhook_url: `/chatwoot/webhook/${instance.instanceName}`,
|
||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||
};
|
||||
|
||||
return response;
|
||||
@@ -56,11 +62,13 @@ export class ChatwootController {
|
||||
|
||||
public async findChatwoot(instance: InstanceDto) {
|
||||
logger.verbose('requested findChatwoot from ' + instance.instanceName + ' instance');
|
||||
const result = this.chatwootService.find(instance);
|
||||
const result = await this.chatwootService.find(instance);
|
||||
|
||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||
|
||||
const response = {
|
||||
...result,
|
||||
webhook_url: `/chatwoot/webhook/${instance.instanceName}`,
|
||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||
};
|
||||
|
||||
return response;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { delay } from '@whiskeysockets/baileys';
|
||||
import EventEmitter2 from 'eventemitter2';
|
||||
import { Auth, ConfigService } from '../../config/env.config';
|
||||
import { Auth, ConfigService, HttpServer } from '../../config/env.config';
|
||||
import { BadRequestException, InternalServerErrorException } from '../../exceptions';
|
||||
import { InstanceDto } from '../dto/instance.dto';
|
||||
import { RepositoryBroker } from '../repository/repository.manager';
|
||||
@@ -154,6 +154,8 @@ export class InstanceController {
|
||||
this.logger.log(error);
|
||||
}
|
||||
|
||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||
|
||||
return {
|
||||
instance: {
|
||||
instanceName: instance.instanceName,
|
||||
@@ -167,7 +169,7 @@ export class InstanceController {
|
||||
url: chatwoot_url,
|
||||
sign_msg: chatwoot_sign_msg,
|
||||
name_inbox: instance.instanceName,
|
||||
webhook_url: `/chatwoot/webhook/${instance.instanceName}`,
|
||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
@@ -288,6 +290,8 @@ export class InstanceController {
|
||||
this.logger.log(error);
|
||||
}
|
||||
|
||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||
|
||||
return {
|
||||
instance: {
|
||||
instanceName: instance.instanceName,
|
||||
@@ -301,7 +305,7 @@ export class InstanceController {
|
||||
url: chatwoot_url,
|
||||
sign_msg: chatwoot_sign_msg,
|
||||
name_inbox: instance.instanceName,
|
||||
webhook_url: `/chatwoot/webhook/${instance.instanceName}`,
|
||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -42,22 +42,26 @@ export class ChatwootService {
|
||||
}
|
||||
|
||||
private async getProvider(instance: InstanceDto) {
|
||||
const provider = await this.waMonitor.waInstances[
|
||||
instance.instanceName
|
||||
].findChatwoot();
|
||||
try {
|
||||
const provider = await this.waMonitor.waInstances[
|
||||
instance.instanceName
|
||||
].findChatwoot();
|
||||
|
||||
if (!provider) {
|
||||
if (!provider) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return provider;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return provider;
|
||||
}
|
||||
|
||||
private async clientCw(instance: InstanceDto) {
|
||||
const provider = await this.getProvider(instance);
|
||||
|
||||
if (!provider) {
|
||||
throw new Error('provider not found');
|
||||
this.logger.error('provider not found');
|
||||
}
|
||||
|
||||
this.provider = provider;
|
||||
@@ -78,7 +82,7 @@ export class ChatwootService {
|
||||
this.logger.verbose('create chatwoot: ' + instance.instanceName);
|
||||
this.waMonitor.waInstances[instance.instanceName].setChatwoot(data);
|
||||
|
||||
return { chatwoot: { ...instance, chatwoot: data } };
|
||||
return data;
|
||||
}
|
||||
|
||||
public async find(instance: InstanceDto): Promise<ChatwootDto> {
|
||||
@@ -583,6 +587,21 @@ export class ChatwootService {
|
||||
}
|
||||
}
|
||||
|
||||
if (body.message_type === 'template' && body.content_type === 'input_csat') {
|
||||
const data: SendTextDto = {
|
||||
number: chatId,
|
||||
textMessage: {
|
||||
text: body.content,
|
||||
},
|
||||
options: {
|
||||
delay: 1200,
|
||||
presence: 'composing',
|
||||
},
|
||||
};
|
||||
|
||||
await waInstance?.textMessage(data);
|
||||
}
|
||||
|
||||
return { message: 'bot' };
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
ConfigService,
|
||||
Database,
|
||||
DelInstance,
|
||||
HttpServer,
|
||||
Redis,
|
||||
} from '../../config/env.config';
|
||||
import { RepositoryBroker } from '../repository/repository.manager';
|
||||
@@ -83,6 +84,19 @@ export class WAMonitoringService {
|
||||
for await (const [key, value] of Object.entries(this.waInstances)) {
|
||||
if (value) {
|
||||
this.logger.verbose('get instance info: ' + key);
|
||||
let chatwoot: any;
|
||||
|
||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||
|
||||
const findChatwoot = await this.waInstances[key].findChatwoot();
|
||||
|
||||
if (findChatwoot.enabled) {
|
||||
chatwoot = {
|
||||
...findChatwoot,
|
||||
webhook_url: `${urlServer}/chatwoot/webhook/${key}`,
|
||||
};
|
||||
}
|
||||
|
||||
if (value.connectionStatus.state === 'open') {
|
||||
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
|
||||
let apikey: string;
|
||||
@@ -101,6 +115,7 @@ export class WAMonitoringService {
|
||||
profilePictureUrl: value.profilePictureUrl,
|
||||
status: (await value.getProfileStatus()) || '',
|
||||
apikey,
|
||||
chatwoot,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
@@ -114,6 +129,7 @@ export class WAMonitoringService {
|
||||
profileName: (await value.getProfileName()) || 'not loaded',
|
||||
profilePictureUrl: value.profilePictureUrl,
|
||||
status: (await value.getProfileStatus()) || '',
|
||||
chatwoot,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -134,6 +150,7 @@ export class WAMonitoringService {
|
||||
instanceName: key,
|
||||
status: value.connectionStatus.state,
|
||||
apikey,
|
||||
chatwoot,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
@@ -144,6 +161,7 @@ export class WAMonitoringService {
|
||||
instance: {
|
||||
instanceName: key,
|
||||
status: value.connectionStatus.state,
|
||||
chatwoot,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@ import makeWASocket, {
|
||||
WAMessageUpdate,
|
||||
WASocket,
|
||||
getAggregateVotesInPollMessage,
|
||||
Browsers,
|
||||
} from '@whiskeysockets/baileys';
|
||||
import {
|
||||
Auth,
|
||||
@@ -38,6 +37,7 @@ import {
|
||||
ConfigService,
|
||||
ConfigSessionPhone,
|
||||
Database,
|
||||
HttpServer,
|
||||
QrCode,
|
||||
Redis,
|
||||
Webhook,
|
||||
@@ -343,6 +343,7 @@ export class WAStartupService {
|
||||
|
||||
public async sendDataWebhook<T = any>(event: Events, data: T, local = true) {
|
||||
const webhookGlobal = this.configService.get<Webhook>('WEBHOOK');
|
||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||
const webhookLocal = this.localWebhook.events;
|
||||
const we = event.replace(/[\.-]/gm, '_').toUpperCase();
|
||||
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
|
||||
@@ -367,6 +368,7 @@ export class WAStartupService {
|
||||
instance: this.instance.name,
|
||||
data,
|
||||
destination: this.localWebhook.url,
|
||||
urlServer,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -378,6 +380,7 @@ export class WAStartupService {
|
||||
instance: this.instance.name,
|
||||
data,
|
||||
destination: this.localWebhook.url,
|
||||
urlServer,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -425,6 +428,7 @@ export class WAStartupService {
|
||||
instance: this.instance.name,
|
||||
data,
|
||||
destination: localUrl,
|
||||
urlServer,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -436,6 +440,7 @@ export class WAStartupService {
|
||||
instance: this.instance.name,
|
||||
data,
|
||||
destination: localUrl,
|
||||
urlServer,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -719,8 +724,7 @@ export class WAStartupService {
|
||||
const { version } = await fetchLatestBaileysVersion();
|
||||
this.logger.verbose('Baileys version: ' + version);
|
||||
const session = this.configService.get<ConfigSessionPhone>('CONFIG_SESSION_PHONE');
|
||||
// const browser: WABrowserDescription = [session.CLIENT, session.NAME, release()];
|
||||
const browser: WABrowserDescription = Browsers.appropriate(session.CLIENT);
|
||||
const browser: WABrowserDescription = [session.CLIENT, session.NAME, release()];
|
||||
this.logger.verbose('Browser: ' + JSON.stringify(browser));
|
||||
|
||||
const socketConfig: UserFacingSocketConfig = {
|
||||
|
||||
@@ -74,7 +74,7 @@ export const webhookController = new WebhookController(webhookService);
|
||||
|
||||
const chatwootService = new ChatwootService(waMonitor);
|
||||
|
||||
export const chatwootController = new ChatwootController(chatwootService);
|
||||
export const chatwootController = new ChatwootController(chatwootService, configService);
|
||||
|
||||
export const instanceController = new InstanceController(
|
||||
waMonitor,
|
||||
|
||||
Reference in New Issue
Block a user