mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 09:51:24 -06:00
Added webhook to send errors
This commit is contained in:
parent
c03919be2d
commit
9b72b3e332
@ -6,6 +6,7 @@
|
|||||||
* Added variables options in Start Typebot
|
* Added variables options in Start Typebot
|
||||||
* Added webhooks for typebot events
|
* Added webhooks for typebot events
|
||||||
* Added ChamaAI integration
|
* Added ChamaAI integration
|
||||||
|
* Added webhook to send errors
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
@ -86,6 +86,8 @@ WEBHOOK_EVENTS_TYPEBOT_START=false
|
|||||||
WEBHOOK_EVENTS_TYPEBOT_CHANGE_STATUS=false
|
WEBHOOK_EVENTS_TYPEBOT_CHANGE_STATUS=false
|
||||||
# This event is used with Chama AI
|
# This event is used with Chama AI
|
||||||
WEBHOOK_EVENTS_CHAMA_AI_ACTION=false
|
WEBHOOK_EVENTS_CHAMA_AI_ACTION=false
|
||||||
|
# This event is used to send errors
|
||||||
|
WEBHOOK_EVENTS_ERRORS=false
|
||||||
|
|
||||||
# Name that will be displayed on smartphone connection
|
# Name that will be displayed on smartphone connection
|
||||||
CONFIG_SESSION_PHONE_CLIENT=EvolutionAPI
|
CONFIG_SESSION_PHONE_CLIENT=EvolutionAPI
|
||||||
|
@ -89,6 +89,8 @@ ENV WEBHOOK_EVENTS_TYPEBOT_CHANGE_STATUS=false
|
|||||||
|
|
||||||
ENV WEBHOOK_EVENTS_CHAMA_AI_ACTION=false
|
ENV WEBHOOK_EVENTS_CHAMA_AI_ACTION=false
|
||||||
|
|
||||||
|
ENV WEBHOOK_EVENTS_ERRORS=false
|
||||||
|
|
||||||
ENV CONFIG_SESSION_PHONE_CLIENT=EvolutionAPI
|
ENV CONFIG_SESSION_PHONE_CLIENT=EvolutionAPI
|
||||||
ENV CONFIG_SESSION_PHONE_NAME=chrome
|
ENV CONFIG_SESSION_PHONE_NAME=chrome
|
||||||
|
|
||||||
|
@ -95,6 +95,7 @@ export type EventsWebhook = {
|
|||||||
TYPEBOT_START: boolean;
|
TYPEBOT_START: boolean;
|
||||||
TYPEBOT_CHANGE_STATUS: boolean;
|
TYPEBOT_CHANGE_STATUS: boolean;
|
||||||
CHAMA_AI_ACTION: boolean;
|
CHAMA_AI_ACTION: boolean;
|
||||||
|
ERRORS: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ApiKey = { KEY: string };
|
export type ApiKey = { KEY: string };
|
||||||
@ -270,6 +271,7 @@ export class ConfigService {
|
|||||||
TYPEBOT_START: process.env?.WEBHOOK_EVENTS_TYPEBOT_START === 'true',
|
TYPEBOT_START: process.env?.WEBHOOK_EVENTS_TYPEBOT_START === 'true',
|
||||||
TYPEBOT_CHANGE_STATUS: process.env?.WEBHOOK_EVENTS_TYPEBOT_CHANGE_STATUS === 'true',
|
TYPEBOT_CHANGE_STATUS: process.env?.WEBHOOK_EVENTS_TYPEBOT_CHANGE_STATUS === 'true',
|
||||||
CHAMA_AI_ACTION: process.env?.WEBHOOK_EVENTS_CHAMA_AI_ACTION === 'true',
|
CHAMA_AI_ACTION: process.env?.WEBHOOK_EVENTS_CHAMA_AI_ACTION === 'true',
|
||||||
|
ERRORS: process.env?.WEBHOOK_EVENTS_ERRORS === 'true',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
CONFIG_SESSION_PHONE: {
|
CONFIG_SESSION_PHONE: {
|
||||||
|
@ -125,6 +125,8 @@ WEBHOOK:
|
|||||||
TYPEBOT_CHANGE_STATUS: false
|
TYPEBOT_CHANGE_STATUS: false
|
||||||
# This event is used with Chama AI
|
# This event is used with Chama AI
|
||||||
CHAMA_AI_ACTION: false
|
CHAMA_AI_ACTION: false
|
||||||
|
# This event is used to send errors to the webhook
|
||||||
|
ERRORS: false
|
||||||
|
|
||||||
CONFIG_SESSION_PHONE:
|
CONFIG_SESSION_PHONE:
|
||||||
# Name that will be displayed on smartphone connection
|
# Name that will be displayed on smartphone connection
|
||||||
|
31
src/main.ts
31
src/main.ts
@ -1,11 +1,12 @@
|
|||||||
import 'express-async-errors';
|
import 'express-async-errors';
|
||||||
|
|
||||||
|
import axios from 'axios';
|
||||||
import compression from 'compression';
|
import compression from 'compression';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import express, { json, NextFunction, Request, Response, urlencoded } from 'express';
|
import express, { json, NextFunction, Request, Response, urlencoded } from 'express';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
|
|
||||||
import { configService, Cors, HttpServer, Rabbitmq } from './config/env.config';
|
import { configService, Cors, HttpServer, Rabbitmq, Webhook } from './config/env.config';
|
||||||
import { onUnexpectedError } from './config/error.config';
|
import { onUnexpectedError } from './config/error.config';
|
||||||
import { Logger } from './config/logger.config';
|
import { Logger } from './config/logger.config';
|
||||||
import { ROOT_DIR } from './config/path.config';
|
import { ROOT_DIR } from './config/path.config';
|
||||||
@ -54,6 +55,34 @@ function bootstrap() {
|
|||||||
app.use(
|
app.use(
|
||||||
(err: Error, req: Request, res: Response, next: NextFunction) => {
|
(err: Error, req: Request, res: Response, next: NextFunction) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
const webhook = configService.get<Webhook>('WEBHOOK');
|
||||||
|
|
||||||
|
if (webhook.GLOBAL.ENABLED && webhook.EVENTS.ERRORS) {
|
||||||
|
const tzoffset = new Date().getTimezoneOffset() * 60000; //offset in milliseconds
|
||||||
|
const localISOTime = new Date(Date.now() - tzoffset).toISOString();
|
||||||
|
const now = localISOTime;
|
||||||
|
|
||||||
|
const errorData = {
|
||||||
|
event: 'error',
|
||||||
|
data: {
|
||||||
|
error: err['error'] || 'Internal Server Error',
|
||||||
|
message: err['message'] || 'Internal Server Error',
|
||||||
|
status: err['status'] || 500,
|
||||||
|
response: {
|
||||||
|
message: err['message'] || 'Internal Server Error',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
date_time: now,
|
||||||
|
};
|
||||||
|
|
||||||
|
logger.error(errorData);
|
||||||
|
|
||||||
|
const baseURL = configService.get<Webhook>('WEBHOOK').GLOBAL.URL;
|
||||||
|
const httpService = axios.create({ baseURL });
|
||||||
|
|
||||||
|
httpService.post('', errorData);
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(err['status'] || 500).json({
|
return res.status(err['status'] || 500).json({
|
||||||
status: err['status'] || 500,
|
status: err['status'] || 500,
|
||||||
error: err['error'] || 'Internal Server Error',
|
error: err['error'] || 'Internal Server Error',
|
||||||
|
@ -4,31 +4,40 @@ import { join } from 'path';
|
|||||||
|
|
||||||
import { configService, Database, Redis } from '../../config/env.config';
|
import { configService, Database, Redis } from '../../config/env.config';
|
||||||
import { INSTANCE_DIR } from '../../config/path.config';
|
import { INSTANCE_DIR } from '../../config/path.config';
|
||||||
import { BadRequestException, ForbiddenException, NotFoundException } from '../../exceptions';
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
ForbiddenException,
|
||||||
|
InternalServerErrorException,
|
||||||
|
NotFoundException,
|
||||||
|
} from '../../exceptions';
|
||||||
import { dbserver } from '../../libs/db.connect';
|
import { dbserver } from '../../libs/db.connect';
|
||||||
import { InstanceDto } from '../dto/instance.dto';
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
import { cache, waMonitor } from '../whatsapp.module';
|
import { cache, waMonitor } from '../whatsapp.module';
|
||||||
|
|
||||||
async function getInstance(instanceName: string) {
|
async function getInstance(instanceName: string) {
|
||||||
const db = configService.get<Database>('DATABASE');
|
try {
|
||||||
const redisConf = configService.get<Redis>('REDIS');
|
const db = configService.get<Database>('DATABASE');
|
||||||
|
const redisConf = configService.get<Redis>('REDIS');
|
||||||
|
|
||||||
const exists = !!waMonitor.waInstances[instanceName];
|
const exists = !!waMonitor.waInstances[instanceName];
|
||||||
|
|
||||||
if (redisConf.ENABLED) {
|
if (redisConf.ENABLED) {
|
||||||
const keyExists = await cache.keyExists();
|
const keyExists = await cache.keyExists();
|
||||||
return exists || keyExists;
|
return exists || keyExists;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (db.ENABLED) {
|
||||||
|
const collection = dbserver
|
||||||
|
.getClient()
|
||||||
|
.db(db.CONNECTION.DB_PREFIX_NAME + '-instances')
|
||||||
|
.collection(instanceName);
|
||||||
|
return exists || (await collection.find({}).toArray()).length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return exists || existsSync(join(INSTANCE_DIR, instanceName));
|
||||||
|
} catch (error) {
|
||||||
|
throw new InternalServerErrorException(error?.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (db.ENABLED) {
|
|
||||||
const collection = dbserver
|
|
||||||
.getClient()
|
|
||||||
.db(db.CONNECTION.DB_PREFIX_NAME + '-instances')
|
|
||||||
.collection(instanceName);
|
|
||||||
return exists || (await collection.find({}).toArray()).length > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return exists || existsSync(join(INSTANCE_DIR, instanceName));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function instanceExistsGuard(req: Request, _: Response, next: NextFunction) {
|
export async function instanceExistsGuard(req: Request, _: Response, next: NextFunction) {
|
||||||
|
Loading…
Reference in New Issue
Block a user