mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-19 11:52:20 -06:00
Compare commits
92 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3454bec79f | ||
|
|
8c27f11f5b | ||
|
|
ae9f3efeff | ||
|
|
ba3a2fae59 | ||
|
|
aa0d793d26 | ||
|
|
48bda1b5af | ||
|
|
dd21a29ea6 | ||
|
|
e83a7e2e88 | ||
|
|
d58d0b8bff | ||
|
|
4efc9b65bc | ||
|
|
cd71ff503d | ||
|
|
582166e5ae | ||
|
|
e1ae03c1e4 | ||
|
|
0737c45df2 | ||
|
|
adbe1079d5 | ||
|
|
423f629b04 | ||
|
|
946dcaeb2e | ||
|
|
d48fbc3a4e | ||
|
|
cdf06666a1 | ||
|
|
5254928887 | ||
|
|
8468690d37 | ||
|
|
bdd9257c47 | ||
|
|
d6834c8741 | ||
|
|
164beddb39 | ||
|
|
4991f1dc37 | ||
|
|
1b1e3b3e9d | ||
|
|
563ca2dd22 | ||
|
|
4e44bfb222 | ||
|
|
9edd600513 | ||
|
|
501b06d133 | ||
|
|
dc530285d5 | ||
|
|
8775cdf036 | ||
|
|
6ad33df879 | ||
|
|
633d0b4c45 | ||
|
|
82c0eadf7c | ||
|
|
1756abf1e6 | ||
|
|
a2f48030dc | ||
|
|
3214a9fb5b | ||
|
|
4b89e3f987 | ||
|
|
72622dca31 | ||
|
|
d73b72b67e | ||
|
|
20eef33df3 | ||
|
|
37571c03b4 | ||
|
|
017949458b | ||
|
|
2feaf1c74e | ||
|
|
4b043cb4b8 | ||
|
|
b0d261b305 | ||
|
|
c041986e26 | ||
|
|
0976109d27 | ||
|
|
b808dda33b | ||
|
|
98b7f15a43 | ||
|
|
94ddc0dfbe | ||
|
|
d4b0cfd2ba | ||
|
|
a5a46dc72a | ||
|
|
e13434804c | ||
|
|
53cd7d5d13 | ||
|
|
bedfb019aa | ||
|
|
6e1d027750 | ||
|
|
fb1fa4d91a | ||
|
|
57ea6707bc | ||
|
|
ad8df44236 | ||
|
|
c132379b3a | ||
|
|
f7862637b1 | ||
|
|
0d8e8bc0fb | ||
|
|
b62917e80f | ||
|
|
eeb324227b | ||
|
|
c31b62fb3d | ||
|
|
22465c0a56 | ||
|
|
da6f1bd540 | ||
|
|
069786b9fe | ||
|
|
bd0c43feac | ||
|
|
5dc1d02d0a | ||
|
|
8697329f71 | ||
|
|
58b5561f72 | ||
|
|
093515555d | ||
|
|
d8268b0eb1 | ||
|
|
4585850741 | ||
|
|
6c150eed6d | ||
|
|
78c7b96f0f | ||
|
|
dfea584aa7 | ||
|
|
6c5b056615 | ||
|
|
d8b4378163 | ||
|
|
838cc14531 | ||
|
|
878da12fa4 | ||
|
|
10a2b60595 | ||
|
|
b0ca79cd11 | ||
|
|
71eb189a6d | ||
|
|
407d254cf7 | ||
|
|
5f44da61fb | ||
|
|
41a36bbb19 | ||
|
|
8ab41fcfc9 | ||
|
|
5e08628d89 |
54
.env.example
54
.env.example
@@ -190,6 +190,60 @@ PUSHER_EVENTS_CALL=true
|
|||||||
PUSHER_EVENTS_TYPEBOT_START=false
|
PUSHER_EVENTS_TYPEBOT_START=false
|
||||||
PUSHER_EVENTS_TYPEBOT_CHANGE_STATUS=false
|
PUSHER_EVENTS_TYPEBOT_CHANGE_STATUS=false
|
||||||
|
|
||||||
|
# Kafka - Environment variables
|
||||||
|
KAFKA_ENABLED=false
|
||||||
|
KAFKA_CLIENT_ID=evolution-api
|
||||||
|
KAFKA_BROKERS=localhost:9092
|
||||||
|
KAFKA_CONNECTION_TIMEOUT=3000
|
||||||
|
KAFKA_REQUEST_TIMEOUT=30000
|
||||||
|
# Global events - By enabling this variable, events from all instances are sent to global Kafka topics.
|
||||||
|
KAFKA_GLOBAL_ENABLED=false
|
||||||
|
KAFKA_CONSUMER_GROUP_ID=evolution-api-consumers
|
||||||
|
KAFKA_TOPIC_PREFIX=evolution
|
||||||
|
KAFKA_NUM_PARTITIONS=1
|
||||||
|
KAFKA_REPLICATION_FACTOR=1
|
||||||
|
KAFKA_AUTO_CREATE_TOPICS=false
|
||||||
|
# Choose the events you want to send to Kafka
|
||||||
|
KAFKA_EVENTS_APPLICATION_STARTUP=false
|
||||||
|
KAFKA_EVENTS_INSTANCE_CREATE=false
|
||||||
|
KAFKA_EVENTS_INSTANCE_DELETE=false
|
||||||
|
KAFKA_EVENTS_QRCODE_UPDATED=false
|
||||||
|
KAFKA_EVENTS_MESSAGES_SET=false
|
||||||
|
KAFKA_EVENTS_MESSAGES_UPSERT=false
|
||||||
|
KAFKA_EVENTS_MESSAGES_EDITED=false
|
||||||
|
KAFKA_EVENTS_MESSAGES_UPDATE=false
|
||||||
|
KAFKA_EVENTS_MESSAGES_DELETE=false
|
||||||
|
KAFKA_EVENTS_SEND_MESSAGE=false
|
||||||
|
KAFKA_EVENTS_SEND_MESSAGE_UPDATE=false
|
||||||
|
KAFKA_EVENTS_CONTACTS_SET=false
|
||||||
|
KAFKA_EVENTS_CONTACTS_UPSERT=false
|
||||||
|
KAFKA_EVENTS_CONTACTS_UPDATE=false
|
||||||
|
KAFKA_EVENTS_PRESENCE_UPDATE=false
|
||||||
|
KAFKA_EVENTS_CHATS_SET=false
|
||||||
|
KAFKA_EVENTS_CHATS_UPSERT=false
|
||||||
|
KAFKA_EVENTS_CHATS_UPDATE=false
|
||||||
|
KAFKA_EVENTS_CHATS_DELETE=false
|
||||||
|
KAFKA_EVENTS_GROUPS_UPSERT=false
|
||||||
|
KAFKA_EVENTS_GROUPS_UPDATE=false
|
||||||
|
KAFKA_EVENTS_GROUP_PARTICIPANTS_UPDATE=false
|
||||||
|
KAFKA_EVENTS_CONNECTION_UPDATE=false
|
||||||
|
KAFKA_EVENTS_LABELS_EDIT=false
|
||||||
|
KAFKA_EVENTS_LABELS_ASSOCIATION=false
|
||||||
|
KAFKA_EVENTS_CALL=false
|
||||||
|
KAFKA_EVENTS_TYPEBOT_START=false
|
||||||
|
KAFKA_EVENTS_TYPEBOT_CHANGE_STATUS=false
|
||||||
|
# SASL Authentication (optional)
|
||||||
|
KAFKA_SASL_ENABLED=false
|
||||||
|
KAFKA_SASL_MECHANISM=plain
|
||||||
|
KAFKA_SASL_USERNAME=
|
||||||
|
KAFKA_SASL_PASSWORD=
|
||||||
|
# SSL Configuration (optional)
|
||||||
|
KAFKA_SSL_ENABLED=false
|
||||||
|
KAFKA_SSL_REJECT_UNAUTHORIZED=true
|
||||||
|
KAFKA_SSL_CA=
|
||||||
|
KAFKA_SSL_KEY=
|
||||||
|
KAFKA_SSL_CERT=
|
||||||
|
|
||||||
# WhatsApp Business API - Environment variables
|
# WhatsApp Business API - Environment variables
|
||||||
# Token used to validate the webhook on the Facebook APP
|
# Token used to validate the webhook on the Facebook APP
|
||||||
WA_BUSINESS_TOKEN_WEBHOOK=evolution
|
WA_BUSINESS_TOKEN_WEBHOOK=evolution
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ module.exports = {
|
|||||||
'@typescript-eslint/no-explicit-any': 'off',
|
'@typescript-eslint/no-explicit-any': 'off',
|
||||||
'@typescript-eslint/no-empty-function': 'off',
|
'@typescript-eslint/no-empty-function': 'off',
|
||||||
'@typescript-eslint/no-non-null-assertion': 'off',
|
'@typescript-eslint/no-non-null-assertion': 'off',
|
||||||
'@typescript-eslint/no-unused-vars': 'warn',
|
'@typescript-eslint/no-unused-vars': 'error',
|
||||||
'import/first': 'error',
|
'import/first': 'error',
|
||||||
'import/no-duplicates': 'error',
|
'import/no-duplicates': 'error',
|
||||||
'simple-import-sort/imports': 'error',
|
'simple-import-sort/imports': 'error',
|
||||||
|
|||||||
2
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
2
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
@@ -59,7 +59,7 @@ body:
|
|||||||
value: |
|
value: |
|
||||||
- OS: [e.g. Ubuntu 20.04, Windows 10, macOS 12.0]
|
- OS: [e.g. Ubuntu 20.04, Windows 10, macOS 12.0]
|
||||||
- Node.js version: [e.g. 18.17.0]
|
- Node.js version: [e.g. 18.17.0]
|
||||||
- Evolution API version: [e.g. 2.3.3]
|
- Evolution API version: [e.g. 2.3.6]
|
||||||
- Database: [e.g. PostgreSQL 14, MySQL 8.0]
|
- Database: [e.g. PostgreSQL 14, MySQL 8.0]
|
||||||
- Connection type: [e.g. Baileys, WhatsApp Business API]
|
- Connection type: [e.g. Baileys, WhatsApp Business API]
|
||||||
validations:
|
validations:
|
||||||
|
|||||||
2
.github/workflows/check_code_quality.yml
vendored
2
.github/workflows/check_code_quality.yml
vendored
@@ -13,6 +13,8 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v5
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
- name: Install Node
|
- name: Install Node
|
||||||
uses: actions/setup-node@v5
|
uses: actions/setup-node@v5
|
||||||
|
|||||||
2
.github/workflows/publish_docker_image.yml
vendored
2
.github/workflows/publish_docker_image.yml
vendored
@@ -15,6 +15,8 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v5
|
uses: actions/checkout@v5
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
- name: Docker meta
|
- name: Docker meta
|
||||||
id: meta
|
id: meta
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v5
|
uses: actions/checkout@v5
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
- name: Docker meta
|
- name: Docker meta
|
||||||
id: meta
|
id: meta
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v5
|
uses: actions/checkout@v5
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
- name: Docker meta
|
- name: Docker meta
|
||||||
id: meta
|
id: meta
|
||||||
|
|||||||
4
.github/workflows/security.yml
vendored
4
.github/workflows/security.yml
vendored
@@ -26,6 +26,8 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v5
|
uses: actions/checkout@v5
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
- name: Initialize CodeQL
|
- name: Initialize CodeQL
|
||||||
uses: github/codeql-action/init@v3
|
uses: github/codeql-action/init@v3
|
||||||
@@ -47,5 +49,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: Checkout Repository
|
- name: Checkout Repository
|
||||||
uses: actions/checkout@v5
|
uses: actions/checkout@v5
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
- name: Dependency Review
|
- name: Dependency Review
|
||||||
uses: actions/dependency-review-action@v4
|
uses: actions/dependency-review-action@v4
|
||||||
|
|||||||
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "evolution-manager-v2"]
|
||||||
|
path = evolution-manager-v2
|
||||||
|
url = https://github.com/EvolutionAPI/evolution-manager-v2.git
|
||||||
142
CHANGELOG.md
142
CHANGELOG.md
@@ -1,3 +1,145 @@
|
|||||||
|
# 2.3.6 (2025-10-21)
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **Baileys, Chatwoot, OnWhatsapp Cache**: Multiple implementations and fixes
|
||||||
|
- Fixed cache for PN, LID and g.us numbers to send correct number
|
||||||
|
- Fixed audio and document sending via Chatwoot in Baileys channel
|
||||||
|
- Multiple fixes in Chatwoot integration
|
||||||
|
- Fixed ignored messages when receiving leads
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* **Baileys**: Fix buffer storage in database
|
||||||
|
- Correctly save Uint8Array values to database
|
||||||
|
* **Baileys**: Simplify logging of messageSent object
|
||||||
|
- Fixed "this.isZero not is function" error
|
||||||
|
|
||||||
|
### Chore
|
||||||
|
|
||||||
|
* **Version**: Bump version to 2.3.6 and update Baileys dependency to 7.0.0-rc.6
|
||||||
|
* **Workflows**: Update checkout step to include submodules
|
||||||
|
- Added 'submodules: recursive' option to checkout step in multiple workflow files to ensure submodules are properly initialized during CI/CD processes
|
||||||
|
* **Manager**: Update asset files and install process
|
||||||
|
- Updated subproject reference in evolution-manager-v2 to the latest commit
|
||||||
|
- Enhanced the manager_install.sh script to include npm install and build steps
|
||||||
|
- Replaced old JavaScript asset file with a new version for improved performance
|
||||||
|
- Added a new CSS file for consistent styling across the application
|
||||||
|
|
||||||
|
# 2.3.5 (2025-10-15)
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **Chatwoot Enhancements**: Comprehensive improvements to message handling, editing, deletion and i18n
|
||||||
|
* **Participants Data**: Add participantsData field maintaining backward compatibility for group participants
|
||||||
|
* **LID to Phone Number**: Convert LID to phoneNumber on group participants
|
||||||
|
* **Docker Configurations**: Add Kafka and frontend services to Docker configurations
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* **Kafka Migration**: Fixed PostgreSQL migration error for Kafka integration
|
||||||
|
- Corrected table reference from `"public"."Instance"` to `"Instance"` in foreign key constraint
|
||||||
|
- Fixed `ERROR: relation "public.Instance" does not exist` issue in migration `20250918182355_add_kafka_integration`
|
||||||
|
- Aligned table naming convention with other Evolution API migrations for consistency
|
||||||
|
- Resolved database migration failure that prevented Kafka integration setup
|
||||||
|
* **Update Baileys Version**: v7.0.0-rc.5 with compatibility fixes
|
||||||
|
- Fixed assertSessions signature compatibility using type assertion
|
||||||
|
- Fixed incompatibility in voice call (wavoip) with new Baileys version
|
||||||
|
- Handle undefined status in update by defaulting to 'DELETED'
|
||||||
|
* **Chatwoot Improvements**: Multiple fixes for enhanced reliability
|
||||||
|
- Correct chatId extraction for non-group JIDs
|
||||||
|
- Resolve webhook timeout on deletion with 5+ images
|
||||||
|
- Improve error handling in Chatwoot messages
|
||||||
|
- Adjust conversation verification logic and cache
|
||||||
|
- Optimize conversation reopening logic and connection notification
|
||||||
|
- Fix conversation reopening and connection loop
|
||||||
|
* **Baileys Message Handling**: Enhanced message processing
|
||||||
|
- Add warning log for messages not found
|
||||||
|
- Fix message verification in Baileys service
|
||||||
|
- Simplify linkPreview handling in BaileysStartupService
|
||||||
|
* **Media Validation**: Fix media content validation
|
||||||
|
* **PostgreSQL Connection**: Refactor connection with PostgreSQL and improve message handling
|
||||||
|
|
||||||
|
### Code Quality & Refactoring
|
||||||
|
|
||||||
|
* **Exponential Backoff**: Implement exponential backoff patterns and extract magic numbers to constants
|
||||||
|
* **TypeScript Build**: Update TypeScript build process and dependencies
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
|
# 2.3.4 (2025-09-23)
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **Kafka Integration**: Added Apache Kafka event integration for real-time event streaming
|
||||||
|
- New Kafka controller, router, and schema for event publishing
|
||||||
|
- Support for instance-specific and global event topics
|
||||||
|
- Configurable SASL/SSL authentication and connection settings
|
||||||
|
- Auto-creation of topics with configurable partitions and replication
|
||||||
|
- Consumer group management for reliable event processing
|
||||||
|
- Integration with existing event manager for seamless event distribution
|
||||||
|
|
||||||
|
* **Evolution Manager v2 Open Source**: Evolution Manager v2 is now available as open source
|
||||||
|
- Added as git submodule with HTTPS URL for easy access
|
||||||
|
- Complete open source setup with Apache 2.0 license + Evolution API custom conditions
|
||||||
|
- GitHub templates for issues, pull requests, and workflows
|
||||||
|
- Comprehensive documentation and contribution guidelines
|
||||||
|
- Docker support for development and production environments
|
||||||
|
- CI/CD workflows for code quality, security audits, and automated builds
|
||||||
|
- Multi-language support (English, Portuguese, Spanish, French)
|
||||||
|
- Modern React + TypeScript + Vite frontend with Tailwind CSS
|
||||||
|
|
||||||
|
* **EvolutionBot Enhancements**: Improved EvolutionBot functionality and message handling
|
||||||
|
- Implemented splitMessages functionality for better message segmentation
|
||||||
|
- Added linkPreview support for enhanced message presentation
|
||||||
|
- Centralized split logic across chatbot services for consistency
|
||||||
|
- Enhanced message formatting and delivery capabilities
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* **MySQL Schema**: Fixed invalid default value errors for `createdAt` fields in `Evoai` and `EvoaiSetting` models
|
||||||
|
- Changed `@default(now())` to `@default(dbgenerated("CURRENT_TIMESTAMP"))` for MySQL compatibility
|
||||||
|
- Added missing relation fields (`N8n`, `N8nSetting`, `Evoai`, `EvoaiSetting`) in Instance model
|
||||||
|
- Resolved Prisma schema validation errors for MySQL provider
|
||||||
|
|
||||||
|
* **Prisma Schema Validation**: Fixed `instanceName` field error in message creation
|
||||||
|
- Removed invalid `instanceName` field from message objects before database insertion
|
||||||
|
- Resolved `Unknown argument 'instanceName'` Prisma validation error
|
||||||
|
- Streamlined message data structure to match Prisma schema requirements
|
||||||
|
|
||||||
|
* **Media Message Processing**: Enhanced media handling across chatbot services
|
||||||
|
- Fixed base64 conversion in EvoAI service for proper image processing
|
||||||
|
- Converted ArrayBuffer to base64 string using `Buffer.from().toString('base64')`
|
||||||
|
- Improved media URL handling and base64 encoding for better chatbot integration
|
||||||
|
- Enhanced image message detection and processing workflow
|
||||||
|
|
||||||
|
* **Evolution Manager v2 Linting**: Resolved ESLint configuration conflicts
|
||||||
|
- Disabled conflicting Prettier rules in ESLint configuration
|
||||||
|
- Added comprehensive rule overrides for TypeScript and React patterns
|
||||||
|
- Fixed import ordering and code formatting issues
|
||||||
|
- Updated security vulnerabilities in dependencies (Vite, esbuild)
|
||||||
|
|
||||||
|
### Code Quality & Refactoring
|
||||||
|
|
||||||
|
* **Chatbot Services**: Streamlined media message handling across all chatbot integrations
|
||||||
|
- Standardized base64 and mediaUrl processing patterns
|
||||||
|
- Improved code readability and maintainability in media handling logic
|
||||||
|
- Enhanced error handling for media download and conversion processes
|
||||||
|
- Unified image message detection across different chatbot services
|
||||||
|
|
||||||
|
* **Database Operations**: Improved data consistency and validation
|
||||||
|
- Enhanced Prisma schema compliance across all message operations
|
||||||
|
- Removed redundant instance name references for better data integrity
|
||||||
|
- Optimized message creation workflow with proper field validation
|
||||||
|
|
||||||
|
### Environment Variables
|
||||||
|
|
||||||
|
* Added comprehensive Kafka configuration options:
|
||||||
|
- `KAFKA_ENABLED`, `KAFKA_CLIENT_ID`, `KAFKA_BROKERS`
|
||||||
|
- `KAFKA_CONSUMER_GROUP_ID`, `KAFKA_TOPIC_PREFIX`
|
||||||
|
- `KAFKA_SASL_*` and `KAFKA_SSL_*` for authentication
|
||||||
|
- `KAFKA_EVENTS_*` for event type configuration
|
||||||
|
|
||||||
# 2.3.3 (2025-09-18)
|
# 2.3.3 (2025-09-18)
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|||||||
51
Docker/kafka/docker-compose.yaml
Normal file
51
Docker/kafka/docker-compose.yaml
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
version: '3.3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
zookeeper:
|
||||||
|
container_name: zookeeper
|
||||||
|
image: confluentinc/cp-zookeeper:7.5.0
|
||||||
|
environment:
|
||||||
|
- ZOOKEEPER_CLIENT_PORT=2181
|
||||||
|
- ZOOKEEPER_TICK_TIME=2000
|
||||||
|
- ZOOKEEPER_SYNC_LIMIT=2
|
||||||
|
volumes:
|
||||||
|
- zookeeper_data:/var/lib/zookeeper/
|
||||||
|
ports:
|
||||||
|
- 2181:2181
|
||||||
|
|
||||||
|
kafka:
|
||||||
|
container_name: kafka
|
||||||
|
image: confluentinc/cp-kafka:7.5.0
|
||||||
|
depends_on:
|
||||||
|
- zookeeper
|
||||||
|
environment:
|
||||||
|
- KAFKA_BROKER_ID=1
|
||||||
|
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
|
||||||
|
- KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT,OUTSIDE:PLAINTEXT
|
||||||
|
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092,OUTSIDE://host.docker.internal:9094
|
||||||
|
- KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT
|
||||||
|
- KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1
|
||||||
|
- KAFKA_TRANSACTION_STATE_LOG_MIN_ISR=1
|
||||||
|
- KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR=1
|
||||||
|
- KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS=0
|
||||||
|
- KAFKA_AUTO_CREATE_TOPICS_ENABLE=true
|
||||||
|
- KAFKA_LOG_RETENTION_HOURS=168
|
||||||
|
- KAFKA_LOG_SEGMENT_BYTES=1073741824
|
||||||
|
- KAFKA_LOG_RETENTION_CHECK_INTERVAL_MS=300000
|
||||||
|
- KAFKA_COMPRESSION_TYPE=gzip
|
||||||
|
ports:
|
||||||
|
- 29092:29092
|
||||||
|
- 9092:9092
|
||||||
|
- 9094:9094
|
||||||
|
volumes:
|
||||||
|
- kafka_data:/var/lib/kafka/data
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
zookeeper_data:
|
||||||
|
kafka_data:
|
||||||
|
|
||||||
|
|
||||||
|
networks:
|
||||||
|
evolution-net:
|
||||||
|
name: evolution-net
|
||||||
|
driver: bridge
|
||||||
@@ -2,7 +2,7 @@ version: "3.7"
|
|||||||
|
|
||||||
services:
|
services:
|
||||||
evolution_v2:
|
evolution_v2:
|
||||||
image: evoapicloud/evolution-api:v2.3.1
|
image: evoapicloud/evolution-api:v2.3.6
|
||||||
volumes:
|
volumes:
|
||||||
- evolution_instances:/evolution/instances
|
- evolution_instances:/evolution/instances
|
||||||
networks:
|
networks:
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@@ -17,5 +17,5 @@ b. Your contributed code may be used for commercial purposes, including but not
|
|||||||
|
|
||||||
Apart from the specific conditions mentioned above, all other rights and restrictions follow the Apache License 2.0. Detailed information about the Apache License 2.0 can be found at http://www.apache.org/licenses/LICENSE-2.0.
|
Apart from the specific conditions mentioned above, all other rights and restrictions follow the Apache License 2.0. Detailed information about the Apache License 2.0 can be found at http://www.apache.org/licenses/LICENSE-2.0.
|
||||||
|
|
||||||
© 2024 Evolution API
|
© 2025 Evolution API
|
||||||
|
|
||||||
|
|||||||
@@ -55,6 +55,9 @@ Evolution API supports various integrations to enhance its functionality. Below
|
|||||||
- [RabbitMQ](https://www.rabbitmq.com/):
|
- [RabbitMQ](https://www.rabbitmq.com/):
|
||||||
- Receive events from the Evolution API via RabbitMQ.
|
- Receive events from the Evolution API via RabbitMQ.
|
||||||
|
|
||||||
|
- [Apache Kafka](https://kafka.apache.org/):
|
||||||
|
- Receive events from the Evolution API via Apache Kafka for real-time event streaming and processing.
|
||||||
|
|
||||||
- [Amazon SQS](https://aws.amazon.com/pt/sqs/):
|
- [Amazon SQS](https://aws.amazon.com/pt/sqs/):
|
||||||
- Receive events from the Evolution API via Amazon SQS.
|
- Receive events from the Evolution API via Amazon SQS.
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,16 @@ services:
|
|||||||
expose:
|
expose:
|
||||||
- 8080
|
- 8080
|
||||||
|
|
||||||
|
frontend:
|
||||||
|
container_name: evolution_frontend
|
||||||
|
image: evolution/manager:local
|
||||||
|
build: ./evolution-manager-v2
|
||||||
|
restart: always
|
||||||
|
ports:
|
||||||
|
- "3000:80"
|
||||||
|
networks:
|
||||||
|
- evolution-net
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
evolution_instances:
|
evolution_instances:
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ version: "3.8"
|
|||||||
services:
|
services:
|
||||||
api:
|
api:
|
||||||
container_name: evolution_api
|
container_name: evolution_api
|
||||||
image: evolution/api:metrics
|
image: evoapicloud/evolution-api:latest
|
||||||
restart: always
|
restart: always
|
||||||
depends_on:
|
depends_on:
|
||||||
- redis
|
- redis
|
||||||
@@ -20,6 +20,15 @@ services:
|
|||||||
expose:
|
expose:
|
||||||
- "8080"
|
- "8080"
|
||||||
|
|
||||||
|
frontend:
|
||||||
|
container_name: evolution_frontend
|
||||||
|
image: evoapicloud/evolution-manager:latest
|
||||||
|
restart: always
|
||||||
|
ports:
|
||||||
|
- "3000:80"
|
||||||
|
networks:
|
||||||
|
- evolution-net
|
||||||
|
|
||||||
redis:
|
redis:
|
||||||
container_name: evolution_redis
|
container_name: evolution_redis
|
||||||
image: redis:latest
|
image: redis:latest
|
||||||
|
|||||||
302
env.example
Normal file
302
env.example
Normal file
@@ -0,0 +1,302 @@
|
|||||||
|
# ===========================================
|
||||||
|
# EVOLUTION API - CONFIGURAÇÃO DE AMBIENTE
|
||||||
|
# ===========================================
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# SERVIDOR
|
||||||
|
# ===========================================
|
||||||
|
SERVER_NAME=evolution
|
||||||
|
SERVER_TYPE=http
|
||||||
|
SERVER_PORT=8080
|
||||||
|
SERVER_URL=http://localhost:8080
|
||||||
|
SERVER_DISABLE_DOCS=false
|
||||||
|
SERVER_DISABLE_MANAGER=false
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# CORS
|
||||||
|
# ===========================================
|
||||||
|
CORS_ORIGIN=*
|
||||||
|
CORS_METHODS=POST,GET,PUT,DELETE
|
||||||
|
CORS_CREDENTIALS=true
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# SSL (opcional)
|
||||||
|
# ===========================================
|
||||||
|
SSL_CONF_PRIVKEY=
|
||||||
|
SSL_CONF_FULLCHAIN=
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# BANCO DE DADOS
|
||||||
|
# ===========================================
|
||||||
|
DATABASE_PROVIDER=postgresql
|
||||||
|
DATABASE_CONNECTION_URI=postgresql://username:password@localhost:5432/evolution_api
|
||||||
|
DATABASE_CONNECTION_CLIENT_NAME=evolution
|
||||||
|
|
||||||
|
# Configurações de salvamento de dados
|
||||||
|
DATABASE_SAVE_DATA_INSTANCE=true
|
||||||
|
DATABASE_SAVE_DATA_NEW_MESSAGE=true
|
||||||
|
DATABASE_SAVE_MESSAGE_UPDATE=true
|
||||||
|
DATABASE_SAVE_DATA_CONTACTS=true
|
||||||
|
DATABASE_SAVE_DATA_CHATS=true
|
||||||
|
DATABASE_SAVE_DATA_HISTORIC=true
|
||||||
|
DATABASE_SAVE_DATA_LABELS=true
|
||||||
|
DATABASE_SAVE_IS_ON_WHATSAPP=true
|
||||||
|
DATABASE_SAVE_IS_ON_WHATSAPP_DAYS=7
|
||||||
|
DATABASE_DELETE_MESSAGE=false
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# REDIS
|
||||||
|
# ===========================================
|
||||||
|
CACHE_REDIS_ENABLED=true
|
||||||
|
CACHE_REDIS_URI=redis://localhost:6379
|
||||||
|
CACHE_REDIS_PREFIX_KEY=evolution-cache
|
||||||
|
CACHE_REDIS_TTL=604800
|
||||||
|
CACHE_REDIS_SAVE_INSTANCES=true
|
||||||
|
|
||||||
|
# Cache local (fallback)
|
||||||
|
CACHE_LOCAL_ENABLED=true
|
||||||
|
CACHE_LOCAL_TTL=86400
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# AUTENTICAÇÃO
|
||||||
|
# ===========================================
|
||||||
|
AUTHENTICATION_API_KEY=BQYHJGJHJ
|
||||||
|
AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=false
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# LOGS
|
||||||
|
# ===========================================
|
||||||
|
LOG_LEVEL=ERROR,WARN,DEBUG,INFO,LOG,VERBOSE,DARK,WEBHOOKS,WEBSOCKET
|
||||||
|
LOG_COLOR=true
|
||||||
|
LOG_BAILEYS=error
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# INSTÂNCIAS
|
||||||
|
# ===========================================
|
||||||
|
DEL_INSTANCE=false
|
||||||
|
DEL_TEMP_INSTANCES=true
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# IDIOMA
|
||||||
|
# ===========================================
|
||||||
|
LANGUAGE=pt-BR
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# WEBHOOK
|
||||||
|
# ===========================================
|
||||||
|
WEBHOOK_GLOBAL_URL=
|
||||||
|
WEBHOOK_GLOBAL_ENABLED=false
|
||||||
|
WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS=false
|
||||||
|
|
||||||
|
# Eventos de webhook
|
||||||
|
WEBHOOK_EVENTS_APPLICATION_STARTUP=false
|
||||||
|
WEBHOOK_EVENTS_INSTANCE_CREATE=false
|
||||||
|
WEBHOOK_EVENTS_INSTANCE_DELETE=false
|
||||||
|
WEBHOOK_EVENTS_QRCODE_UPDATED=false
|
||||||
|
WEBHOOK_EVENTS_MESSAGES_SET=false
|
||||||
|
WEBHOOK_EVENTS_MESSAGES_UPSERT=false
|
||||||
|
WEBHOOK_EVENTS_MESSAGES_EDITED=false
|
||||||
|
WEBHOOK_EVENTS_MESSAGES_UPDATE=false
|
||||||
|
WEBHOOK_EVENTS_MESSAGES_DELETE=false
|
||||||
|
WEBHOOK_EVENTS_SEND_MESSAGE=false
|
||||||
|
WEBHOOK_EVENTS_SEND_MESSAGE_UPDATE=false
|
||||||
|
WEBHOOK_EVENTS_CONTACTS_SET=false
|
||||||
|
WEBHOOK_EVENTS_CONTACTS_UPDATE=false
|
||||||
|
WEBHOOK_EVENTS_CONTACTS_UPSERT=false
|
||||||
|
WEBHOOK_EVENTS_PRESENCE_UPDATE=false
|
||||||
|
WEBHOOK_EVENTS_CHATS_SET=false
|
||||||
|
WEBHOOK_EVENTS_CHATS_UPDATE=false
|
||||||
|
WEBHOOK_EVENTS_CHATS_UPSERT=false
|
||||||
|
WEBHOOK_EVENTS_CHATS_DELETE=false
|
||||||
|
WEBHOOK_EVENTS_CONNECTION_UPDATE=false
|
||||||
|
WEBHOOK_EVENTS_LABELS_EDIT=false
|
||||||
|
WEBHOOK_EVENTS_LABELS_ASSOCIATION=false
|
||||||
|
WEBHOOK_EVENTS_GROUPS_UPSERT=false
|
||||||
|
WEBHOOK_EVENTS_GROUPS_UPDATE=false
|
||||||
|
WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=false
|
||||||
|
WEBHOOK_EVENTS_CALL=false
|
||||||
|
WEBHOOK_EVENTS_TYPEBOT_START=false
|
||||||
|
WEBHOOK_EVENTS_TYPEBOT_CHANGE_STATUS=false
|
||||||
|
WEBHOOK_EVENTS_ERRORS=false
|
||||||
|
WEBHOOK_EVENTS_ERRORS_WEBHOOK=
|
||||||
|
|
||||||
|
# Configurações de webhook
|
||||||
|
WEBHOOK_REQUEST_TIMEOUT_MS=30000
|
||||||
|
WEBHOOK_RETRY_MAX_ATTEMPTS=10
|
||||||
|
WEBHOOK_RETRY_INITIAL_DELAY_SECONDS=5
|
||||||
|
WEBHOOK_RETRY_USE_EXPONENTIAL_BACKOFF=true
|
||||||
|
WEBHOOK_RETRY_MAX_DELAY_SECONDS=300
|
||||||
|
WEBHOOK_RETRY_JITTER_FACTOR=0.2
|
||||||
|
WEBHOOK_RETRY_NON_RETRYABLE_STATUS_CODES=400,401,403,404,422
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# WEBSOCKET
|
||||||
|
# ===========================================
|
||||||
|
WEBSOCKET_ENABLED=true
|
||||||
|
WEBSOCKET_GLOBAL_EVENTS=true
|
||||||
|
WEBSOCKET_ALLOWED_HOSTS=
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# RABBITMQ
|
||||||
|
# ===========================================
|
||||||
|
RABBITMQ_ENABLED=false
|
||||||
|
RABBITMQ_GLOBAL_ENABLED=false
|
||||||
|
RABBITMQ_PREFIX_KEY=
|
||||||
|
RABBITMQ_EXCHANGE_NAME=evolution_exchange
|
||||||
|
RABBITMQ_URI=
|
||||||
|
RABBITMQ_FRAME_MAX=8192
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# NATS
|
||||||
|
# ===========================================
|
||||||
|
NATS_ENABLED=false
|
||||||
|
NATS_GLOBAL_ENABLED=false
|
||||||
|
NATS_PREFIX_KEY=
|
||||||
|
NATS_EXCHANGE_NAME=evolution_exchange
|
||||||
|
NATS_URI=
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# SQS
|
||||||
|
# ===========================================
|
||||||
|
SQS_ENABLED=false
|
||||||
|
SQS_GLOBAL_ENABLED=false
|
||||||
|
SQS_GLOBAL_FORCE_SINGLE_QUEUE=false
|
||||||
|
SQS_GLOBAL_PREFIX_NAME=global
|
||||||
|
SQS_ACCESS_KEY_ID=
|
||||||
|
SQS_SECRET_ACCESS_KEY=
|
||||||
|
SQS_ACCOUNT_ID=
|
||||||
|
SQS_REGION=
|
||||||
|
SQS_MAX_PAYLOAD_SIZE=1048576
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# PUSHER
|
||||||
|
# ===========================================
|
||||||
|
PUSHER_ENABLED=false
|
||||||
|
PUSHER_GLOBAL_ENABLED=false
|
||||||
|
PUSHER_GLOBAL_APP_ID=
|
||||||
|
PUSHER_GLOBAL_KEY=
|
||||||
|
PUSHER_GLOBAL_SECRET=
|
||||||
|
PUSHER_GLOBAL_CLUSTER=
|
||||||
|
PUSHER_GLOBAL_USE_TLS=false
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# WHATSAPP BUSINESS
|
||||||
|
# ===========================================
|
||||||
|
WA_BUSINESS_TOKEN_WEBHOOK=evolution
|
||||||
|
WA_BUSINESS_URL=https://graph.facebook.com
|
||||||
|
WA_BUSINESS_VERSION=v18.0
|
||||||
|
WA_BUSINESS_LANGUAGE=en
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# CONFIGURAÇÕES DE SESSÃO
|
||||||
|
# ===========================================
|
||||||
|
CONFIG_SESSION_PHONE_CLIENT=Evolution API
|
||||||
|
CONFIG_SESSION_PHONE_NAME=Chrome
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# QR CODE
|
||||||
|
# ===========================================
|
||||||
|
QRCODE_LIMIT=30
|
||||||
|
QRCODE_COLOR=#198754
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# INTEGRAÇÕES
|
||||||
|
# ===========================================
|
||||||
|
|
||||||
|
# Typebot
|
||||||
|
TYPEBOT_ENABLED=false
|
||||||
|
TYPEBOT_API_VERSION=old
|
||||||
|
TYPEBOT_SEND_MEDIA_BASE64=false
|
||||||
|
|
||||||
|
# Chatwoot
|
||||||
|
CHATWOOT_ENABLED=false
|
||||||
|
CHATWOOT_MESSAGE_DELETE=false
|
||||||
|
CHATWOOT_MESSAGE_READ=false
|
||||||
|
CHATWOOT_BOT_CONTACT=true
|
||||||
|
CHATWOOT_IMPORT_DATABASE_CONNECTION_URI=
|
||||||
|
CHATWOOT_IMPORT_PLACEHOLDER_MEDIA_MESSAGE=false
|
||||||
|
|
||||||
|
# OpenAI
|
||||||
|
OPENAI_ENABLED=false
|
||||||
|
OPENAI_API_KEY_GLOBAL=
|
||||||
|
|
||||||
|
# Dify
|
||||||
|
DIFY_ENABLED=false
|
||||||
|
|
||||||
|
# N8N
|
||||||
|
N8N_ENABLED=false
|
||||||
|
|
||||||
|
# EvoAI
|
||||||
|
EVOAI_ENABLED=false
|
||||||
|
|
||||||
|
# Flowise
|
||||||
|
FLOWISE_ENABLED=false
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# S3 / MINIO
|
||||||
|
# ===========================================
|
||||||
|
S3_ENABLED=false
|
||||||
|
S3_ACCESS_KEY=
|
||||||
|
S3_SECRET_KEY=
|
||||||
|
S3_ENDPOINT=
|
||||||
|
S3_BUCKET=
|
||||||
|
S3_PORT=9000
|
||||||
|
S3_USE_SSL=false
|
||||||
|
S3_REGION=
|
||||||
|
S3_SKIP_POLICY=false
|
||||||
|
S3_SAVE_VIDEO=false
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# MÉTRICAS
|
||||||
|
# ===========================================
|
||||||
|
PROMETHEUS_METRICS=false
|
||||||
|
METRICS_AUTH_REQUIRED=false
|
||||||
|
METRICS_USER=
|
||||||
|
METRICS_PASSWORD=
|
||||||
|
METRICS_ALLOWED_IPS=
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# TELEMETRIA
|
||||||
|
# ===========================================
|
||||||
|
TELEMETRY_ENABLED=true
|
||||||
|
TELEMETRY_URL=
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# PROXY
|
||||||
|
# ===========================================
|
||||||
|
PROXY_HOST=
|
||||||
|
PROXY_PORT=
|
||||||
|
PROXY_PROTOCOL=
|
||||||
|
PROXY_USERNAME=
|
||||||
|
PROXY_PASSWORD=
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# CONVERSOR DE ÁUDIO
|
||||||
|
# ===========================================
|
||||||
|
API_AUDIO_CONVERTER=
|
||||||
|
API_AUDIO_CONVERTER_KEY=
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# FACEBOOK
|
||||||
|
# ===========================================
|
||||||
|
FACEBOOK_APP_ID=
|
||||||
|
FACEBOOK_CONFIG_ID=
|
||||||
|
FACEBOOK_USER_TOKEN=
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# SENTRY
|
||||||
|
# ===========================================
|
||||||
|
SENTRY_DSN=
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# EVENT EMITTER
|
||||||
|
# ===========================================
|
||||||
|
EVENT_EMITTER_MAX_LISTENERS=50
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# PROVIDER
|
||||||
|
# ===========================================
|
||||||
|
PROVIDER_ENABLED=false
|
||||||
|
PROVIDER_HOST=
|
||||||
|
PROVIDER_PORT=5656
|
||||||
|
PROVIDER_PREFIX=evolution
|
||||||
1
evolution-manager-v2
Submodule
1
evolution-manager-v2
Submodule
Submodule evolution-manager-v2 added at f054b9bc28
BIN
manager/dist/assets/images/evolution-logo.png
vendored
BIN
manager/dist/assets/images/evolution-logo.png
vendored
Binary file not shown.
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 25 KiB |
485
manager/dist/assets/index-CO3NSIFj.js
vendored
Normal file
485
manager/dist/assets/index-CO3NSIFj.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
manager/dist/assets/index-CXH2BdD4.css
vendored
1
manager/dist/assets/index-CXH2BdD4.css
vendored
File diff suppressed because one or more lines are too long
381
manager/dist/assets/index-D-oOjDYe.js
vendored
381
manager/dist/assets/index-D-oOjDYe.js
vendored
File diff suppressed because one or more lines are too long
1
manager/dist/assets/index-DsIrum0U.css
vendored
Normal file
1
manager/dist/assets/index-DsIrum0U.css
vendored
Normal file
File diff suppressed because one or more lines are too long
4
manager/dist/index.html
vendored
4
manager/dist/index.html
vendored
@@ -5,8 +5,8 @@
|
|||||||
<link rel="icon" type="image/png" href="https://evolution-api.com/files/evo/favicon.svg" />
|
<link rel="icon" type="image/png" href="https://evolution-api.com/files/evo/favicon.svg" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>Evolution Manager</title>
|
<title>Evolution Manager</title>
|
||||||
<script type="module" crossorigin src="/assets/index-D-oOjDYe.js"></script>
|
<script type="module" crossorigin src="/assets/index-CO3NSIFj.js"></script>
|
||||||
<link rel="stylesheet" crossorigin href="/assets/index-CXH2BdD4.css">
|
<link rel="stylesheet" crossorigin href="/assets/index-DsIrum0U.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
|
|||||||
8
manager_install.sh
Executable file
8
manager_install.sh
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
#! /bin/bash
|
||||||
|
|
||||||
|
cd evolution-manager-v2
|
||||||
|
npm install
|
||||||
|
npm run build
|
||||||
|
cd ..
|
||||||
|
rm -rf manager/dist
|
||||||
|
cp -r evolution-manager-v2/dist manager/dist
|
||||||
4340
package-lock.json
generated
4340
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "evolution-api",
|
"name": "evolution-api",
|
||||||
"version": "2.3.3",
|
"version": "2.3.6",
|
||||||
"description": "Rest api for communication with WhatsApp",
|
"description": "Rest api for communication with WhatsApp",
|
||||||
"main": "./dist/main.js",
|
"main": "./dist/main.js",
|
||||||
"type": "commonjs",
|
"type": "commonjs",
|
||||||
@@ -56,7 +56,7 @@
|
|||||||
"eslint --fix"
|
"eslint --fix"
|
||||||
],
|
],
|
||||||
"src/**/*.ts": [
|
"src/**/*.ts": [
|
||||||
"sh -c 'npm run build'"
|
"sh -c 'tsc --noEmit'"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
@@ -77,7 +77,7 @@
|
|||||||
"amqplib": "^0.10.5",
|
"amqplib": "^0.10.5",
|
||||||
"audio-decode": "^2.2.3",
|
"audio-decode": "^2.2.3",
|
||||||
"axios": "^1.7.9",
|
"axios": "^1.7.9",
|
||||||
"baileys": "^7.0.0-rc.3",
|
"baileys": "7.0.0-rc.6",
|
||||||
"class-validator": "^0.14.1",
|
"class-validator": "^0.14.1",
|
||||||
"compression": "^1.7.5",
|
"compression": "^1.7.5",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
@@ -95,6 +95,7 @@
|
|||||||
"json-schema": "^0.4.0",
|
"json-schema": "^0.4.0",
|
||||||
"jsonschema": "^1.4.1",
|
"jsonschema": "^1.4.1",
|
||||||
"jsonwebtoken": "^9.0.2",
|
"jsonwebtoken": "^9.0.2",
|
||||||
|
"kafkajs": "^2.2.4",
|
||||||
"link-preview-js": "^3.0.13",
|
"link-preview-js": "^3.0.13",
|
||||||
"long": "^5.2.3",
|
"long": "^5.2.3",
|
||||||
"mediainfo.js": "^0.3.4",
|
"mediainfo.js": "^0.3.4",
|
||||||
|
|||||||
@@ -0,0 +1,231 @@
|
|||||||
|
/*
|
||||||
|
Warnings:
|
||||||
|
|
||||||
|
- You are about to alter the column `createdAt` on the `Chat` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Chat` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Chatwoot` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Chatwoot` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Contact` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Contact` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Dify` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Dify` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `DifySetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `DifySetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Evoai` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Evoai` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `EvoaiSetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `EvoaiSetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `EvolutionBot` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `EvolutionBot` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `EvolutionBotSetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `EvolutionBotSetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Flowise` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Flowise` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `FlowiseSetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `FlowiseSetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `disconnectionAt` on the `Instance` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Instance` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Instance` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `IntegrationSession` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `IntegrationSession` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to drop the column `lid` on the `IsOnWhatsapp` table. All the data in the column will be lost.
|
||||||
|
- You are about to alter the column `createdAt` on the `IsOnWhatsapp` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `IsOnWhatsapp` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Label` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Label` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Media` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `N8n` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `N8n` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `N8nSetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `N8nSetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Nats` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Nats` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `OpenaiBot` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `OpenaiBot` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `OpenaiCreds` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `OpenaiCreds` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `OpenaiSetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `OpenaiSetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Proxy` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Proxy` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Pusher` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Pusher` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Rabbitmq` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Rabbitmq` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Session` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Setting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Setting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Sqs` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Sqs` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Template` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Template` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to drop the column `splitMessages` on the `Typebot` table. All the data in the column will be lost.
|
||||||
|
- You are about to drop the column `timePerChar` on the `Typebot` table. All the data in the column will be lost.
|
||||||
|
- You are about to alter the column `createdAt` on the `Typebot` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Typebot` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to drop the column `splitMessages` on the `TypebotSetting` table. All the data in the column will be lost.
|
||||||
|
- You are about to drop the column `timePerChar` on the `TypebotSetting` table. All the data in the column will be lost.
|
||||||
|
- You are about to alter the column `createdAt` on the `TypebotSetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `TypebotSetting` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Webhook` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Webhook` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `createdAt` on the `Websocket` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
- You are about to alter the column `updatedAt` on the `Websocket` table. The data in that column could be lost. The data in that column will be cast from `Timestamp(0)` to `Timestamp`.
|
||||||
|
|
||||||
|
*/
|
||||||
|
-- DropIndex
|
||||||
|
DROP INDEX `unique_remote_instance` ON `Chat`;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Chat` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Chatwoot` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Contact` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Dify` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `DifySetting` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Evoai` MODIFY `triggerType` ENUM('all', 'keyword', 'none', 'advanced') NULL,
|
||||||
|
MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `EvoaiSetting` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `EvolutionBot` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `EvolutionBotSetting` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Flowise` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `FlowiseSetting` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Instance` MODIFY `disconnectionAt` TIMESTAMP NULL,
|
||||||
|
MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `IntegrationSession` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `IsOnWhatsapp` DROP COLUMN `lid`,
|
||||||
|
MODIFY `createdAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Label` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Media` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `N8n` MODIFY `triggerType` ENUM('all', 'keyword', 'none', 'advanced') NULL,
|
||||||
|
MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `N8nSetting` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Nats` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `OpenaiBot` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `OpenaiCreds` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `OpenaiSetting` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Proxy` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Pusher` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Rabbitmq` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Session` MODIFY `createdAt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Setting` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Sqs` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Template` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Typebot` DROP COLUMN `splitMessages`,
|
||||||
|
DROP COLUMN `timePerChar`,
|
||||||
|
MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `TypebotSetting` DROP COLUMN `splitMessages`,
|
||||||
|
DROP COLUMN `timePerChar`,
|
||||||
|
MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Webhook` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `Websocket` MODIFY `createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
MODIFY `updatedAt` TIMESTAMP NOT NULL;
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE `Kafka` (
|
||||||
|
`id` VARCHAR(191) NOT NULL,
|
||||||
|
`enabled` BOOLEAN NOT NULL DEFAULT false,
|
||||||
|
`events` JSON NOT NULL,
|
||||||
|
`createdAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
`updatedAt` TIMESTAMP NOT NULL,
|
||||||
|
`instanceId` VARCHAR(191) NOT NULL,
|
||||||
|
|
||||||
|
UNIQUE INDEX `Kafka_instanceId_key`(`instanceId`),
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE `Kafka` ADD CONSTRAINT `Kafka_instanceId_fkey` FOREIGN KEY (`instanceId`) REFERENCES `Instance`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
@@ -88,6 +88,7 @@ model Instance {
|
|||||||
Rabbitmq Rabbitmq?
|
Rabbitmq Rabbitmq?
|
||||||
Nats Nats?
|
Nats Nats?
|
||||||
Sqs Sqs?
|
Sqs Sqs?
|
||||||
|
Kafka Kafka?
|
||||||
Websocket Websocket?
|
Websocket Websocket?
|
||||||
Typebot Typebot[]
|
Typebot Typebot[]
|
||||||
Session Session?
|
Session Session?
|
||||||
@@ -105,8 +106,11 @@ model Instance {
|
|||||||
EvolutionBotSetting EvolutionBotSetting?
|
EvolutionBotSetting EvolutionBotSetting?
|
||||||
Flowise Flowise[]
|
Flowise Flowise[]
|
||||||
FlowiseSetting FlowiseSetting?
|
FlowiseSetting FlowiseSetting?
|
||||||
Pusher Pusher?
|
|
||||||
N8n N8n[]
|
N8n N8n[]
|
||||||
|
N8nSetting N8nSetting?
|
||||||
|
Evoai Evoai[]
|
||||||
|
EvoaiSetting EvoaiSetting?
|
||||||
|
Pusher Pusher?
|
||||||
}
|
}
|
||||||
|
|
||||||
model Session {
|
model Session {
|
||||||
@@ -309,6 +313,16 @@ model Sqs {
|
|||||||
instanceId String @unique
|
instanceId String @unique
|
||||||
}
|
}
|
||||||
|
|
||||||
|
model Kafka {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
enabled Boolean @default(false)
|
||||||
|
events Json @db.Json
|
||||||
|
createdAt DateTime? @default(dbgenerated("CURRENT_TIMESTAMP")) @db.Timestamp
|
||||||
|
updatedAt DateTime @updatedAt @db.Timestamp
|
||||||
|
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
|
||||||
|
instanceId String @unique
|
||||||
|
}
|
||||||
|
|
||||||
model Websocket {
|
model Websocket {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
enabled Boolean @default(false)
|
enabled Boolean @default(false)
|
||||||
@@ -647,7 +661,7 @@ model IsOnWhatsapp {
|
|||||||
|
|
||||||
model N8n {
|
model N8n {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
enabled Boolean @default(true) @db.TinyInt(1)
|
enabled Boolean @default(true) @db.TinyInt()
|
||||||
description String? @db.VarChar(255)
|
description String? @db.VarChar(255)
|
||||||
webhookUrl String? @db.VarChar(255)
|
webhookUrl String? @db.VarChar(255)
|
||||||
basicAuthUser String? @db.VarChar(255)
|
basicAuthUser String? @db.VarChar(255)
|
||||||
@@ -666,7 +680,7 @@ model N8n {
|
|||||||
triggerType TriggerType?
|
triggerType TriggerType?
|
||||||
triggerOperator TriggerOperator?
|
triggerOperator TriggerOperator?
|
||||||
triggerValue String?
|
triggerValue String?
|
||||||
createdAt DateTime? @default(now()) @db.Timestamp
|
createdAt DateTime? @default(dbgenerated("CURRENT_TIMESTAMP")) @db.Timestamp
|
||||||
updatedAt DateTime @updatedAt @db.Timestamp
|
updatedAt DateTime @updatedAt @db.Timestamp
|
||||||
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
|
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
|
||||||
instanceId String
|
instanceId String
|
||||||
@@ -686,7 +700,7 @@ model N8nSetting {
|
|||||||
ignoreJids Json?
|
ignoreJids Json?
|
||||||
splitMessages Boolean? @default(false)
|
splitMessages Boolean? @default(false)
|
||||||
timePerChar Int? @default(50) @db.Int
|
timePerChar Int? @default(50) @db.Int
|
||||||
createdAt DateTime? @default(now()) @db.Timestamp
|
createdAt DateTime? @default(dbgenerated("CURRENT_TIMESTAMP")) @db.Timestamp
|
||||||
updatedAt DateTime @updatedAt @db.Timestamp
|
updatedAt DateTime @updatedAt @db.Timestamp
|
||||||
Fallback N8n? @relation(fields: [n8nIdFallback], references: [id])
|
Fallback N8n? @relation(fields: [n8nIdFallback], references: [id])
|
||||||
n8nIdFallback String? @db.VarChar(100)
|
n8nIdFallback String? @db.VarChar(100)
|
||||||
@@ -696,7 +710,7 @@ model N8nSetting {
|
|||||||
|
|
||||||
model Evoai {
|
model Evoai {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
enabled Boolean @default(true) @db.TinyInt(1)
|
enabled Boolean @default(true) @db.TinyInt()
|
||||||
description String? @db.VarChar(255)
|
description String? @db.VarChar(255)
|
||||||
agentUrl String? @db.VarChar(255)
|
agentUrl String? @db.VarChar(255)
|
||||||
apiKey String? @db.VarChar(255)
|
apiKey String? @db.VarChar(255)
|
||||||
@@ -714,7 +728,7 @@ model Evoai {
|
|||||||
triggerType TriggerType?
|
triggerType TriggerType?
|
||||||
triggerOperator TriggerOperator?
|
triggerOperator TriggerOperator?
|
||||||
triggerValue String?
|
triggerValue String?
|
||||||
createdAt DateTime? @default(now()) @db.Timestamp
|
createdAt DateTime? @default(dbgenerated("CURRENT_TIMESTAMP")) @db.Timestamp
|
||||||
updatedAt DateTime @updatedAt @db.Timestamp
|
updatedAt DateTime @updatedAt @db.Timestamp
|
||||||
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
|
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
|
||||||
instanceId String
|
instanceId String
|
||||||
@@ -734,7 +748,7 @@ model EvoaiSetting {
|
|||||||
ignoreJids Json?
|
ignoreJids Json?
|
||||||
splitMessages Boolean? @default(false)
|
splitMessages Boolean? @default(false)
|
||||||
timePerChar Int? @default(50) @db.Int
|
timePerChar Int? @default(50) @db.Int
|
||||||
createdAt DateTime? @default(now()) @db.Timestamp
|
createdAt DateTime? @default(dbgenerated("CURRENT_TIMESTAMP")) @db.Timestamp
|
||||||
updatedAt DateTime @updatedAt @db.Timestamp
|
updatedAt DateTime @updatedAt @db.Timestamp
|
||||||
Fallback Evoai? @relation(fields: [evoaiIdFallback], references: [id])
|
Fallback Evoai? @relation(fields: [evoaiIdFallback], references: [id])
|
||||||
evoaiIdFallback String? @db.VarChar(100)
|
evoaiIdFallback String? @db.VarChar(100)
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "Kafka" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"enabled" BOOLEAN NOT NULL DEFAULT false,
|
||||||
|
"events" JSONB NOT NULL,
|
||||||
|
"createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"updatedAt" TIMESTAMP NOT NULL,
|
||||||
|
"instanceId" TEXT NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT "Kafka_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "Kafka_instanceId_key" ON "Kafka"("instanceId");
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Kafka" ADD CONSTRAINT "Kafka_instanceId_fkey" FOREIGN KEY ("instanceId") REFERENCES "Instance"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
@@ -88,6 +88,7 @@ model Instance {
|
|||||||
Rabbitmq Rabbitmq?
|
Rabbitmq Rabbitmq?
|
||||||
Nats Nats?
|
Nats Nats?
|
||||||
Sqs Sqs?
|
Sqs Sqs?
|
||||||
|
Kafka Kafka?
|
||||||
Websocket Websocket?
|
Websocket Websocket?
|
||||||
Typebot Typebot[]
|
Typebot Typebot[]
|
||||||
Session Session?
|
Session Session?
|
||||||
@@ -312,6 +313,16 @@ model Sqs {
|
|||||||
instanceId String @unique
|
instanceId String @unique
|
||||||
}
|
}
|
||||||
|
|
||||||
|
model Kafka {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
enabled Boolean @default(false) @db.Boolean
|
||||||
|
events Json @db.JsonB
|
||||||
|
createdAt DateTime? @default(now()) @db.Timestamp
|
||||||
|
updatedAt DateTime @updatedAt @db.Timestamp
|
||||||
|
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
|
||||||
|
instanceId String @unique
|
||||||
|
}
|
||||||
|
|
||||||
model Websocket {
|
model Websocket {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
enabled Boolean @default(false) @db.Boolean
|
enabled Boolean @default(false) @db.Boolean
|
||||||
|
|||||||
@@ -89,6 +89,7 @@ model Instance {
|
|||||||
Rabbitmq Rabbitmq?
|
Rabbitmq Rabbitmq?
|
||||||
Nats Nats?
|
Nats Nats?
|
||||||
Sqs Sqs?
|
Sqs Sqs?
|
||||||
|
Kafka Kafka?
|
||||||
Websocket Websocket?
|
Websocket Websocket?
|
||||||
Typebot Typebot[]
|
Typebot Typebot[]
|
||||||
Session Session?
|
Session Session?
|
||||||
@@ -313,6 +314,16 @@ model Sqs {
|
|||||||
instanceId String @unique
|
instanceId String @unique
|
||||||
}
|
}
|
||||||
|
|
||||||
|
model Kafka {
|
||||||
|
id String @id @default(cuid())
|
||||||
|
enabled Boolean @default(false) @db.Boolean
|
||||||
|
events Json @db.JsonB
|
||||||
|
createdAt DateTime? @default(now()) @db.Timestamp
|
||||||
|
updatedAt DateTime @updatedAt @db.Timestamp
|
||||||
|
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
|
||||||
|
instanceId String @unique
|
||||||
|
}
|
||||||
|
|
||||||
model Websocket {
|
model Websocket {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
enabled Boolean @default(false) @db.Boolean
|
enabled Boolean @default(false) @db.Boolean
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import { Events, wa } from '@api/types/wa.types';
|
|||||||
import { AudioConverter, Chatwoot, ConfigService, Openai, S3 } from '@config/env.config';
|
import { AudioConverter, Chatwoot, ConfigService, Openai, S3 } from '@config/env.config';
|
||||||
import { BadRequestException, InternalServerErrorException } from '@exceptions';
|
import { BadRequestException, InternalServerErrorException } from '@exceptions';
|
||||||
import { createJid } from '@utils/createJid';
|
import { createJid } from '@utils/createJid';
|
||||||
|
import { sendTelemetry } from '@utils/sendTelemetry';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { isBase64, isURL } from 'class-validator';
|
import { isBase64, isURL } from 'class-validator';
|
||||||
import EventEmitter2 from 'eventemitter2';
|
import EventEmitter2 from 'eventemitter2';
|
||||||
@@ -171,6 +172,8 @@ export class EvolutionStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
this.logger.log(messageRaw);
|
this.logger.log(messageRaw);
|
||||||
|
|
||||||
|
sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`);
|
||||||
|
|
||||||
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
|
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
|
||||||
|
|
||||||
await chatbotController.emit({
|
await chatbotController.emit({
|
||||||
@@ -323,8 +326,8 @@ export class EvolutionStartupService extends ChannelStartupService {
|
|||||||
messageRaw = {
|
messageRaw = {
|
||||||
key: { fromMe: true, id: messageId, remoteJid: number },
|
key: { fromMe: true, id: messageId, remoteJid: number },
|
||||||
message: {
|
message: {
|
||||||
base64: isBase64(message.media) ? message.media : undefined,
|
base64: isBase64(message.media) ? message.media : null,
|
||||||
mediaUrl: isURL(message.media) ? message.media : undefined,
|
mediaUrl: isURL(message.media) ? message.media : null,
|
||||||
quoted,
|
quoted,
|
||||||
},
|
},
|
||||||
messageType: 'imageMessage',
|
messageType: 'imageMessage',
|
||||||
@@ -337,8 +340,8 @@ export class EvolutionStartupService extends ChannelStartupService {
|
|||||||
messageRaw = {
|
messageRaw = {
|
||||||
key: { fromMe: true, id: messageId, remoteJid: number },
|
key: { fromMe: true, id: messageId, remoteJid: number },
|
||||||
message: {
|
message: {
|
||||||
base64: isBase64(message.media) ? message.media : undefined,
|
base64: isBase64(message.media) ? message.media : null,
|
||||||
mediaUrl: isURL(message.media) ? message.media : undefined,
|
mediaUrl: isURL(message.media) ? message.media : null,
|
||||||
quoted,
|
quoted,
|
||||||
},
|
},
|
||||||
messageType: 'videoMessage',
|
messageType: 'videoMessage',
|
||||||
@@ -351,8 +354,8 @@ export class EvolutionStartupService extends ChannelStartupService {
|
|||||||
messageRaw = {
|
messageRaw = {
|
||||||
key: { fromMe: true, id: messageId, remoteJid: number },
|
key: { fromMe: true, id: messageId, remoteJid: number },
|
||||||
message: {
|
message: {
|
||||||
base64: isBase64(message.media) ? message.media : undefined,
|
base64: isBase64(message.media) ? message.media : null,
|
||||||
mediaUrl: isURL(message.media) ? message.media : undefined,
|
mediaUrl: isURL(message.media) ? message.media : null,
|
||||||
quoted,
|
quoted,
|
||||||
},
|
},
|
||||||
messageType: 'audioMessage',
|
messageType: 'audioMessage',
|
||||||
@@ -372,8 +375,8 @@ export class EvolutionStartupService extends ChannelStartupService {
|
|||||||
messageRaw = {
|
messageRaw = {
|
||||||
key: { fromMe: true, id: messageId, remoteJid: number },
|
key: { fromMe: true, id: messageId, remoteJid: number },
|
||||||
message: {
|
message: {
|
||||||
base64: isBase64(message.media) ? message.media : undefined,
|
base64: isBase64(message.media) ? message.media : null,
|
||||||
mediaUrl: isURL(message.media) ? message.media : undefined,
|
mediaUrl: isURL(message.media) ? message.media : null,
|
||||||
quoted,
|
quoted,
|
||||||
},
|
},
|
||||||
messageType: 'documentMessage',
|
messageType: 'documentMessage',
|
||||||
@@ -449,7 +452,7 @@ export class EvolutionStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const base64 = messageRaw.message.base64;
|
const { base64 } = messageRaw.message;
|
||||||
delete messageRaw.message.base64;
|
delete messageRaw.message.base64;
|
||||||
|
|
||||||
if (base64 || file || audioFile) {
|
if (base64 || file || audioFile) {
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import { AudioConverter, Chatwoot, ConfigService, Database, Openai, S3, WaBusine
|
|||||||
import { BadRequestException, InternalServerErrorException } from '@exceptions';
|
import { BadRequestException, InternalServerErrorException } from '@exceptions';
|
||||||
import { createJid } from '@utils/createJid';
|
import { createJid } from '@utils/createJid';
|
||||||
import { status } from '@utils/renderStatus';
|
import { status } from '@utils/renderStatus';
|
||||||
|
import { sendTelemetry } from '@utils/sendTelemetry';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { arrayUnique, isURL } from 'class-validator';
|
import { arrayUnique, isURL } from 'class-validator';
|
||||||
import EventEmitter2 from 'eventemitter2';
|
import EventEmitter2 from 'eventemitter2';
|
||||||
@@ -655,6 +656,8 @@ export class BusinessStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
this.logger.log(messageRaw);
|
this.logger.log(messageRaw);
|
||||||
|
|
||||||
|
sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`);
|
||||||
|
|
||||||
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
|
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
|
||||||
|
|
||||||
await chatbotController.emit({
|
await chatbotController.emit({
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Logger } from '@config/logger.config';
|
import { Logger } from '@config/logger.config';
|
||||||
import { BaileysEventMap, MessageUpsertType, proto } from 'baileys';
|
import { BaileysEventMap, MessageUpsertType, WAMessage } from 'baileys';
|
||||||
import { catchError, concatMap, delay, EMPTY, from, retryWhen, Subject, Subscription, take, tap } from 'rxjs';
|
import { catchError, concatMap, delay, EMPTY, from, retryWhen, Subject, Subscription, take, tap } from 'rxjs';
|
||||||
|
|
||||||
type MessageUpsertPayload = BaileysEventMap['messages.upsert'];
|
type MessageUpsertPayload = BaileysEventMap['messages.upsert'];
|
||||||
@@ -12,7 +12,7 @@ export class BaileysMessageProcessor {
|
|||||||
private subscription?: Subscription;
|
private subscription?: Subscription;
|
||||||
|
|
||||||
protected messageSubject = new Subject<{
|
protected messageSubject = new Subject<{
|
||||||
messages: proto.IWebMessageInfo[];
|
messages: WAMessage[];
|
||||||
type: MessageUpsertType;
|
type: MessageUpsertType;
|
||||||
requestId?: string;
|
requestId?: string;
|
||||||
settings: any;
|
settings: any;
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ export const useVoiceCallsBaileys = async (
|
|||||||
|
|
||||||
socket.on('assertSessions', async (jids, force, callback) => {
|
socket.on('assertSessions', async (jids, force, callback) => {
|
||||||
try {
|
try {
|
||||||
const response = await baileys_sock.assertSessions(jids, force);
|
const response = await baileys_sock.assertSessions(jids);
|
||||||
|
|
||||||
callback(response);
|
callback(response);
|
||||||
|
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ import { fetchLatestWaWebVersion } from '@utils/fetchLatestWaWebVersion';
|
|||||||
import { makeProxyAgent } from '@utils/makeProxyAgent';
|
import { makeProxyAgent } from '@utils/makeProxyAgent';
|
||||||
import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache';
|
import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache';
|
||||||
import { status } from '@utils/renderStatus';
|
import { status } from '@utils/renderStatus';
|
||||||
|
import { sendTelemetry } from '@utils/sendTelemetry';
|
||||||
import useMultiFileAuthStatePrisma from '@utils/use-multi-file-auth-state-prisma';
|
import useMultiFileAuthStatePrisma from '@utils/use-multi-file-auth-state-prisma';
|
||||||
import { AuthStateProvider } from '@utils/use-multi-file-auth-state-provider-files';
|
import { AuthStateProvider } from '@utils/use-multi-file-auth-state-provider-files';
|
||||||
import { useMultiFileAuthStateRedisDb } from '@utils/use-multi-file-auth-state-redis-db';
|
import { useMultiFileAuthStateRedisDb } from '@utils/use-multi-file-auth-state-redis-db';
|
||||||
@@ -132,7 +133,6 @@ import { Label } from 'baileys/lib/Types/Label';
|
|||||||
import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation';
|
import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation';
|
||||||
import { spawn } from 'child_process';
|
import { spawn } from 'child_process';
|
||||||
import { isArray, isBase64, isURL } from 'class-validator';
|
import { isArray, isBase64, isURL } from 'class-validator';
|
||||||
import { randomBytes } from 'crypto';
|
|
||||||
import EventEmitter2 from 'eventemitter2';
|
import EventEmitter2 from 'eventemitter2';
|
||||||
import ffmpeg from 'fluent-ffmpeg';
|
import ffmpeg from 'fluent-ffmpeg';
|
||||||
import FormData from 'form-data';
|
import FormData from 'form-data';
|
||||||
@@ -152,13 +152,7 @@ import { v4 } from 'uuid';
|
|||||||
import { BaileysMessageProcessor } from './baileysMessage.processor';
|
import { BaileysMessageProcessor } from './baileysMessage.processor';
|
||||||
import { useVoiceCallsBaileys } from './voiceCalls/useVoiceCallsBaileys';
|
import { useVoiceCallsBaileys } from './voiceCalls/useVoiceCallsBaileys';
|
||||||
|
|
||||||
export interface ExtendedMessageKey extends WAMessageKey {
|
|
||||||
senderPn?: string;
|
|
||||||
previousRemoteJid?: string | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ExtendedIMessageKey extends proto.IMessageKey {
|
export interface ExtendedIMessageKey extends proto.IMessageKey {
|
||||||
senderPn?: string;
|
|
||||||
remoteJidAlt?: string;
|
remoteJidAlt?: string;
|
||||||
participantAlt?: string;
|
participantAlt?: string;
|
||||||
server_id?: string;
|
server_id?: string;
|
||||||
@@ -254,6 +248,10 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
private endSession = false;
|
private endSession = false;
|
||||||
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
|
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
|
||||||
|
|
||||||
|
// Cache TTL constants (in seconds)
|
||||||
|
private readonly MESSAGE_CACHE_TTL_SECONDS = 5 * 60; // 5 minutes - avoid duplicate message processing
|
||||||
|
private readonly UPDATE_CACHE_TTL_SECONDS = 30 * 60; // 30 minutes - avoid duplicate status updates
|
||||||
|
|
||||||
public stateConnection: wa.StateConnection = { state: 'close' };
|
public stateConnection: wa.StateConnection = { state: 'close' };
|
||||||
|
|
||||||
public phoneNumber: string;
|
public phoneNumber: string;
|
||||||
@@ -445,7 +443,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
try {
|
try {
|
||||||
const profilePic = await this.profilePicture(this.instance.wuid);
|
const profilePic = await this.profilePicture(this.instance.wuid);
|
||||||
this.instance.profilePictureUrl = profilePic.profilePictureUrl;
|
this.instance.profilePictureUrl = profilePic.profilePictureUrl;
|
||||||
} catch (error) {
|
} catch {
|
||||||
this.instance.profilePictureUrl = null;
|
this.instance.profilePictureUrl = null;
|
||||||
}
|
}
|
||||||
const formattedWuid = this.instance.wuid.split('@')[0].padEnd(30, ' ');
|
const formattedWuid = this.instance.wuid.split('@')[0].padEnd(30, ' ');
|
||||||
@@ -500,8 +498,8 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
try {
|
try {
|
||||||
// Use raw SQL to avoid JSON path issues
|
// Use raw SQL to avoid JSON path issues
|
||||||
const webMessageInfo = (await this.prismaRepository.$queryRaw`
|
const webMessageInfo = (await this.prismaRepository.$queryRaw`
|
||||||
SELECT * FROM "Message"
|
SELECT * FROM "Message"
|
||||||
WHERE "instanceId" = ${this.instanceId}
|
WHERE "instanceId" = ${this.instanceId}
|
||||||
AND "key"->>'id' = ${key.id}
|
AND "key"->>'id' = ${key.id}
|
||||||
`) as proto.IWebMessageInfo[];
|
`) as proto.IWebMessageInfo[];
|
||||||
|
|
||||||
@@ -524,7 +522,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return webMessageInfo[0].message;
|
return webMessageInfo[0].message;
|
||||||
} catch (error) {
|
} catch {
|
||||||
return { conversation: '' };
|
return { conversation: '' };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -597,7 +595,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
const rand = Math.floor(Math.random() * Math.floor(proxyUrls.length));
|
const rand = Math.floor(Math.random() * Math.floor(proxyUrls.length));
|
||||||
const proxyUrl = 'http://' + proxyUrls[rand];
|
const proxyUrl = 'http://' + proxyUrls[rand];
|
||||||
options = { agent: makeProxyAgent(proxyUrl), fetchAgent: makeProxyAgent(proxyUrl) };
|
options = { agent: makeProxyAgent(proxyUrl), fetchAgent: makeProxyAgent(proxyUrl) };
|
||||||
} catch (error) {
|
} catch {
|
||||||
this.localProxy.enabled = false;
|
this.localProxy.enabled = false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -877,6 +875,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
'contacts.update': async (contacts: Partial<Contact>[]) => {
|
'contacts.update': async (contacts: Partial<Contact>[]) => {
|
||||||
const contactsRaw: { remoteJid: string; pushName?: string; profilePicUrl?: string; instanceId: string }[] = [];
|
const contactsRaw: { remoteJid: string; pushName?: string; profilePicUrl?: string; instanceId: string }[] = [];
|
||||||
for await (const contact of contacts) {
|
for await (const contact of contacts) {
|
||||||
|
this.logger.debug(`Updating contact: ${JSON.stringify(contact, null, 2)}`);
|
||||||
contactsRaw.push({
|
contactsRaw.push({
|
||||||
remoteJid: contact.id,
|
remoteJid: contact.id,
|
||||||
pushName: contact?.name ?? contact?.verifiedName,
|
pushName: contact?.name ?? contact?.verifiedName,
|
||||||
@@ -896,10 +895,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
);
|
);
|
||||||
await this.prismaRepository.$transaction(updateTransactions);
|
await this.prismaRepository.$transaction(updateTransactions);
|
||||||
|
|
||||||
const usersContacts = contactsRaw.filter((c) => c.remoteJid.includes('@s.whatsapp'));
|
//const usersContacts = contactsRaw.filter((c) => c.remoteJid.includes('@s.whatsapp'));
|
||||||
if (usersContacts) {
|
|
||||||
await saveOnWhatsappCache(usersContacts.map((c) => ({ remoteJid: c.remoteJid })));
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1000,10 +996,6 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m.key.remoteJid?.includes('@lid') && (m.key as ExtendedIMessageKey).senderPn) {
|
|
||||||
m.key.remoteJid = (m.key as ExtendedIMessageKey).senderPn;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Long.isLong(m?.messageTimestamp)) {
|
if (Long.isLong(m?.messageTimestamp)) {
|
||||||
m.messageTimestamp = m.messageTimestamp?.toNumber();
|
m.messageTimestamp = m.messageTimestamp?.toNumber();
|
||||||
}
|
}
|
||||||
@@ -1066,10 +1058,6 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
for (const received of messages) {
|
for (const received of messages) {
|
||||||
if (received.key.remoteJid?.includes('@lid') && (received.key as ExtendedMessageKey).senderPn) {
|
|
||||||
(received.key as ExtendedMessageKey).previousRemoteJid = received.key.remoteJid;
|
|
||||||
received.key.remoteJid = (received.key as ExtendedMessageKey).senderPn;
|
|
||||||
}
|
|
||||||
if (
|
if (
|
||||||
received?.messageStubParameters?.some?.((param) =>
|
received?.messageStubParameters?.some?.((param) =>
|
||||||
[
|
[
|
||||||
@@ -1117,9 +1105,9 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
await this.sendDataWebhook(Events.MESSAGES_EDITED, editedMessage);
|
await this.sendDataWebhook(Events.MESSAGES_EDITED, editedMessage);
|
||||||
const oldMessage = await this.getMessage(editedMessage.key, true);
|
const oldMessage = await this.getMessage(editedMessage.key, true);
|
||||||
if ((oldMessage as any)?.id) {
|
if ((oldMessage as any)?.id) {
|
||||||
const editedMessageTimestamp = Long.isLong(editedMessage?.timestampMs)
|
const editedMessageTimestamp = Long.isLong(received?.messageTimestamp)
|
||||||
? Math.floor(editedMessage.timestampMs.toNumber() / 1000)
|
? Math.floor(received?.messageTimestamp.toNumber())
|
||||||
: Math.floor((editedMessage.timestampMs as number) / 1000);
|
: Math.floor(received?.messageTimestamp as number);
|
||||||
|
|
||||||
await this.prismaRepository.message.update({
|
await this.prismaRepository.message.update({
|
||||||
where: { id: (oldMessage as any).id },
|
where: { id: (oldMessage as any).id },
|
||||||
@@ -1145,12 +1133,12 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
const messageKey = `${this.instance.id}_${received.key.id}`;
|
const messageKey = `${this.instance.id}_${received.key.id}`;
|
||||||
const cached = await this.baileysCache.get(messageKey);
|
const cached = await this.baileysCache.get(messageKey);
|
||||||
|
|
||||||
if (cached && !editedMessage) {
|
if (cached && !editedMessage && !requestId) {
|
||||||
this.logger.info(`Message duplicated ignored: ${received.key.id}`);
|
this.logger.info(`Message duplicated ignored: ${received.key.id}`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.baileysCache.set(messageKey, true, 5 * 60);
|
await this.baileysCache.set(messageKey, true, this.MESSAGE_CACHE_TTL_SECONDS);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(type !== 'notify' && type !== 'append') ||
|
(type !== 'notify' && type !== 'append') ||
|
||||||
@@ -1189,7 +1177,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
where: { id: existingChat.id },
|
where: { id: existingChat.id },
|
||||||
data: { name: received.pushName },
|
data: { name: received.pushName },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch {
|
||||||
console.log(`Chat insert record ignored: ${received.key.remoteJid} - ${this.instanceId}`);
|
console.log(`Chat insert record ignored: ${received.key.remoteJid} - ${this.instanceId}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1270,7 +1258,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
await this.updateMessagesReadedByTimestamp(remoteJid, timestamp);
|
await this.updateMessagesReadedByTimestamp(remoteJid, timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.baileysCache.set(messageKey, true, 5 * 60);
|
await this.baileysCache.set(messageKey, true, this.MESSAGE_CACHE_TTL_SECONDS);
|
||||||
} else {
|
} else {
|
||||||
this.logger.info(`Update readed messages duplicated ignored [avoid deadlock]: ${messageKey}`);
|
this.logger.info(`Update readed messages duplicated ignored [avoid deadlock]: ${messageKey}`);
|
||||||
}
|
}
|
||||||
@@ -1358,11 +1346,9 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (messageRaw.key.remoteJid?.includes('@lid') && messageRaw.key.remoteJidAlt) {
|
this.logger.verbose(messageRaw);
|
||||||
messageRaw.key.remoteJid = messageRaw.key.remoteJidAlt;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.log(messageRaw);
|
sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`);
|
||||||
|
|
||||||
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
|
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
|
||||||
|
|
||||||
@@ -1377,7 +1363,12 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
where: { remoteJid: received.key.remoteJid, instanceId: this.instanceId },
|
where: { remoteJid: received.key.remoteJid, instanceId: this.instanceId },
|
||||||
});
|
});
|
||||||
|
|
||||||
const contactRaw: { remoteJid: string; pushName: string; profilePicUrl?: string; instanceId: string } = {
|
const contactRaw: {
|
||||||
|
remoteJid: string;
|
||||||
|
pushName: string;
|
||||||
|
profilePicUrl?: string;
|
||||||
|
instanceId: string;
|
||||||
|
} = {
|
||||||
remoteJid: received.key.remoteJid,
|
remoteJid: received.key.remoteJid,
|
||||||
pushName: received.key.fromMe ? '' : received.key.fromMe == null ? '' : received.pushName,
|
pushName: received.key.fromMe ? '' : received.key.fromMe == null ? '' : received.pushName,
|
||||||
profilePicUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
|
profilePicUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
|
||||||
@@ -1388,6 +1379,17 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (contactRaw.remoteJid.includes('@s.whatsapp') || contactRaw.remoteJid.includes('@lid')) {
|
||||||
|
await saveOnWhatsappCache([
|
||||||
|
{
|
||||||
|
remoteJid:
|
||||||
|
messageRaw.key.addressingMode === 'lid' ? messageRaw.key.remoteJidAlt : messageRaw.key.remoteJid,
|
||||||
|
remoteJidAlt: messageRaw.key.remoteJidAlt,
|
||||||
|
lid: messageRaw.key.addressingMode === 'lid' ? 'lid' : null,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
if (contact) {
|
if (contact) {
|
||||||
this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw);
|
this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw);
|
||||||
|
|
||||||
@@ -1417,10 +1419,6 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
update: contactRaw,
|
update: contactRaw,
|
||||||
create: contactRaw,
|
create: contactRaw,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (contactRaw.remoteJid.includes('@s.whatsapp')) {
|
|
||||||
await saveOnWhatsappCache([{ remoteJid: contactRaw.remoteJid }]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
@@ -1428,7 +1426,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
},
|
},
|
||||||
|
|
||||||
'messages.update': async (args: { update: Partial<WAMessage>; key: WAMessageKey }[], settings: any) => {
|
'messages.update': async (args: { update: Partial<WAMessage>; key: WAMessageKey }[], settings: any) => {
|
||||||
this.logger.log(`Update messages ${JSON.stringify(args, undefined, 2)}`);
|
this.logger.verbose(`Update messages ${JSON.stringify(args, undefined, 2)}`);
|
||||||
|
|
||||||
const readChatToUpdate: Record<string, true> = {}; // {remoteJid: true}
|
const readChatToUpdate: Record<string, true> = {}; // {remoteJid: true}
|
||||||
|
|
||||||
@@ -1437,9 +1435,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key.remoteJid?.includes('@lid') && key.remoteJidAlt) {
|
if (update.message !== null && update.status === undefined) continue;
|
||||||
key.remoteJid = key.remoteJidAlt;
|
|
||||||
}
|
|
||||||
|
|
||||||
const updateKey = `${this.instance.id}_${key.id}_${update.status}`;
|
const updateKey = `${this.instance.id}_${key.id}_${update.status}`;
|
||||||
|
|
||||||
@@ -1480,7 +1476,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
keyId: key.id,
|
keyId: key.id,
|
||||||
remoteJid: key?.remoteJid,
|
remoteJid: key?.remoteJid,
|
||||||
fromMe: key.fromMe,
|
fromMe: key.fromMe,
|
||||||
participant: key?.remoteJid,
|
participant: key?.participant,
|
||||||
status: status[update.status] ?? 'DELETED',
|
status: status[update.status] ?? 'DELETED',
|
||||||
pollUpdates,
|
pollUpdates,
|
||||||
instanceId: this.instanceId,
|
instanceId: this.instanceId,
|
||||||
@@ -1491,14 +1487,18 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
if (configDatabaseData.HISTORIC || configDatabaseData.NEW_MESSAGE) {
|
if (configDatabaseData.HISTORIC || configDatabaseData.NEW_MESSAGE) {
|
||||||
// Use raw SQL to avoid JSON path issues
|
// Use raw SQL to avoid JSON path issues
|
||||||
const messages = (await this.prismaRepository.$queryRaw`
|
const messages = (await this.prismaRepository.$queryRaw`
|
||||||
SELECT * FROM "Message"
|
SELECT * FROM "Message"
|
||||||
WHERE "instanceId" = ${this.instanceId}
|
WHERE "instanceId" = ${this.instanceId}
|
||||||
AND "key"->>'id' = ${key.id}
|
AND "key"->>'id' = ${key.id}
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
`) as any[];
|
`) as any[];
|
||||||
findMessage = messages[0] || null;
|
findMessage = messages[0] || null;
|
||||||
|
|
||||||
if (findMessage) message.messageId = findMessage.id;
|
if (!findMessage?.id) {
|
||||||
|
this.logger.warn(`Original message not found for update. Skipping. Key: ${JSON.stringify(key)}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
message.messageId = findMessage.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (update.message === null && update.status === undefined) {
|
if (update.message === null && update.status === undefined) {
|
||||||
@@ -1533,7 +1533,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
if (status[update.status] === status[4]) {
|
if (status[update.status] === status[4]) {
|
||||||
this.logger.log(`Update as read in message.update ${remoteJid} - ${timestamp}`);
|
this.logger.log(`Update as read in message.update ${remoteJid} - ${timestamp}`);
|
||||||
await this.updateMessagesReadedByTimestamp(remoteJid, timestamp);
|
await this.updateMessagesReadedByTimestamp(remoteJid, timestamp);
|
||||||
await this.baileysCache.set(messageKey, true, 5 * 60);
|
await this.baileysCache.set(messageKey, true, this.MESSAGE_CACHE_TTL_SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.prismaRepository.message.update({
|
await this.prismaRepository.message.update({
|
||||||
@@ -1564,7 +1564,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) {
|
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) {
|
||||||
try {
|
try {
|
||||||
await this.prismaRepository.chat.update({ where: { id: existingChat.id }, data: chatToInsert });
|
await this.prismaRepository.chat.update({ where: { id: existingChat.id }, data: chatToInsert });
|
||||||
} catch (error) {
|
} catch {
|
||||||
console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`);
|
console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1591,12 +1591,66 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
'group-participants.update': (participantsUpdate: {
|
'group-participants.update': async (participantsUpdate: {
|
||||||
id: string;
|
id: string;
|
||||||
participants: string[];
|
participants: string[];
|
||||||
action: ParticipantAction;
|
action: ParticipantAction;
|
||||||
}) => {
|
}) => {
|
||||||
this.sendDataWebhook(Events.GROUP_PARTICIPANTS_UPDATE, participantsUpdate);
|
// ENHANCEMENT: Adds participantsData field while maintaining backward compatibility
|
||||||
|
// MAINTAINS: participants: string[] (original JID strings)
|
||||||
|
// ADDS: participantsData: { jid: string, phoneNumber: string, name?: string, imgUrl?: string }[]
|
||||||
|
// This enables LID to phoneNumber conversion without breaking existing webhook consumers
|
||||||
|
|
||||||
|
// Helper to normalize participantId as phone number
|
||||||
|
const normalizePhoneNumber = (id: string): string => {
|
||||||
|
// Remove @lid, @s.whatsapp.net suffixes and extract just the number part
|
||||||
|
return id.split('@')[0];
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Usa o mesmo método que o endpoint /group/participants
|
||||||
|
const groupParticipants = await this.findParticipants({ groupJid: participantsUpdate.id });
|
||||||
|
|
||||||
|
// Validação para garantir que temos dados válidos
|
||||||
|
if (!groupParticipants?.participants || !Array.isArray(groupParticipants.participants)) {
|
||||||
|
throw new Error('Invalid participant data received from findParticipants');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filtra apenas os participantes que estão no evento
|
||||||
|
const resolvedParticipants = participantsUpdate.participants.map((participantId) => {
|
||||||
|
const participantData = groupParticipants.participants.find((p) => p.id === participantId);
|
||||||
|
|
||||||
|
let phoneNumber: string;
|
||||||
|
if (participantData?.phoneNumber) {
|
||||||
|
phoneNumber = participantData.phoneNumber;
|
||||||
|
} else {
|
||||||
|
phoneNumber = normalizePhoneNumber(participantId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
jid: participantId,
|
||||||
|
phoneNumber,
|
||||||
|
name: participantData?.name,
|
||||||
|
imgUrl: participantData?.imgUrl,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// Mantém formato original + adiciona dados resolvidos
|
||||||
|
const enhancedParticipantsUpdate = {
|
||||||
|
...participantsUpdate,
|
||||||
|
participants: participantsUpdate.participants, // Mantém array original de strings
|
||||||
|
// Adiciona dados resolvidos em campo separado
|
||||||
|
participantsData: resolvedParticipants,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.sendDataWebhook(Events.GROUP_PARTICIPANTS_UPDATE, enhancedParticipantsUpdate);
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(
|
||||||
|
`Failed to resolve participant data for GROUP_PARTICIPANTS_UPDATE webhook: ${error.message} | Group: ${participantsUpdate.id} | Participants: ${participantsUpdate.participants.length}`,
|
||||||
|
);
|
||||||
|
// Fallback - envia sem conversão
|
||||||
|
this.sendDataWebhook(Events.GROUP_PARTICIPANTS_UPDATE, participantsUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
this.updateGroupMetadataCache(participantsUpdate.id);
|
this.updateGroupMetadataCache(participantsUpdate.id);
|
||||||
},
|
},
|
||||||
@@ -1678,6 +1732,9 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (settings?.msgCall?.trim().length > 0 && call.status == 'offer') {
|
if (settings?.msgCall?.trim().length > 0 && call.status == 'offer') {
|
||||||
|
if (call.from.endsWith('@lid')) {
|
||||||
|
call.from = await this.client.signalRepository.lidMapping.getPNForLID(call.from as string);
|
||||||
|
}
|
||||||
const msg = await this.client.sendMessage(call.from, { text: settings.msgCall });
|
const msg = await this.client.sendMessage(call.from, { text: settings.msgCall });
|
||||||
|
|
||||||
this.client.ev.emit('messages.upsert', { messages: [msg], type: 'notify' });
|
this.client.ev.emit('messages.upsert', { messages: [msg], type: 'notify' });
|
||||||
@@ -1750,7 +1807,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (events['group-participants.update']) {
|
if (events['group-participants.update']) {
|
||||||
const payload = events['group-participants.update'];
|
const payload = events['group-participants.update'] as any;
|
||||||
this.groupHandler['group-participants.update'](payload);
|
this.groupHandler['group-participants.update'](payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1832,7 +1889,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
const profilePictureUrl = await this.client.profilePictureUrl(jid, 'image');
|
const profilePictureUrl = await this.client.profilePictureUrl(jid, 'image');
|
||||||
|
|
||||||
return { wuid: jid, profilePictureUrl };
|
return { wuid: jid, profilePictureUrl };
|
||||||
} catch (error) {
|
} catch {
|
||||||
return { wuid: jid, profilePictureUrl: null };
|
return { wuid: jid, profilePictureUrl: null };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1842,7 +1899,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
return { wuid: jid, status: (await this.client.fetchStatus(jid))[0]?.status };
|
return { wuid: jid, status: (await this.client.fetchStatus(jid))[0]?.status };
|
||||||
} catch (error) {
|
} catch {
|
||||||
return { wuid: jid, status: null };
|
return { wuid: jid, status: null };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1891,7 +1948,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
website: business?.website?.shift(),
|
website: business?.website?.shift(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch {
|
||||||
return { wuid: jid, name: null, picture: null, status: null, os: null, isBusiness: false };
|
return { wuid: jid, name: null, picture: null, status: null, os: null, isBusiness: false };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1918,6 +1975,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
quoted: any,
|
quoted: any,
|
||||||
messageId?: string,
|
messageId?: string,
|
||||||
ephemeralExpiration?: number,
|
ephemeralExpiration?: number,
|
||||||
|
contextInfo?: any,
|
||||||
// participants?: GroupParticipant[],
|
// participants?: GroupParticipant[],
|
||||||
) {
|
) {
|
||||||
sender = sender.toLowerCase();
|
sender = sender.toLowerCase();
|
||||||
@@ -1934,8 +1992,8 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
if (ephemeralExpiration) option.ephemeralExpiration = ephemeralExpiration;
|
if (ephemeralExpiration) option.ephemeralExpiration = ephemeralExpiration;
|
||||||
|
|
||||||
|
// NOTE: NÃO DEVEMOS GERAR O messageId AQUI, SOMENTE SE VIER INFORMADO POR PARAMETRO. A GERAÇÃO ANTERIOR IMPEDE O WZAP DE IDENTIFICAR A SOURCE.
|
||||||
if (messageId) option.messageId = messageId;
|
if (messageId) option.messageId = messageId;
|
||||||
else option.messageId = '3EB0' + randomBytes(18).toString('hex').toUpperCase();
|
|
||||||
|
|
||||||
if (message['viewOnceMessage']) {
|
if (message['viewOnceMessage']) {
|
||||||
const m = generateWAMessageFromContent(sender, message, {
|
const m = generateWAMessageFromContent(sender, message, {
|
||||||
@@ -1972,10 +2030,19 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (contextInfo) {
|
||||||
|
message['contextInfo'] = contextInfo;
|
||||||
|
}
|
||||||
|
|
||||||
if (message['conversation']) {
|
if (message['conversation']) {
|
||||||
return await this.client.sendMessage(
|
return await this.client.sendMessage(
|
||||||
sender,
|
sender,
|
||||||
{ text: message['conversation'], mentions, linkPreview: linkPreview } as unknown as AnyMessageContent,
|
{
|
||||||
|
text: message['conversation'],
|
||||||
|
mentions,
|
||||||
|
linkPreview: linkPreview,
|
||||||
|
contextInfo: message['contextInfo'],
|
||||||
|
} as unknown as AnyMessageContent,
|
||||||
option as unknown as MiscMessageGenerationOptions,
|
option as unknown as MiscMessageGenerationOptions,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1983,7 +2050,11 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
if (!message['audio'] && !message['poll'] && !message['sticker'] && sender != 'status@broadcast') {
|
if (!message['audio'] && !message['poll'] && !message['sticker'] && sender != 'status@broadcast') {
|
||||||
return await this.client.sendMessage(
|
return await this.client.sendMessage(
|
||||||
sender,
|
sender,
|
||||||
{ forward: { key: { remoteJid: this.instance.wuid, fromMe: true }, message }, mentions },
|
{
|
||||||
|
forward: { key: { remoteJid: this.instance.wuid, fromMe: true }, message },
|
||||||
|
mentions,
|
||||||
|
contextInfo: message['contextInfo'],
|
||||||
|
},
|
||||||
option as unknown as MiscMessageGenerationOptions,
|
option as unknown as MiscMessageGenerationOptions,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -2114,7 +2185,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
if (options?.quoted) {
|
if (options?.quoted) {
|
||||||
const m = options?.quoted;
|
const m = options?.quoted;
|
||||||
|
|
||||||
const msg = m?.message ? m : ((await this.getMessage(m.key, true)) as proto.IWebMessageInfo);
|
const msg = m?.message ? m : ((await this.getMessage(m.key, true)) as WAMessage);
|
||||||
|
|
||||||
if (msg) {
|
if (msg) {
|
||||||
quoted = msg;
|
quoted = msg;
|
||||||
@@ -2124,6 +2195,8 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
let messageSent: WAMessage;
|
let messageSent: WAMessage;
|
||||||
|
|
||||||
let mentions: string[];
|
let mentions: string[];
|
||||||
|
let contextInfo: any;
|
||||||
|
|
||||||
if (isJidGroup(sender)) {
|
if (isJidGroup(sender)) {
|
||||||
let group;
|
let group;
|
||||||
try {
|
try {
|
||||||
@@ -2131,7 +2204,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
if (!cache.REDIS.ENABLED && !cache.LOCAL.ENABLED) group = await this.findGroup({ groupJid: sender }, 'inner');
|
if (!cache.REDIS.ENABLED && !cache.LOCAL.ENABLED) group = await this.findGroup({ groupJid: sender }, 'inner');
|
||||||
else group = await this.getGroupMetadataCache(sender);
|
else group = await this.getGroupMetadataCache(sender);
|
||||||
// group = await this.findGroup({ groupJid: sender }, 'inner');
|
// group = await this.findGroup({ groupJid: sender }, 'inner');
|
||||||
} catch (error) {
|
} catch {
|
||||||
throw new NotFoundException('Group not found');
|
throw new NotFoundException('Group not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2162,7 +2235,27 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
// group?.participants,
|
// group?.participants,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
messageSent = await this.sendMessage(sender, message, mentions, linkPreview, quoted);
|
contextInfo = {
|
||||||
|
mentionedJid: [],
|
||||||
|
groupMentions: [],
|
||||||
|
//expiration: 7776000,
|
||||||
|
ephemeralSettingTimestamp: {
|
||||||
|
low: Math.floor(Date.now() / 1000) - 172800,
|
||||||
|
high: 0,
|
||||||
|
unsigned: false,
|
||||||
|
},
|
||||||
|
disappearingMode: { initiator: 0 },
|
||||||
|
};
|
||||||
|
messageSent = await this.sendMessage(
|
||||||
|
sender,
|
||||||
|
message,
|
||||||
|
mentions,
|
||||||
|
linkPreview,
|
||||||
|
quoted,
|
||||||
|
null,
|
||||||
|
undefined,
|
||||||
|
contextInfo,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Long.isLong(messageSent?.messageTimestamp)) {
|
if (Long.isLong(messageSent?.messageTimestamp)) {
|
||||||
@@ -2282,7 +2375,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.log(messageRaw);
|
this.logger.verbose(messageSent);
|
||||||
|
|
||||||
this.sendDataWebhook(Events.SEND_MESSAGE, messageRaw);
|
this.sendDataWebhook(Events.SEND_MESSAGE, messageRaw);
|
||||||
|
|
||||||
@@ -3289,125 +3382,128 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
where: { instanceId: this.instanceId, remoteJid: { in: jids.users.map(({ jid }) => jid) } },
|
where: { instanceId: this.instanceId, remoteJid: { in: jids.users.map(({ jid }) => jid) } },
|
||||||
});
|
});
|
||||||
|
|
||||||
// Separate @lid numbers from normal numbers
|
// Unified cache verification for all numbers (normal and LID)
|
||||||
const lidUsers = jids.users.filter(({ jid }) => jid.includes('@lid'));
|
const numbersToVerify = jids.users.map(({ jid }) => jid.replace('+', ''));
|
||||||
const normalUsers = jids.users.filter(({ jid }) => !jid.includes('@lid'));
|
|
||||||
|
|
||||||
// For normal numbers, use traditional Baileys verification
|
// Get all numbers from cache
|
||||||
let normalVerifiedUsers: OnWhatsAppDto[] = [];
|
const cachedNumbers = await getOnWhatsappCache(numbersToVerify);
|
||||||
if (normalUsers.length > 0) {
|
|
||||||
console.log('normalUsers', normalUsers);
|
|
||||||
const numbersToVerify = normalUsers.map(({ jid }) => jid.replace('+', ''));
|
|
||||||
console.log('numbersToVerify', numbersToVerify);
|
|
||||||
|
|
||||||
const cachedNumbers = await getOnWhatsappCache(numbersToVerify);
|
// Separate numbers that are and are not in cache
|
||||||
console.log('cachedNumbers', cachedNumbers);
|
const cachedJids = new Set(cachedNumbers.flatMap((cached) => cached.jidOptions));
|
||||||
|
const numbersNotInCache = numbersToVerify.filter((jid) => !cachedJids.has(jid));
|
||||||
|
|
||||||
const filteredNumbers = numbersToVerify.filter(
|
// Only call Baileys for normal numbers (@s.whatsapp.net) that are not in cache
|
||||||
(jid) => !cachedNumbers.some((cached) => cached.jidOptions.includes(jid)),
|
let verify: { jid: string; exists: boolean }[] = [];
|
||||||
);
|
const normalNumbersNotInCache = numbersNotInCache.filter((jid) => !jid.includes('@lid'));
|
||||||
console.log('filteredNumbers', filteredNumbers);
|
|
||||||
|
|
||||||
const verify = await this.client.onWhatsApp(...filteredNumbers);
|
if (normalNumbersNotInCache.length > 0) {
|
||||||
console.log('verify', verify);
|
this.logger.verbose(`Checking ${normalNumbersNotInCache.length} numbers via Baileys (not found in cache)`);
|
||||||
normalVerifiedUsers = await Promise.all(
|
verify = await this.client.onWhatsApp(...normalNumbersNotInCache);
|
||||||
normalUsers.map(async (user) => {
|
|
||||||
let numberVerified: (typeof verify)[0] | null = null;
|
|
||||||
|
|
||||||
const cached = cachedNumbers.find((cached) => cached.jidOptions.includes(user.jid.replace('+', '')));
|
|
||||||
if (cached) {
|
|
||||||
return new OnWhatsAppDto(
|
|
||||||
cached.remoteJid,
|
|
||||||
true,
|
|
||||||
user.number,
|
|
||||||
contacts.find((c) => c.remoteJid === cached.remoteJid)?.pushName,
|
|
||||||
cached.lid || (cached.remoteJid.includes('@lid') ? cached.remoteJid.split('@')[1] : undefined),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Brazilian numbers
|
|
||||||
if (user.number.startsWith('55')) {
|
|
||||||
const numberWithDigit =
|
|
||||||
user.number.slice(4, 5) === '9' && user.number.length === 13
|
|
||||||
? user.number
|
|
||||||
: `${user.number.slice(0, 4)}9${user.number.slice(4)}`;
|
|
||||||
const numberWithoutDigit =
|
|
||||||
user.number.length === 12 ? user.number : user.number.slice(0, 4) + user.number.slice(5);
|
|
||||||
|
|
||||||
numberVerified = verify.find(
|
|
||||||
(v) => v.jid === `${numberWithDigit}@s.whatsapp.net` || v.jid === `${numberWithoutDigit}@s.whatsapp.net`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mexican/Argentina numbers
|
|
||||||
// Ref: https://faq.whatsapp.com/1294841057948784
|
|
||||||
if (!numberVerified && (user.number.startsWith('52') || user.number.startsWith('54'))) {
|
|
||||||
let prefix = '';
|
|
||||||
if (user.number.startsWith('52')) {
|
|
||||||
prefix = '1';
|
|
||||||
}
|
|
||||||
if (user.number.startsWith('54')) {
|
|
||||||
prefix = '9';
|
|
||||||
}
|
|
||||||
|
|
||||||
const numberWithDigit =
|
|
||||||
user.number.slice(2, 3) === prefix && user.number.length === 13
|
|
||||||
? user.number
|
|
||||||
: `${user.number.slice(0, 2)}${prefix}${user.number.slice(2)}`;
|
|
||||||
const numberWithoutDigit =
|
|
||||||
user.number.length === 12 ? user.number : user.number.slice(0, 2) + user.number.slice(3);
|
|
||||||
|
|
||||||
numberVerified = verify.find(
|
|
||||||
(v) => v.jid === `${numberWithDigit}@s.whatsapp.net` || v.jid === `${numberWithoutDigit}@s.whatsapp.net`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!numberVerified) {
|
|
||||||
numberVerified = verify.find((v) => v.jid === user.jid);
|
|
||||||
}
|
|
||||||
|
|
||||||
const numberJid = numberVerified?.jid || user.jid;
|
|
||||||
const lid =
|
|
||||||
typeof numberVerified?.lid === 'string'
|
|
||||||
? numberVerified.lid
|
|
||||||
: numberJid.includes('@lid')
|
|
||||||
? numberJid.split('@')[1]
|
|
||||||
: undefined;
|
|
||||||
return new OnWhatsAppDto(
|
|
||||||
numberJid,
|
|
||||||
!!numberVerified?.exists,
|
|
||||||
user.number,
|
|
||||||
contacts.find((c) => c.remoteJid === numberJid)?.pushName,
|
|
||||||
lid,
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// For @lid numbers, always consider them as valid
|
const verifiedUsers = await Promise.all(
|
||||||
const lidVerifiedUsers: OnWhatsAppDto[] = lidUsers.map((user) => {
|
jids.users.map(async (user) => {
|
||||||
return new OnWhatsAppDto(
|
// Try to get from cache first (works for all: normal and LID)
|
||||||
user.jid,
|
const cached = cachedNumbers.find((cached) => cached.jidOptions.includes(user.jid.replace('+', '')));
|
||||||
true,
|
|
||||||
user.number,
|
if (cached) {
|
||||||
contacts.find((c) => c.remoteJid === user.jid)?.pushName,
|
this.logger.verbose(`Number ${user.number} found in cache`);
|
||||||
user.jid.split('@')[1],
|
return new OnWhatsAppDto(
|
||||||
);
|
cached.remoteJid,
|
||||||
});
|
true,
|
||||||
|
user.number,
|
||||||
|
contacts.find((c) => c.remoteJid === cached.remoteJid)?.pushName,
|
||||||
|
cached.lid || (cached.remoteJid.includes('@lid') ? 'lid' : undefined),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it's a LID number and not in cache, consider it valid
|
||||||
|
if (user.jid.includes('@lid')) {
|
||||||
|
return new OnWhatsAppDto(
|
||||||
|
user.jid,
|
||||||
|
true,
|
||||||
|
user.number,
|
||||||
|
contacts.find((c) => c.remoteJid === user.jid)?.pushName,
|
||||||
|
'lid',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If not in cache and is a normal number, use Baileys verification
|
||||||
|
let numberVerified: (typeof verify)[0] | null = null;
|
||||||
|
|
||||||
|
// Brazilian numbers
|
||||||
|
if (user.number.startsWith('55')) {
|
||||||
|
const numberWithDigit =
|
||||||
|
user.number.slice(4, 5) === '9' && user.number.length === 13
|
||||||
|
? user.number
|
||||||
|
: `${user.number.slice(0, 4)}9${user.number.slice(4)}`;
|
||||||
|
const numberWithoutDigit =
|
||||||
|
user.number.length === 12 ? user.number : user.number.slice(0, 4) + user.number.slice(5);
|
||||||
|
|
||||||
|
numberVerified = verify.find(
|
||||||
|
(v) => v.jid === `${numberWithDigit}@s.whatsapp.net` || v.jid === `${numberWithoutDigit}@s.whatsapp.net`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mexican/Argentina numbers
|
||||||
|
// Ref: https://faq.whatsapp.com/1294841057948784
|
||||||
|
if (!numberVerified && (user.number.startsWith('52') || user.number.startsWith('54'))) {
|
||||||
|
let prefix = '';
|
||||||
|
if (user.number.startsWith('52')) {
|
||||||
|
prefix = '1';
|
||||||
|
}
|
||||||
|
if (user.number.startsWith('54')) {
|
||||||
|
prefix = '9';
|
||||||
|
}
|
||||||
|
|
||||||
|
const numberWithDigit =
|
||||||
|
user.number.slice(2, 3) === prefix && user.number.length === 13
|
||||||
|
? user.number
|
||||||
|
: `${user.number.slice(0, 2)}${prefix}${user.number.slice(2)}`;
|
||||||
|
const numberWithoutDigit =
|
||||||
|
user.number.length === 12 ? user.number : user.number.slice(0, 2) + user.number.slice(3);
|
||||||
|
|
||||||
|
numberVerified = verify.find(
|
||||||
|
(v) => v.jid === `${numberWithDigit}@s.whatsapp.net` || v.jid === `${numberWithoutDigit}@s.whatsapp.net`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!numberVerified) {
|
||||||
|
numberVerified = verify.find((v) => v.jid === user.jid);
|
||||||
|
}
|
||||||
|
|
||||||
|
const numberJid = numberVerified?.jid || user.jid;
|
||||||
|
|
||||||
|
return new OnWhatsAppDto(
|
||||||
|
numberJid,
|
||||||
|
!!numberVerified?.exists,
|
||||||
|
user.number,
|
||||||
|
contacts.find((c) => c.remoteJid === numberJid)?.pushName,
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
// Combine results
|
// Combine results
|
||||||
onWhatsapp.push(...normalVerifiedUsers, ...lidVerifiedUsers);
|
onWhatsapp.push(...verifiedUsers);
|
||||||
|
|
||||||
// Save to cache only valid numbers
|
// TODO: Salvar no cache apenas números que NÃO estavam no cache
|
||||||
await saveOnWhatsappCache(
|
const numbersToCache = onWhatsapp.filter((user) => {
|
||||||
onWhatsapp
|
if (!user.exists) return false;
|
||||||
.filter((user) => user.exists)
|
// Verifica se estava no cache usando jidOptions
|
||||||
.map((user) => ({
|
const cached = cachedNumbers?.find((cached) => cached.jidOptions.includes(user.jid.replace('+', '')));
|
||||||
|
return !cached;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (numbersToCache.length > 0) {
|
||||||
|
this.logger.verbose(`Salvando ${numbersToCache.length} números no cache`);
|
||||||
|
await saveOnWhatsappCache(
|
||||||
|
numbersToCache.map((user) => ({
|
||||||
remoteJid: user.jid,
|
remoteJid: user.jid,
|
||||||
jidOptions: user.jid.replace('+', ''),
|
lid: user.lid === 'lid' ? 'lid' : undefined,
|
||||||
lid: user.lid,
|
|
||||||
})),
|
})),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return onWhatsapp;
|
return onWhatsapp;
|
||||||
}
|
}
|
||||||
@@ -3530,7 +3626,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
keyId: messageId,
|
keyId: messageId,
|
||||||
remoteJid: response.key.remoteJid,
|
remoteJid: response.key.remoteJid,
|
||||||
fromMe: response.key.fromMe,
|
fromMe: response.key.fromMe,
|
||||||
participant: response.key?.remoteJid,
|
participant: response.key?.participant,
|
||||||
status: 'DELETED',
|
status: 'DELETED',
|
||||||
instanceId: this.instanceId,
|
instanceId: this.instanceId,
|
||||||
};
|
};
|
||||||
@@ -3640,7 +3736,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
{},
|
{},
|
||||||
{ logger: P({ level: 'error' }) as any, reuploadRequest: this.client.updateMediaMessage },
|
{ logger: P({ level: 'error' }) as any, reuploadRequest: this.client.updateMediaMessage },
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch {
|
||||||
this.logger.error('Download Media failed, trying to retry in 5 seconds...');
|
this.logger.error('Download Media failed, trying to retry in 5 seconds...');
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5000));
|
await new Promise((resolve) => setTimeout(resolve, 5000));
|
||||||
const mediaType = Object.keys(msg.message).find((key) => key.endsWith('Message'));
|
const mediaType = Object.keys(msg.message).find((key) => key.endsWith('Message'));
|
||||||
@@ -3965,7 +4061,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
keyId: messageId,
|
keyId: messageId,
|
||||||
remoteJid: messageSent.key.remoteJid,
|
remoteJid: messageSent.key.remoteJid,
|
||||||
fromMe: messageSent.key.fromMe,
|
fromMe: messageSent.key.fromMe,
|
||||||
participant: messageSent.key?.remoteJid,
|
participant: messageSent.key?.participant,
|
||||||
status: 'EDITED',
|
status: 'EDITED',
|
||||||
instanceId: this.instanceId,
|
instanceId: this.instanceId,
|
||||||
};
|
};
|
||||||
@@ -4230,7 +4326,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
public async inviteInfo(id: GroupInvite) {
|
public async inviteInfo(id: GroupInvite) {
|
||||||
try {
|
try {
|
||||||
return await this.client.groupGetInviteInfo(id.inviteCode);
|
return await this.client.groupGetInviteInfo(id.inviteCode);
|
||||||
} catch (error) {
|
} catch {
|
||||||
throw new NotFoundException('No invite info', id.inviteCode);
|
throw new NotFoundException('No invite info', id.inviteCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4253,7 +4349,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return { send: true, inviteUrl };
|
return { send: true, inviteUrl };
|
||||||
} catch (error) {
|
} catch {
|
||||||
throw new NotFoundException('No send invite');
|
throw new NotFoundException('No send invite');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4348,24 +4444,37 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
throw new Error('Method not available in the Baileys service');
|
throw new Error('Method not available in the Baileys service');
|
||||||
}
|
}
|
||||||
|
|
||||||
private convertLongToNumber(obj: any): any {
|
private deserializeMessageBuffers(obj: any): any {
|
||||||
if (obj === null || obj === undefined) {
|
if (obj === null || obj === undefined) {
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Long.isLong(obj)) {
|
if (typeof obj === 'object' && !Array.isArray(obj) && !Buffer.isBuffer(obj)) {
|
||||||
return obj.toNumber();
|
const keys = Object.keys(obj);
|
||||||
|
const isIndexedObject = keys.every((key) => !isNaN(Number(key)));
|
||||||
|
|
||||||
|
if (isIndexedObject && keys.length > 0) {
|
||||||
|
const values = keys.sort((a, b) => Number(a) - Number(b)).map((key) => obj[key]);
|
||||||
|
return new Uint8Array(values);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Is Buffer?, converter to Uint8Array
|
||||||
|
if (Buffer.isBuffer(obj)) {
|
||||||
|
return new Uint8Array(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process arrays recursively
|
||||||
if (Array.isArray(obj)) {
|
if (Array.isArray(obj)) {
|
||||||
return obj.map((item) => this.convertLongToNumber(item));
|
return obj.map((item) => this.deserializeMessageBuffers(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Process objects recursively
|
||||||
if (typeof obj === 'object') {
|
if (typeof obj === 'object') {
|
||||||
const converted: any = {};
|
const converted: any = {};
|
||||||
for (const key in obj) {
|
for (const key in obj) {
|
||||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||||
converted[key] = this.convertLongToNumber(obj[key]);
|
converted[key] = this.deserializeMessageBuffers(obj[key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return converted;
|
return converted;
|
||||||
@@ -4386,8 +4495,8 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
? 'Você'
|
? 'Você'
|
||||||
: message?.participant || (message.key?.participant ? message.key.participant.split('@')[0] : null)),
|
: message?.participant || (message.key?.participant ? message.key.participant.split('@')[0] : null)),
|
||||||
status: status[message.status],
|
status: status[message.status],
|
||||||
message: this.convertLongToNumber({ ...message.message }),
|
message: this.deserializeMessageBuffers({ ...message.message }),
|
||||||
contextInfo: this.convertLongToNumber(contentMsg?.contextInfo),
|
contextInfo: this.deserializeMessageBuffers(contentMsg?.contextInfo),
|
||||||
messageType: contentType || 'unknown',
|
messageType: contentType || 'unknown',
|
||||||
messageTimestamp: Long.isLong(message.messageTimestamp)
|
messageTimestamp: Long.isLong(message.messageTimestamp)
|
||||||
? message.messageTimestamp.toNumber()
|
? message.messageTimestamp.toNumber()
|
||||||
@@ -4461,7 +4570,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
// Use raw SQL to avoid JSON path issues
|
// Use raw SQL to avoid JSON path issues
|
||||||
const result = await this.prismaRepository.$executeRaw`
|
const result = await this.prismaRepository.$executeRaw`
|
||||||
UPDATE "Message"
|
UPDATE "Message"
|
||||||
SET "status" = ${status[4]}
|
SET "status" = ${status[4]}
|
||||||
WHERE "instanceId" = ${this.instanceId}
|
WHERE "instanceId" = ${this.instanceId}
|
||||||
AND "key"->>'remoteJid' = ${remoteJid}
|
AND "key"->>'remoteJid' = ${remoteJid}
|
||||||
@@ -4486,7 +4595,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
this.prismaRepository.chat.findFirst({ where: { remoteJid } }),
|
this.prismaRepository.chat.findFirst({ where: { remoteJid } }),
|
||||||
// Use raw SQL to avoid JSON path issues
|
// Use raw SQL to avoid JSON path issues
|
||||||
this.prismaRepository.$queryRaw`
|
this.prismaRepository.$queryRaw`
|
||||||
SELECT COUNT(*)::int as count FROM "Message"
|
SELECT COUNT(*)::int as count FROM "Message"
|
||||||
WHERE "instanceId" = ${this.instanceId}
|
WHERE "instanceId" = ${this.instanceId}
|
||||||
AND "key"->>'remoteJid' = ${remoteJid}
|
AND "key"->>'remoteJid' = ${remoteJid}
|
||||||
AND ("key"->>'fromMe')::boolean = false
|
AND ("key"->>'fromMe')::boolean = false
|
||||||
@@ -4561,8 +4670,8 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async baileysAssertSessions(jids: string[], force: boolean) {
|
public async baileysAssertSessions(jids: string[]) {
|
||||||
const response = await this.client.assertSessions(jids, force);
|
const response = await this.client.assertSessions(jids);
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
@@ -4717,7 +4826,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
collectionsLength: collections?.length,
|
collectionsLength: collections?.length,
|
||||||
collections: collections,
|
collections: collections,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch {
|
||||||
return { wuid: jid, name: null, isBusiness: false };
|
return { wuid: jid, name: null, isBusiness: false };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4766,7 +4875,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
{
|
{
|
||||||
OR: [
|
OR: [
|
||||||
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
|
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
|
||||||
keyFilters?.senderPn ? { key: { path: ['senderPn'], equals: keyFilters?.senderPn } } : {},
|
keyFilters?.remoteJidAlt ? { key: { path: ['remoteJidAlt'], equals: keyFilters?.remoteJidAlt } } : {},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@@ -4796,7 +4905,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
{
|
{
|
||||||
OR: [
|
OR: [
|
||||||
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
|
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
|
||||||
keyFilters?.senderPn ? { key: { path: ['senderPn'], equals: keyFilters?.senderPn } } : {},
|
keyFilters?.remoteJidAlt ? { key: { path: ['remoteJidAlt'], equals: keyFilters?.remoteJidAlt } } : {},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
|
|||||||
try {
|
try {
|
||||||
JSON.parse(str);
|
JSON.parse(str);
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -180,6 +180,7 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
|
|||||||
remoteJid: string,
|
remoteJid: string,
|
||||||
message: string,
|
message: string,
|
||||||
settings: SettingsType,
|
settings: SettingsType,
|
||||||
|
linkPreview: boolean = true,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (!message) return;
|
if (!message) return;
|
||||||
|
|
||||||
@@ -202,7 +203,7 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
|
|||||||
if (mediaType) {
|
if (mediaType) {
|
||||||
// Send accumulated text before sending media
|
// Send accumulated text before sending media
|
||||||
if (textBuffer.trim()) {
|
if (textBuffer.trim()) {
|
||||||
await this.sendFormattedText(instance, remoteJid, textBuffer.trim(), settings, splitMessages);
|
await this.sendFormattedText(instance, remoteJid, textBuffer.trim(), settings, splitMessages, linkPreview);
|
||||||
textBuffer = '';
|
textBuffer = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,7 +253,56 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
|
|||||||
|
|
||||||
// Send any remaining text
|
// Send any remaining text
|
||||||
if (textBuffer.trim()) {
|
if (textBuffer.trim()) {
|
||||||
await this.sendFormattedText(instance, remoteJid, textBuffer.trim(), settings, splitMessages);
|
await this.sendFormattedText(instance, remoteJid, textBuffer.trim(), settings, splitMessages, linkPreview);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Split message by double line breaks and return array of message parts
|
||||||
|
*/
|
||||||
|
private splitMessageByDoubleLineBreaks(message: string): string[] {
|
||||||
|
return message.split('\n\n').filter((part) => part.trim().length > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a single message with proper typing indicators and delays
|
||||||
|
*/
|
||||||
|
private async sendSingleMessage(
|
||||||
|
instance: any,
|
||||||
|
remoteJid: string,
|
||||||
|
message: string,
|
||||||
|
settings: any,
|
||||||
|
linkPreview: boolean = true,
|
||||||
|
): Promise<void> {
|
||||||
|
const timePerChar = settings?.timePerChar ?? 0;
|
||||||
|
const minDelay = 1000;
|
||||||
|
const maxDelay = 20000;
|
||||||
|
const delay = Math.min(Math.max(message.length * timePerChar, minDelay), maxDelay);
|
||||||
|
|
||||||
|
this.logger.debug(`[BaseChatbot] Sending single message with linkPreview: ${linkPreview}`);
|
||||||
|
|
||||||
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
|
await instance.client.presenceSubscribe(remoteJid);
|
||||||
|
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
||||||
|
}
|
||||||
|
|
||||||
|
await new Promise<void>((resolve) => {
|
||||||
|
setTimeout(async () => {
|
||||||
|
await instance.textMessage(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
text: message,
|
||||||
|
linkPreview,
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
resolve();
|
||||||
|
}, delay);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -265,67 +315,24 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
|
|||||||
text: string,
|
text: string,
|
||||||
settings: any,
|
settings: any,
|
||||||
splitMessages: boolean,
|
splitMessages: boolean,
|
||||||
|
linkPreview: boolean = true,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const timePerChar = settings?.timePerChar ?? 0;
|
|
||||||
const minDelay = 1000;
|
|
||||||
const maxDelay = 20000;
|
|
||||||
|
|
||||||
if (splitMessages) {
|
if (splitMessages) {
|
||||||
const multipleMessages = text.split('\n\n');
|
const messageParts = this.splitMessageByDoubleLineBreaks(text);
|
||||||
for (let index = 0; index < multipleMessages.length; index++) {
|
|
||||||
const message = multipleMessages[index];
|
|
||||||
if (!message.trim()) continue;
|
|
||||||
|
|
||||||
const delay = Math.min(Math.max(message.length * timePerChar, minDelay), maxDelay);
|
this.logger.debug(`[BaseChatbot] Splitting message into ${messageParts.length} parts`);
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
for (let index = 0; index < messageParts.length; index++) {
|
||||||
await instance.client.presenceSubscribe(remoteJid);
|
const message = messageParts[index];
|
||||||
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
|
||||||
}
|
|
||||||
|
|
||||||
await new Promise<void>((resolve) => {
|
this.logger.debug(`[BaseChatbot] Sending message part ${index + 1}/${messageParts.length}`);
|
||||||
setTimeout(async () => {
|
await this.sendSingleMessage(instance, remoteJid, message, settings, linkPreview);
|
||||||
await instance.textMessage(
|
|
||||||
{
|
|
||||||
number: remoteJid.split('@')[0],
|
|
||||||
delay: settings?.delayMessage || 1000,
|
|
||||||
text: message,
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
resolve();
|
|
||||||
}, delay);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.debug(`[BaseChatbot] All message parts sent successfully`);
|
||||||
} else {
|
} else {
|
||||||
const delay = Math.min(Math.max(text.length * timePerChar, minDelay), maxDelay);
|
this.logger.debug(`[BaseChatbot] Sending single message`);
|
||||||
|
await this.sendSingleMessage(instance, remoteJid, text, settings, linkPreview);
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
|
||||||
await instance.client.presenceSubscribe(remoteJid);
|
|
||||||
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
|
||||||
}
|
|
||||||
|
|
||||||
await new Promise<void>((resolve) => {
|
|
||||||
setTimeout(async () => {
|
|
||||||
await instance.textMessage(
|
|
||||||
{
|
|
||||||
number: remoteJid.split('@')[0],
|
|
||||||
delay: settings?.delayMessage || 1000,
|
|
||||||
text: text,
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
resolve();
|
|
||||||
}, delay);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -91,19 +91,19 @@ export class ChatbotController {
|
|||||||
pushName,
|
pushName,
|
||||||
isIntegration,
|
isIntegration,
|
||||||
};
|
};
|
||||||
await evolutionBotController.emit(emitData);
|
evolutionBotController.emit(emitData);
|
||||||
|
|
||||||
await typebotController.emit(emitData);
|
typebotController.emit(emitData);
|
||||||
|
|
||||||
await openaiController.emit(emitData);
|
openaiController.emit(emitData);
|
||||||
|
|
||||||
await difyController.emit(emitData);
|
difyController.emit(emitData);
|
||||||
|
|
||||||
await n8nController.emit(emitData);
|
n8nController.emit(emitData);
|
||||||
|
|
||||||
await evoaiController.emit(emitData);
|
evoaiController.emit(emitData);
|
||||||
|
|
||||||
await flowiseController.emit(emitData);
|
flowiseController.emit(emitData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public processDebounce(
|
public processDebounce(
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { InstanceDto } from '@api/dto/instance.dto';
|
import { InstanceDto } from '@api/dto/instance.dto';
|
||||||
import { Options, Quoted, SendAudioDto, SendMediaDto, SendTextDto } from '@api/dto/sendMessage.dto';
|
import { Options, Quoted, SendAudioDto, SendMediaDto, SendTextDto } from '@api/dto/sendMessage.dto';
|
||||||
import { ExtendedMessageKey } from '@api/integrations/channel/whatsapp/whatsapp.baileys.service';
|
|
||||||
import { ChatwootDto } from '@api/integrations/chatbot/chatwoot/dto/chatwoot.dto';
|
import { ChatwootDto } from '@api/integrations/chatbot/chatwoot/dto/chatwoot.dto';
|
||||||
import { postgresClient } from '@api/integrations/chatbot/chatwoot/libs/postgres.client';
|
import { postgresClient } from '@api/integrations/chatbot/chatwoot/libs/postgres.client';
|
||||||
import { chatwootImport } from '@api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper';
|
import { chatwootImport } from '@api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper';
|
||||||
@@ -24,7 +23,7 @@ import { Chatwoot as ChatwootModel, Contact as ContactModel, Message as MessageM
|
|||||||
import i18next from '@utils/i18n';
|
import i18next from '@utils/i18n';
|
||||||
import { sendTelemetry } from '@utils/sendTelemetry';
|
import { sendTelemetry } from '@utils/sendTelemetry';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { proto } from 'baileys';
|
import { WAMessageContent, WAMessageKey } from 'baileys';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import FormData from 'form-data';
|
import FormData from 'form-data';
|
||||||
import { Jimp, JimpMime } from 'jimp';
|
import { Jimp, JimpMime } from 'jimp';
|
||||||
@@ -44,6 +43,9 @@ interface ChatwootMessage {
|
|||||||
export class ChatwootService {
|
export class ChatwootService {
|
||||||
private readonly logger = new Logger('ChatwootService');
|
private readonly logger = new Logger('ChatwootService');
|
||||||
|
|
||||||
|
// Lock polling delay
|
||||||
|
private readonly LOCK_POLLING_DELAY_MS = 300; // Delay between lock status checks
|
||||||
|
|
||||||
private provider: any;
|
private provider: any;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@@ -130,7 +132,7 @@ export class ChatwootService {
|
|||||||
public async find(instance: InstanceDto): Promise<ChatwootDto> {
|
public async find(instance: InstanceDto): Promise<ChatwootDto> {
|
||||||
try {
|
try {
|
||||||
return await this.waMonitor.waInstances[instance.instanceName].findChatwoot();
|
return await this.waMonitor.waInstances[instance.instanceName].findChatwoot();
|
||||||
} catch (error) {
|
} catch {
|
||||||
this.logger.error('chatwoot not found');
|
this.logger.error('chatwoot not found');
|
||||||
return { enabled: null, url: '' };
|
return { enabled: null, url: '' };
|
||||||
}
|
}
|
||||||
@@ -370,7 +372,7 @@ export class ChatwootService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return contact;
|
return contact;
|
||||||
} catch (error) {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -407,7 +409,7 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -568,27 +570,31 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async createConversation(instance: InstanceDto, body: any) {
|
public async createConversation(instance: InstanceDto, body: any) {
|
||||||
const isLid = body.key.previousRemoteJid?.includes('@lid') && body.key.senderPn;
|
const isLid = body.key.addressingMode === 'lid';
|
||||||
const remoteJid = body.key.remoteJid;
|
const isGroup = body.key.remoteJid.endsWith('@g.us');
|
||||||
|
const phoneNumber = isLid && !isGroup ? body.key.remoteJidAlt : body.key.remoteJid;
|
||||||
|
const { remoteJid } = body.key;
|
||||||
const cacheKey = `${instance.instanceName}:createConversation-${remoteJid}`;
|
const cacheKey = `${instance.instanceName}:createConversation-${remoteJid}`;
|
||||||
const lockKey = `${instance.instanceName}:lock:createConversation-${remoteJid}`;
|
const lockKey = `${instance.instanceName}:lock:createConversation-${remoteJid}`;
|
||||||
const maxWaitTime = 5000; // 5 secounds
|
const maxWaitTime = 5000; // 5 seconds
|
||||||
|
const client = await this.clientCw(instance);
|
||||||
|
if (!client) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Processa atualização de contatos já criados @lid
|
// Processa atualização de contatos já criados @lid
|
||||||
if (isLid && body.key.senderPn !== body.key.previousRemoteJid) {
|
if (phoneNumber && remoteJid && !isGroup) {
|
||||||
const contact = await this.findContact(instance, body.key.remoteJid.split('@')[0]);
|
const contact = await this.findContact(instance, phoneNumber.split('@')[0]);
|
||||||
if (contact && contact.identifier !== body.key.senderPn) {
|
if (contact && contact.identifier !== remoteJid) {
|
||||||
this.logger.verbose(
|
this.logger.verbose(
|
||||||
`Identifier needs update: (contact.identifier: ${contact.identifier}, body.key.remoteJid: ${body.key.remoteJid}, body.key.senderPn: ${body.key.senderPn}`,
|
`Identifier needs update: (contact.identifier: ${contact.identifier}, phoneNumber: ${phoneNumber}, body.key.remoteJidAlt: ${remoteJid}`,
|
||||||
);
|
);
|
||||||
const updateContact = await this.updateContact(instance, contact.id, {
|
const updateContact = await this.updateContact(instance, contact.id, {
|
||||||
identifier: body.key.senderPn,
|
identifier: remoteJid,
|
||||||
phone_number: `+${body.key.senderPn.split('@')[0]}`,
|
phone_number: `+${phoneNumber.split('@')[0]}`,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (updateContact === null) {
|
if (updateContact === null) {
|
||||||
const baseContact = await this.findContact(instance, body.key.senderPn.split('@')[0]);
|
const baseContact = await this.findContact(instance, phoneNumber.split('@')[0]);
|
||||||
if (baseContact) {
|
if (baseContact) {
|
||||||
await this.mergeContacts(baseContact.id, contact.id);
|
await this.mergeContacts(baseContact.id, contact.id);
|
||||||
this.logger.verbose(
|
this.logger.verbose(
|
||||||
@@ -604,7 +610,23 @@ export class ChatwootService {
|
|||||||
// If it already exists in the cache, return conversationId
|
// If it already exists in the cache, return conversationId
|
||||||
if (await this.cache.has(cacheKey)) {
|
if (await this.cache.has(cacheKey)) {
|
||||||
const conversationId = (await this.cache.get(cacheKey)) as number;
|
const conversationId = (await this.cache.get(cacheKey)) as number;
|
||||||
this.logger.verbose(`Found conversation to: ${remoteJid}, conversation ID: ${conversationId}`);
|
this.logger.verbose(`Found conversation to: ${phoneNumber}, conversation ID: ${conversationId}`);
|
||||||
|
let conversationExists: conversation | boolean;
|
||||||
|
try {
|
||||||
|
conversationExists = await client.conversations.get({
|
||||||
|
accountId: this.provider.accountId,
|
||||||
|
conversationId: conversationId,
|
||||||
|
});
|
||||||
|
this.logger.verbose(`Conversation exists: ${JSON.stringify(conversationExists)}`);
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(`Error getting conversation: ${error}`);
|
||||||
|
conversationExists = false;
|
||||||
|
}
|
||||||
|
if (!conversationExists) {
|
||||||
|
this.logger.verbose('Conversation does not exist, re-calling createConversation');
|
||||||
|
this.cache.delete(cacheKey);
|
||||||
|
return await this.createConversation(instance, body);
|
||||||
|
}
|
||||||
return conversationId;
|
return conversationId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -617,7 +639,7 @@ export class ChatwootService {
|
|||||||
this.logger.warn(`Timeout aguardando lock para ${remoteJid}`);
|
this.logger.warn(`Timeout aguardando lock para ${remoteJid}`);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
await new Promise((res) => setTimeout(res, 300));
|
await new Promise((res) => setTimeout(res, this.LOCK_POLLING_DELAY_MS));
|
||||||
if (await this.cache.has(cacheKey)) {
|
if (await this.cache.has(cacheKey)) {
|
||||||
const conversationId = (await this.cache.get(cacheKey)) as number;
|
const conversationId = (await this.cache.get(cacheKey)) as number;
|
||||||
this.logger.verbose(`Resolves creation of: ${remoteJid}, conversation ID: ${conversationId}`);
|
this.logger.verbose(`Resolves creation of: ${remoteJid}, conversation ID: ${conversationId}`);
|
||||||
@@ -639,11 +661,7 @@ export class ChatwootService {
|
|||||||
return (await this.cache.get(cacheKey)) as number;
|
return (await this.cache.get(cacheKey)) as number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = await this.clientCw(instance);
|
const chatId = isGroup ? remoteJid : phoneNumber.split('@')[0].split(':')[0];
|
||||||
if (!client) return null;
|
|
||||||
|
|
||||||
const isGroup = remoteJid.includes('@g.us');
|
|
||||||
const chatId = isGroup ? remoteJid : remoteJid.split('@')[0];
|
|
||||||
let nameContact = !body.key.fromMe ? body.pushName : chatId;
|
let nameContact = !body.key.fromMe ? body.pushName : chatId;
|
||||||
const filterInbox = await this.getInbox(instance);
|
const filterInbox = await this.getInbox(instance);
|
||||||
if (!filterInbox) return null;
|
if (!filterInbox) return null;
|
||||||
@@ -653,14 +671,15 @@ export class ChatwootService {
|
|||||||
const group = await this.waMonitor.waInstances[instance.instanceName].client.groupMetadata(chatId);
|
const group = await this.waMonitor.waInstances[instance.instanceName].client.groupMetadata(chatId);
|
||||||
this.logger.verbose(`Group metadata: ${JSON.stringify(group)}`);
|
this.logger.verbose(`Group metadata: ${JSON.stringify(group)}`);
|
||||||
|
|
||||||
|
const participantJid = isLid && !body.key.fromMe ? body.key.participantAlt : body.key.participant;
|
||||||
nameContact = `${group.subject} (GROUP)`;
|
nameContact = `${group.subject} (GROUP)`;
|
||||||
|
|
||||||
const picture_url = await this.waMonitor.waInstances[instance.instanceName].profilePicture(
|
const picture_url = await this.waMonitor.waInstances[instance.instanceName].profilePicture(
|
||||||
body.key.participant.split('@')[0],
|
participantJid.split('@')[0],
|
||||||
);
|
);
|
||||||
this.logger.verbose(`Participant profile picture URL: ${JSON.stringify(picture_url)}`);
|
this.logger.verbose(`Participant profile picture URL: ${JSON.stringify(picture_url)}`);
|
||||||
|
|
||||||
const findParticipant = await this.findContact(instance, body.key.participant.split('@')[0]);
|
const findParticipant = await this.findContact(instance, participantJid.split('@')[0]);
|
||||||
this.logger.verbose(`Found participant: ${JSON.stringify(findParticipant)}`);
|
this.logger.verbose(`Found participant: ${JSON.stringify(findParticipant)}`);
|
||||||
|
|
||||||
if (findParticipant) {
|
if (findParticipant) {
|
||||||
@@ -673,12 +692,12 @@ export class ChatwootService {
|
|||||||
} else {
|
} else {
|
||||||
await this.createContact(
|
await this.createContact(
|
||||||
instance,
|
instance,
|
||||||
body.key.participant.split('@')[0],
|
participantJid.split('@')[0],
|
||||||
filterInbox.id,
|
filterInbox.id,
|
||||||
false,
|
false,
|
||||||
body.pushName,
|
body.pushName,
|
||||||
picture_url.profilePictureUrl || null,
|
picture_url.profilePictureUrl || null,
|
||||||
body.key.participant,
|
participantJid,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -686,6 +705,7 @@ export class ChatwootService {
|
|||||||
const picture_url = await this.waMonitor.waInstances[instance.instanceName].profilePicture(chatId);
|
const picture_url = await this.waMonitor.waInstances[instance.instanceName].profilePicture(chatId);
|
||||||
this.logger.verbose(`Contact profile picture URL: ${JSON.stringify(picture_url)}`);
|
this.logger.verbose(`Contact profile picture URL: ${JSON.stringify(picture_url)}`);
|
||||||
|
|
||||||
|
this.logger.verbose(`Searching contact for: ${chatId}`);
|
||||||
let contact = await this.findContact(instance, chatId);
|
let contact = await this.findContact(instance, chatId);
|
||||||
|
|
||||||
if (contact) {
|
if (contact) {
|
||||||
@@ -769,7 +789,7 @@ export class ChatwootService {
|
|||||||
|
|
||||||
if (inboxConversation) {
|
if (inboxConversation) {
|
||||||
this.logger.verbose(`Returning existing conversation ID: ${inboxConversation.id}`);
|
this.logger.verbose(`Returning existing conversation ID: ${inboxConversation.id}`);
|
||||||
this.cache.set(cacheKey, inboxConversation.id);
|
this.cache.set(cacheKey, inboxConversation.id, 8 * 3600);
|
||||||
return inboxConversation.id;
|
return inboxConversation.id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -802,7 +822,7 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose(`New conversation created of ${remoteJid} with ID: ${conversation.id}`);
|
this.logger.verbose(`New conversation created of ${remoteJid} with ID: ${conversation.id}`);
|
||||||
this.cache.set(cacheKey, conversation.id);
|
this.cache.set(cacheKey, conversation.id, 8 * 3600);
|
||||||
return conversation.id;
|
return conversation.id;
|
||||||
} finally {
|
} finally {
|
||||||
await this.cache.delete(lockKey);
|
await this.cache.delete(lockKey);
|
||||||
@@ -1158,7 +1178,7 @@ export class ChatwootService {
|
|||||||
const data: SendAudioDto = {
|
const data: SendAudioDto = {
|
||||||
number: number,
|
number: number,
|
||||||
audio: media,
|
audio: media,
|
||||||
delay: 1200,
|
delay: Math.floor(Math.random() * (2000 - 500 + 1)) + 500,
|
||||||
quoted: options?.quoted,
|
quoted: options?.quoted,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1169,7 +1189,7 @@ export class ChatwootService {
|
|||||||
return messageSent;
|
return messageSent;
|
||||||
}
|
}
|
||||||
|
|
||||||
const documentExtensions = ['.gif', '.svg', '.tiff', '.tif'];
|
const documentExtensions = ['.gif', '.svg', '.tiff', '.tif', '.dxf', '.dwg'];
|
||||||
if (type === 'image' && parsedMedia && documentExtensions.includes(parsedMedia?.ext)) {
|
if (type === 'image' && parsedMedia && documentExtensions.includes(parsedMedia?.ext)) {
|
||||||
type = 'document';
|
type = 'document';
|
||||||
}
|
}
|
||||||
@@ -1194,6 +1214,7 @@ export class ChatwootService {
|
|||||||
return messageSent;
|
return messageSent;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
|
throw error; // Re-throw para que o erro seja tratado pelo caller
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1275,6 +1296,7 @@ export class ChatwootService {
|
|||||||
|
|
||||||
const senderName = body?.conversation?.messages[0]?.sender?.available_name || body?.sender?.name;
|
const senderName = body?.conversation?.messages[0]?.sender?.available_name || body?.sender?.name;
|
||||||
const waInstance = this.waMonitor.waInstances[instance.instanceName];
|
const waInstance = this.waMonitor.waInstances[instance.instanceName];
|
||||||
|
instance.instanceId = waInstance.instanceId;
|
||||||
|
|
||||||
if (body.event === 'message_updated' && body.content_attributes?.deleted) {
|
if (body.event === 'message_updated' && body.content_attributes?.deleted) {
|
||||||
const message = await this.prismaRepository.message.findFirst({
|
const message = await this.prismaRepository.message.findFirst({
|
||||||
@@ -1285,7 +1307,7 @@ export class ChatwootService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (message) {
|
if (message) {
|
||||||
const key = message.key as ExtendedMessageKey;
|
const key = message.key as WAMessageKey;
|
||||||
|
|
||||||
await waInstance?.client.sendMessage(key.remoteJid, { delete: key });
|
await waInstance?.client.sendMessage(key.remoteJid, { delete: key });
|
||||||
|
|
||||||
@@ -1370,7 +1392,10 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (body.message_type === 'outgoing' && body?.conversation?.messages?.length && chatId !== '123456') {
|
if (body.message_type === 'outgoing' && body?.conversation?.messages?.length && chatId !== '123456') {
|
||||||
if (body?.conversation?.messages[0]?.source_id?.substring(0, 5) === 'WAID:') {
|
if (
|
||||||
|
body?.conversation?.messages[0]?.source_id?.substring(0, 5) === 'WAID:' &&
|
||||||
|
body?.conversation?.messages[0]?.id === body?.id
|
||||||
|
) {
|
||||||
return { message: 'bot' };
|
return { message: 'bot' };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1417,7 +1442,6 @@ export class ChatwootService {
|
|||||||
await this.updateChatwootMessageId(
|
await this.updateChatwootMessageId(
|
||||||
{
|
{
|
||||||
...messageSent,
|
...messageSent,
|
||||||
owner: instance.instanceName,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
messageId: body.id,
|
messageId: body.id,
|
||||||
@@ -1432,7 +1456,7 @@ export class ChatwootService {
|
|||||||
const data: SendTextDto = {
|
const data: SendTextDto = {
|
||||||
number: chatId,
|
number: chatId,
|
||||||
text: formatText,
|
text: formatText,
|
||||||
delay: 1200,
|
delay: Math.floor(Math.random() * (2000 - 500 + 1)) + 500,
|
||||||
quoted: await this.getQuotedMessage(body, instance),
|
quoted: await this.getQuotedMessage(body, instance),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1452,7 +1476,6 @@ export class ChatwootService {
|
|||||||
await this.updateChatwootMessageId(
|
await this.updateChatwootMessageId(
|
||||||
{
|
{
|
||||||
...messageSent,
|
...messageSent,
|
||||||
instanceId: instance.instanceId,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
messageId: body.id,
|
messageId: body.id,
|
||||||
@@ -1483,7 +1506,7 @@ export class ChatwootService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (lastMessage && !lastMessage.chatwootIsRead) {
|
if (lastMessage && !lastMessage.chatwootIsRead) {
|
||||||
const key = lastMessage.key as ExtendedMessageKey;
|
const key = lastMessage.key as WAMessageKey;
|
||||||
|
|
||||||
waInstance?.markMessageAsRead({
|
waInstance?.markMessageAsRead({
|
||||||
readMessages: [
|
readMessages: [
|
||||||
@@ -1520,7 +1543,7 @@ export class ChatwootService {
|
|||||||
const data: SendTextDto = {
|
const data: SendTextDto = {
|
||||||
number: chatId,
|
number: chatId,
|
||||||
text: body.content.replace(/\\\r\n|\\\n|\n/g, '\n'),
|
text: body.content.replace(/\\\r\n|\\\n|\n/g, '\n'),
|
||||||
delay: 1200,
|
delay: Math.floor(Math.random() * (2000 - 500 + 1)) + 500,
|
||||||
};
|
};
|
||||||
|
|
||||||
sendTelemetry('/message/sendText');
|
sendTelemetry('/message/sendText');
|
||||||
@@ -1541,14 +1564,14 @@ export class ChatwootService {
|
|||||||
chatwootMessageIds: ChatwootMessage,
|
chatwootMessageIds: ChatwootMessage,
|
||||||
instance: InstanceDto,
|
instance: InstanceDto,
|
||||||
) {
|
) {
|
||||||
const key = message.key as ExtendedMessageKey;
|
const key = message.key as WAMessageKey;
|
||||||
|
|
||||||
if (!chatwootMessageIds.messageId || !key?.id) {
|
if (!chatwootMessageIds.messageId || !key?.id) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use raw SQL to avoid JSON path issues
|
// Use raw SQL to avoid JSON path issues
|
||||||
await this.prismaRepository.$executeRaw`
|
const result = await this.prismaRepository.$executeRaw`
|
||||||
UPDATE "Message"
|
UPDATE "Message"
|
||||||
SET
|
SET
|
||||||
"chatwootMessageId" = ${chatwootMessageIds.messageId},
|
"chatwootMessageId" = ${chatwootMessageIds.messageId},
|
||||||
@@ -1560,6 +1583,8 @@ export class ChatwootService {
|
|||||||
AND "key"->>'id' = ${key.id}
|
AND "key"->>'id' = ${key.id}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
this.logger.verbose(`Update result: ${result} rows affected`);
|
||||||
|
|
||||||
if (this.isImportHistoryAvailable()) {
|
if (this.isImportHistoryAvailable()) {
|
||||||
chatwootImport.updateMessageSourceID(chatwootMessageIds.messageId, key.id);
|
chatwootImport.updateMessageSourceID(chatwootMessageIds.messageId, key.id);
|
||||||
}
|
}
|
||||||
@@ -1609,12 +1634,13 @@ export class ChatwootService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const key = message?.key as ExtendedMessageKey;
|
const key = message?.key as WAMessageKey;
|
||||||
|
const messageContent = message?.message as WAMessageContent;
|
||||||
|
|
||||||
if (message && key?.id) {
|
if (messageContent && key?.id) {
|
||||||
return {
|
return {
|
||||||
key: message.key as proto.IMessageKey,
|
key: key,
|
||||||
message: message.message as proto.IMessage,
|
message: messageContent,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1913,6 +1939,7 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (event === 'messages.upsert' || event === 'send.message') {
|
if (event === 'messages.upsert' || event === 'send.message') {
|
||||||
|
this.logger.info(`[${event}] New message received - Instance: ${JSON.stringify(body, null, 2)}`);
|
||||||
if (body.key.remoteJid === 'status@broadcast') {
|
if (body.key.remoteJid === 'status@broadcast') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -2003,7 +2030,10 @@ export class ChatwootService {
|
|||||||
|
|
||||||
if (body.key.remoteJid.includes('@g.us')) {
|
if (body.key.remoteJid.includes('@g.us')) {
|
||||||
const participantName = body.pushName;
|
const participantName = body.pushName;
|
||||||
const rawPhoneNumber = body.key.participant.split('@')[0];
|
const rawPhoneNumber =
|
||||||
|
body.key.addressingMode === 'lid' && !body.key.fromMe
|
||||||
|
? body.key.participantAlt.split('@')[0]
|
||||||
|
: body.key.participant.split('@')[0];
|
||||||
const phoneMatch = rawPhoneNumber.match(/^(\d{2})(\d{2})(\d{4})(\d{4})$/);
|
const phoneMatch = rawPhoneNumber.match(/^(\d{2})(\d{2})(\d{4})(\d{4})$/);
|
||||||
|
|
||||||
let formattedPhoneNumber: string;
|
let formattedPhoneNumber: string;
|
||||||
@@ -2017,9 +2047,11 @@ export class ChatwootService {
|
|||||||
let content: string;
|
let content: string;
|
||||||
|
|
||||||
if (!body.key.fromMe) {
|
if (!body.key.fromMe) {
|
||||||
content = `**${formattedPhoneNumber} - ${participantName}:**\n\n${bodyMessage}`;
|
content = bodyMessage
|
||||||
|
? `**${formattedPhoneNumber} - ${participantName}:**\n\n${bodyMessage}`
|
||||||
|
: `**${formattedPhoneNumber} - ${participantName}:**`;
|
||||||
} else {
|
} else {
|
||||||
content = `${bodyMessage}`;
|
content = bodyMessage || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
const send = await this.sendData(
|
const send = await this.sendData(
|
||||||
@@ -2144,7 +2176,10 @@ export class ChatwootService {
|
|||||||
|
|
||||||
if (body.key.remoteJid.includes('@g.us')) {
|
if (body.key.remoteJid.includes('@g.us')) {
|
||||||
const participantName = body.pushName;
|
const participantName = body.pushName;
|
||||||
const rawPhoneNumber = body.key.participant.split('@')[0];
|
const rawPhoneNumber =
|
||||||
|
body.key.addressingMode === 'lid' && !body.key.fromMe
|
||||||
|
? body.key.participantAlt.split('@')[0]
|
||||||
|
: body.key.participant.split('@')[0];
|
||||||
const phoneMatch = rawPhoneNumber.match(/^(\d{2})(\d{2})(\d{4})(\d{4})$/);
|
const phoneMatch = rawPhoneNumber.match(/^(\d{2})(\d{2})(\d{4})(\d{4})$/);
|
||||||
|
|
||||||
let formattedPhoneNumber: string;
|
let formattedPhoneNumber: string;
|
||||||
@@ -2235,9 +2270,8 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (event === 'messages.edit' || event === 'send.message.update') {
|
if (event === 'messages.edit' || event === 'send.message.update') {
|
||||||
const editedText = `${
|
const editedMessageContent =
|
||||||
body?.editedMessage?.conversation || body?.editedMessage?.extendedTextMessage?.text
|
body?.editedMessage?.conversation || body?.editedMessage?.extendedTextMessage?.text;
|
||||||
}\n\n_\`${i18next.t('cw.message.edited')}.\`_`;
|
|
||||||
const message = await this.getMessageByKeyId(instance, body?.key?.id);
|
const message = await this.getMessageByKeyId(instance, body?.key?.id);
|
||||||
|
|
||||||
if (!message) {
|
if (!message) {
|
||||||
@@ -2245,11 +2279,14 @@ export class ChatwootService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const key = message.key as ExtendedMessageKey;
|
const key = message.key as WAMessageKey;
|
||||||
|
|
||||||
const messageType = key?.fromMe ? 'outgoing' : 'incoming';
|
const messageType = key?.fromMe ? 'outgoing' : 'incoming';
|
||||||
|
|
||||||
if (message && message.chatwootConversationId) {
|
if (message && message.chatwootConversationId && message.chatwootMessageId) {
|
||||||
|
// Criar nova mensagem com formato: "Mensagem editada:\n\nteste1"
|
||||||
|
const editedText = `\n\n\`${i18next.t('cw.message.edited')}:\`\n\n${editedMessageContent}`;
|
||||||
|
|
||||||
const send = await this.createMessage(
|
const send = await this.createMessage(
|
||||||
instance,
|
instance,
|
||||||
message.chatwootConversationId,
|
message.chatwootConversationId,
|
||||||
@@ -2327,15 +2364,30 @@ export class ChatwootService {
|
|||||||
await this.createBotMessage(instance, msgStatus, 'incoming');
|
await this.createBotMessage(instance, msgStatus, 'incoming');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event === 'connection.update') {
|
if (event === 'connection.update' && body.status === 'open') {
|
||||||
if (body.status === 'open') {
|
const waInstance = this.waMonitor.waInstances[instance.instanceName];
|
||||||
// if we have qrcode count then we understand that a new connection was established
|
if (!waInstance) return;
|
||||||
if (this.waMonitor.waInstances[instance.instanceName].qrCode.count > 0) {
|
|
||||||
const msgConnection = i18next.t('cw.inbox.connected');
|
const now = Date.now();
|
||||||
await this.createBotMessage(instance, msgConnection, 'incoming');
|
const timeSinceLastNotification = now - (waInstance.lastConnectionNotification || 0);
|
||||||
this.waMonitor.waInstances[instance.instanceName].qrCode.count = 0;
|
|
||||||
chatwootImport.clearAll(instance);
|
// Se a conexão foi estabelecida via QR code, notifica imediatamente.
|
||||||
}
|
if (waInstance.qrCode && waInstance.qrCode.count > 0) {
|
||||||
|
const msgConnection = i18next.t('cw.inbox.connected');
|
||||||
|
await this.createBotMessage(instance, msgConnection, 'incoming');
|
||||||
|
waInstance.qrCode.count = 0;
|
||||||
|
waInstance.lastConnectionNotification = now;
|
||||||
|
chatwootImport.clearAll(instance);
|
||||||
|
}
|
||||||
|
// Se não foi via QR code, verifica o throttling.
|
||||||
|
else if (timeSinceLastNotification >= 30000) {
|
||||||
|
const msgConnection = i18next.t('cw.inbox.connected');
|
||||||
|
await this.createBotMessage(instance, msgConnection, 'incoming');
|
||||||
|
waInstance.lastConnectionNotification = now;
|
||||||
|
} else {
|
||||||
|
this.logger.warn(
|
||||||
|
`Connection notification skipped for ${instance.instanceName} - too frequent (${timeSinceLastNotification}ms since last)`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2552,7 +2604,7 @@ export class ChatwootService {
|
|||||||
await chatwootImport.importHistoryMessages(instance, this, inbox, this.provider);
|
await chatwootImport.importHistoryMessages(instance, this, inbox, this.provider);
|
||||||
const waInstance = this.waMonitor.waInstances[instance.instanceName];
|
const waInstance = this.waMonitor.waInstances[instance.instanceName];
|
||||||
waInstance.clearCacheChatwoot();
|
waInstance.clearCacheChatwoot();
|
||||||
} catch (error) {
|
} catch {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,12 +112,19 @@ class ChatwootImport {
|
|||||||
const bindInsert = [provider.accountId];
|
const bindInsert = [provider.accountId];
|
||||||
|
|
||||||
for (const contact of contactsChunk) {
|
for (const contact of contactsChunk) {
|
||||||
bindInsert.push(contact.pushName);
|
const isGroup = this.isIgnorePhoneNumber(contact.remoteJid);
|
||||||
|
|
||||||
|
const contactName = isGroup ? `${contact.pushName} (GROUP)` : contact.pushName;
|
||||||
|
bindInsert.push(contactName);
|
||||||
const bindName = `$${bindInsert.length}`;
|
const bindName = `$${bindInsert.length}`;
|
||||||
|
|
||||||
bindInsert.push(`+${contact.remoteJid.split('@')[0]}`);
|
let bindPhoneNumber: string;
|
||||||
const bindPhoneNumber = `$${bindInsert.length}`;
|
if (!isGroup) {
|
||||||
|
bindInsert.push(`+${contact.remoteJid.split('@')[0]}`);
|
||||||
|
bindPhoneNumber = `$${bindInsert.length}`;
|
||||||
|
} else {
|
||||||
|
bindPhoneNumber = 'NULL';
|
||||||
|
}
|
||||||
bindInsert.push(contact.remoteJid);
|
bindInsert.push(contact.remoteJid);
|
||||||
const bindIdentifier = `$${bindInsert.length}`;
|
const bindIdentifier = `$${bindInsert.length}`;
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { Integration } from '@api/types/wa.types';
|
|||||||
import { ConfigService, HttpServer } from '@config/env.config';
|
import { ConfigService, HttpServer } from '@config/env.config';
|
||||||
import { Dify, DifySetting, IntegrationSession } from '@prisma/client';
|
import { Dify, DifySetting, IntegrationSession } from '@prisma/client';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import { isURL } from 'class-validator';
|
||||||
|
|
||||||
import { BaseChatbotService } from '../../base-chatbot.service';
|
import { BaseChatbotService } from '../../base-chatbot.service';
|
||||||
import { OpenaiService } from '../../openai/services/openai.service';
|
import { OpenaiService } from '../../openai/services/openai.service';
|
||||||
@@ -78,15 +79,35 @@ export class DifyService extends BaseChatbotService<Dify, DifySetting> {
|
|||||||
|
|
||||||
// Handle image messages
|
// Handle image messages
|
||||||
if (this.isImageMessage(content)) {
|
if (this.isImageMessage(content)) {
|
||||||
const contentSplit = content.split('|');
|
const media = content.split('|');
|
||||||
payload.files = [
|
|
||||||
{
|
if (msg.message.mediaUrl || msg.message.base64) {
|
||||||
type: 'image',
|
let mediaBase64 = msg.message.base64 || null;
|
||||||
transfer_method: 'remote_url',
|
|
||||||
url: contentSplit[1].split('?')[0],
|
if (msg.message.mediaUrl && isURL(msg.message.mediaUrl)) {
|
||||||
},
|
const result = await axios.get(msg.message.mediaUrl, { responseType: 'arraybuffer' });
|
||||||
];
|
mediaBase64 = Buffer.from(result.data).toString('base64');
|
||||||
payload.query = contentSplit[2] || content;
|
}
|
||||||
|
|
||||||
|
if (mediaBase64) {
|
||||||
|
payload.files = [
|
||||||
|
{
|
||||||
|
type: 'image',
|
||||||
|
transfer_method: 'remote_url',
|
||||||
|
url: mediaBase64,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
payload.files = [
|
||||||
|
{
|
||||||
|
type: 'image',
|
||||||
|
transfer_method: 'remote_url',
|
||||||
|
url: media[1].split('?')[0],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
payload.query = media[2] || content;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
@@ -107,7 +128,7 @@ export class DifyService extends BaseChatbotService<Dify, DifySetting> {
|
|||||||
const conversationId = response?.data?.conversation_id;
|
const conversationId = response?.data?.conversation_id;
|
||||||
|
|
||||||
if (message) {
|
if (message) {
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.prismaRepository.integrationSession.update({
|
await this.prismaRepository.integrationSession.update({
|
||||||
@@ -140,15 +161,35 @@ export class DifyService extends BaseChatbotService<Dify, DifySetting> {
|
|||||||
|
|
||||||
// Handle image messages
|
// Handle image messages
|
||||||
if (this.isImageMessage(content)) {
|
if (this.isImageMessage(content)) {
|
||||||
const contentSplit = content.split('|');
|
const media = content.split('|');
|
||||||
payload.files = [
|
|
||||||
{
|
if (msg.message.mediaUrl || msg.message.base64) {
|
||||||
type: 'image',
|
let mediaBase64 = msg.message.base64 || null;
|
||||||
transfer_method: 'remote_url',
|
|
||||||
url: contentSplit[1].split('?')[0],
|
if (msg.message.mediaUrl && isURL(msg.message.mediaUrl)) {
|
||||||
},
|
const result = await axios.get(msg.message.mediaUrl, { responseType: 'arraybuffer' });
|
||||||
];
|
mediaBase64 = Buffer.from(result.data).toString('base64');
|
||||||
payload.inputs.query = contentSplit[2] || content;
|
}
|
||||||
|
|
||||||
|
if (mediaBase64) {
|
||||||
|
payload.files = [
|
||||||
|
{
|
||||||
|
type: 'image',
|
||||||
|
transfer_method: 'remote_url',
|
||||||
|
url: mediaBase64,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
payload.files = [
|
||||||
|
{
|
||||||
|
type: 'image',
|
||||||
|
transfer_method: 'remote_url',
|
||||||
|
url: media[1].split('?')[0],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
payload.inputs.query = media[2] || content;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
@@ -169,7 +210,7 @@ export class DifyService extends BaseChatbotService<Dify, DifySetting> {
|
|||||||
const conversationId = response?.data?.conversation_id;
|
const conversationId = response?.data?.conversation_id;
|
||||||
|
|
||||||
if (message) {
|
if (message) {
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.prismaRepository.integrationSession.update({
|
await this.prismaRepository.integrationSession.update({
|
||||||
@@ -202,15 +243,26 @@ export class DifyService extends BaseChatbotService<Dify, DifySetting> {
|
|||||||
|
|
||||||
// Handle image messages
|
// Handle image messages
|
||||||
if (this.isImageMessage(content)) {
|
if (this.isImageMessage(content)) {
|
||||||
const contentSplit = content.split('|');
|
const media = content.split('|');
|
||||||
payload.files = [
|
|
||||||
{
|
if (msg.message.mediaUrl || msg.message.base64) {
|
||||||
type: 'image',
|
payload.files = [
|
||||||
transfer_method: 'remote_url',
|
{
|
||||||
url: contentSplit[1].split('?')[0],
|
type: 'image',
|
||||||
},
|
transfer_method: 'remote_url',
|
||||||
];
|
url: msg.message.mediaUrl || msg.message.base64,
|
||||||
payload.query = contentSplit[2] || content;
|
},
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
payload.files = [
|
||||||
|
{
|
||||||
|
type: 'image',
|
||||||
|
transfer_method: 'remote_url',
|
||||||
|
url: media[1].split('?')[0],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
payload.query = media[2] || content;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
@@ -246,7 +298,7 @@ export class DifyService extends BaseChatbotService<Dify, DifySetting> {
|
|||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
|
||||||
if (answer) {
|
if (answer) {
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, answer, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, answer, settings, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.prismaRepository.integrationSession.update({
|
await this.prismaRepository.integrationSession.update({
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { ConfigService, HttpServer } from '@config/env.config';
|
|||||||
import { Evoai, EvoaiSetting, IntegrationSession } from '@prisma/client';
|
import { Evoai, EvoaiSetting, IntegrationSession } from '@prisma/client';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { downloadMediaMessage } from 'baileys';
|
import { downloadMediaMessage } from 'baileys';
|
||||||
|
import { isURL } from 'class-validator';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
import { BaseChatbotService } from '../../base-chatbot.service';
|
import { BaseChatbotService } from '../../base-chatbot.service';
|
||||||
@@ -82,23 +83,43 @@ export class EvoaiService extends BaseChatbotService<Evoai, EvoaiSetting> {
|
|||||||
|
|
||||||
// Handle image message if present
|
// Handle image message if present
|
||||||
if (this.isImageMessage(content) && msg) {
|
if (this.isImageMessage(content) && msg) {
|
||||||
const contentSplit = content.split('|');
|
const media = content.split('|');
|
||||||
parts[0].text = contentSplit[2] || content;
|
parts[0].text = media[2] || content;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Download the image
|
if (msg.message.mediaUrl || msg.message.base64) {
|
||||||
const mediaBuffer = await downloadMediaMessage(msg, 'buffer', {});
|
let mediaBase64 = msg.message.base64 || null;
|
||||||
const fileContent = Buffer.from(mediaBuffer).toString('base64');
|
|
||||||
const fileName = contentSplit[2] || `${msg.key?.id || 'image'}.jpg`;
|
|
||||||
|
|
||||||
parts.push({
|
if (msg.message.mediaUrl && isURL(msg.message.mediaUrl)) {
|
||||||
type: 'file',
|
const result = await axios.get(msg.message.mediaUrl, { responseType: 'arraybuffer' });
|
||||||
file: {
|
mediaBase64 = Buffer.from(result.data).toString('base64');
|
||||||
name: fileName,
|
}
|
||||||
mimeType: 'image/jpeg',
|
|
||||||
bytes: fileContent,
|
if (mediaBase64) {
|
||||||
},
|
parts.push({
|
||||||
} as any);
|
type: 'file',
|
||||||
|
file: {
|
||||||
|
name: msg.key.id + '.jpeg',
|
||||||
|
mimeType: 'image/jpeg',
|
||||||
|
bytes: mediaBase64,
|
||||||
|
},
|
||||||
|
} as any);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Download the image
|
||||||
|
const mediaBuffer = await downloadMediaMessage(msg, 'buffer', {});
|
||||||
|
const fileContent = Buffer.from(mediaBuffer).toString('base64');
|
||||||
|
const fileName = media[2] || `${msg.key?.id || 'image'}.jpg`;
|
||||||
|
|
||||||
|
parts.push({
|
||||||
|
type: 'file',
|
||||||
|
file: {
|
||||||
|
name: fileName,
|
||||||
|
mimeType: 'image/jpeg',
|
||||||
|
bytes: fileContent,
|
||||||
|
},
|
||||||
|
} as any);
|
||||||
|
}
|
||||||
} catch (fileErr) {
|
} catch (fileErr) {
|
||||||
this.logger.error(`[EvoAI] Failed to process image: ${fileErr}`);
|
this.logger.error(`[EvoAI] Failed to process image: ${fileErr}`);
|
||||||
}
|
}
|
||||||
@@ -174,7 +195,7 @@ export class EvoaiService extends BaseChatbotService<Evoai, EvoaiSetting> {
|
|||||||
this.logger.debug(`[EvoAI] Extracted message to send: ${message}`);
|
this.logger.debug(`[EvoAI] Extracted message to send: ${message}`);
|
||||||
|
|
||||||
if (message) {
|
if (message) {
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings, true);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(
|
this.logger.error(
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { ConfigService, HttpServer } from '@config/env.config';
|
|||||||
import { EvolutionBot, EvolutionBotSetting, IntegrationSession } from '@prisma/client';
|
import { EvolutionBot, EvolutionBotSetting, IntegrationSession } from '@prisma/client';
|
||||||
import { sendTelemetry } from '@utils/sendTelemetry';
|
import { sendTelemetry } from '@utils/sendTelemetry';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import { isURL } from 'class-validator';
|
||||||
|
|
||||||
import { BaseChatbotService } from '../../base-chatbot.service';
|
import { BaseChatbotService } from '../../base-chatbot.service';
|
||||||
import { OpenaiService } from '../../openai/services/openai.service';
|
import { OpenaiService } from '../../openai/services/openai.service';
|
||||||
@@ -71,16 +72,26 @@ export class EvolutionBotService extends BaseChatbotService<EvolutionBot, Evolut
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isImageMessage(content)) {
|
if (this.isImageMessage(content) && msg) {
|
||||||
const contentSplit = content.split('|');
|
const media = content.split('|');
|
||||||
|
|
||||||
payload.files = [
|
if (msg.message.mediaUrl || msg.message.base64) {
|
||||||
{
|
payload.files = [
|
||||||
type: 'image',
|
{
|
||||||
url: contentSplit[1].split('?')[0],
|
type: 'image',
|
||||||
},
|
url: msg.message.base64 || msg.message.mediaUrl,
|
||||||
];
|
},
|
||||||
payload.query = contentSplit[2] || content;
|
];
|
||||||
|
} else {
|
||||||
|
payload.files = [
|
||||||
|
{
|
||||||
|
type: 'image',
|
||||||
|
url: media[1].split('?')[0],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
payload.query = media[2] || content;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
@@ -115,15 +126,10 @@ export class EvolutionBotService extends BaseChatbotService<EvolutionBot, Evolut
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
this.logger.debug(`[EvolutionBot] Sending request to endpoint: ${endpoint}`);
|
|
||||||
this.logger.debug(`[EvolutionBot] Request payload: ${JSON.stringify(sanitizedPayload, null, 2)}`);
|
|
||||||
|
|
||||||
const response = await axios.post(endpoint, payload, {
|
const response = await axios.post(endpoint, payload, {
|
||||||
headers,
|
headers,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.logger.debug(`[EvolutionBot] Response received - Status: ${response.status}`);
|
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
}
|
}
|
||||||
@@ -134,10 +140,6 @@ export class EvolutionBotService extends BaseChatbotService<EvolutionBot, Evolut
|
|||||||
// Validate linkPreview is boolean and default to true for backward compatibility
|
// Validate linkPreview is boolean and default to true for backward compatibility
|
||||||
const linkPreview = typeof rawLinkPreview === 'boolean' ? rawLinkPreview : true;
|
const linkPreview = typeof rawLinkPreview === 'boolean' ? rawLinkPreview : true;
|
||||||
|
|
||||||
this.logger.debug(
|
|
||||||
`[EvolutionBot] Processing response - Message length: ${message?.length || 0}, LinkPreview: ${linkPreview}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (message && typeof message === 'string' && message.startsWith("'") && message.endsWith("'")) {
|
if (message && typeof message === 'string' && message.startsWith("'") && message.endsWith("'")) {
|
||||||
const innerContent = message.slice(1, -1);
|
const innerContent = message.slice(1, -1);
|
||||||
if (!innerContent.includes("'")) {
|
if (!innerContent.includes("'")) {
|
||||||
@@ -146,17 +148,8 @@ export class EvolutionBotService extends BaseChatbotService<EvolutionBot, Evolut
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (message) {
|
if (message) {
|
||||||
// Send message directly with validated linkPreview option
|
// Use the base class method that handles splitMessages functionality
|
||||||
await instance.textMessage(
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings, linkPreview);
|
||||||
{
|
|
||||||
number: remoteJid.split('@')[0],
|
|
||||||
delay: settings?.delayMessage || 1000,
|
|
||||||
text: message,
|
|
||||||
linkPreview, // Always boolean, defaults to true
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
this.logger.debug(`[EvolutionBot] Message sent successfully with linkPreview: ${linkPreview}`);
|
|
||||||
} else {
|
} else {
|
||||||
this.logger.warn(`[EvolutionBot] No message content received from bot response`);
|
this.logger.warn(`[EvolutionBot] No message content received from bot response`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { Integration } from '@api/types/wa.types';
|
|||||||
import { ConfigService, HttpServer } from '@config/env.config';
|
import { ConfigService, HttpServer } from '@config/env.config';
|
||||||
import { Flowise as FlowiseModel, IntegrationSession } from '@prisma/client';
|
import { Flowise as FlowiseModel, IntegrationSession } from '@prisma/client';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import { isURL } from 'class-validator';
|
||||||
|
|
||||||
import { BaseChatbotService } from '../../base-chatbot.service';
|
import { BaseChatbotService } from '../../base-chatbot.service';
|
||||||
import { OpenaiService } from '../../openai/services/openai.service';
|
import { OpenaiService } from '../../openai/services/openai.service';
|
||||||
@@ -82,17 +83,28 @@ export class FlowiseService extends BaseChatbotService<FlowiseModel> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.isImageMessage(content)) {
|
if (this.isImageMessage(content)) {
|
||||||
const contentSplit = content.split('|');
|
const media = content.split('|');
|
||||||
|
|
||||||
payload.uploads = [
|
if (msg.message.mediaUrl || msg.message.base64) {
|
||||||
{
|
payload.uploads = [
|
||||||
data: contentSplit[1].split('?')[0],
|
{
|
||||||
type: 'url',
|
data: msg.message.base64 || msg.message.mediaUrl,
|
||||||
name: 'Flowise.png',
|
type: 'url',
|
||||||
mime: 'image/png',
|
name: 'Flowise.png',
|
||||||
},
|
mime: 'image/png',
|
||||||
];
|
},
|
||||||
payload.question = contentSplit[2] || content;
|
];
|
||||||
|
} else {
|
||||||
|
payload.uploads = [
|
||||||
|
{
|
||||||
|
data: media[1].split('?')[0],
|
||||||
|
type: 'url',
|
||||||
|
name: 'Flowise.png',
|
||||||
|
mime: 'image/png',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
payload.question = media[2] || content;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
@@ -130,7 +142,7 @@ export class FlowiseService extends BaseChatbotService<FlowiseModel> {
|
|||||||
|
|
||||||
if (message) {
|
if (message) {
|
||||||
// Use the base class method to send the message to WhatsApp
|
// Use the base class method to send the message to WhatsApp
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ export class N8nService extends BaseChatbotService<N8n, N8nSetting> {
|
|||||||
const message = response?.data?.output || response?.data?.answer;
|
const message = response?.data?.output || response?.data?.answer;
|
||||||
|
|
||||||
// Use base class method instead of custom implementation
|
// Use base class method instead of custom implementation
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings, true);
|
||||||
|
|
||||||
await this.prismaRepository.integrationSession.update({
|
await this.prismaRepository.integrationSession.update({
|
||||||
where: {
|
where: {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { IntegrationSession, OpenaiBot, OpenaiSetting } from '@prisma/client';
|
|||||||
import { sendTelemetry } from '@utils/sendTelemetry';
|
import { sendTelemetry } from '@utils/sendTelemetry';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { downloadMediaMessage } from 'baileys';
|
import { downloadMediaMessage } from 'baileys';
|
||||||
|
import { isURL } from 'class-validator';
|
||||||
import FormData from 'form-data';
|
import FormData from 'form-data';
|
||||||
import OpenAI from 'openai';
|
import OpenAI from 'openai';
|
||||||
import P from 'pino';
|
import P from 'pino';
|
||||||
@@ -85,6 +86,7 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
|
|||||||
remoteJid,
|
remoteJid,
|
||||||
"Sorry, I couldn't transcribe your audio message. Could you please type your message instead?",
|
"Sorry, I couldn't transcribe your audio message. Could you please type your message instead?",
|
||||||
settings,
|
settings,
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -173,7 +175,7 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Process with the appropriate API based on bot type
|
// Process with the appropriate API based on bot type
|
||||||
await this.sendMessageToBot(instance, session, settings, openaiBot, remoteJid, pushName || '', content);
|
await this.sendMessageToBot(instance, session, settings, openaiBot, remoteJid, pushName || '', content, msg);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(`Error in process: ${error.message || JSON.stringify(error)}`);
|
this.logger.error(`Error in process: ${error.message || JSON.stringify(error)}`);
|
||||||
return;
|
return;
|
||||||
@@ -191,6 +193,7 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
|
|||||||
remoteJid: string,
|
remoteJid: string,
|
||||||
pushName: string,
|
pushName: string,
|
||||||
content: string,
|
content: string,
|
||||||
|
msg?: any,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
this.logger.log(`Sending message to bot for remoteJid: ${remoteJid}, bot type: ${openaiBot.botType}`);
|
this.logger.log(`Sending message to bot for remoteJid: ${remoteJid}, bot type: ${openaiBot.botType}`);
|
||||||
|
|
||||||
@@ -222,10 +225,11 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
|
|||||||
pushName,
|
pushName,
|
||||||
false, // Not fromMe
|
false, // Not fromMe
|
||||||
content,
|
content,
|
||||||
|
msg,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
this.logger.log('Processing with ChatCompletion API');
|
this.logger.log('Processing with ChatCompletion API');
|
||||||
message = await this.processChatCompletionMessage(instance, openaiBot, remoteJid, content);
|
message = await this.processChatCompletionMessage(instance, openaiBot, remoteJid, content, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.log(`Got response from OpenAI: ${message?.substring(0, 50)}${message?.length > 50 ? '...' : ''}`);
|
this.logger.log(`Got response from OpenAI: ${message?.substring(0, 50)}${message?.length > 50 ? '...' : ''}`);
|
||||||
@@ -233,7 +237,7 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
|
|||||||
// Send the response
|
// Send the response
|
||||||
if (message) {
|
if (message) {
|
||||||
this.logger.log('Sending message to WhatsApp');
|
this.logger.log('Sending message to WhatsApp');
|
||||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
await this.sendMessageWhatsApp(instance, remoteJid, message, settings, true);
|
||||||
} else {
|
} else {
|
||||||
this.logger.error('No message to send to WhatsApp');
|
this.logger.error('No message to send to WhatsApp');
|
||||||
}
|
}
|
||||||
@@ -268,6 +272,7 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
|
|||||||
pushName: string,
|
pushName: string,
|
||||||
fromMe: boolean,
|
fromMe: boolean,
|
||||||
content: string,
|
content: string,
|
||||||
|
msg?: any,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const messageData: any = {
|
const messageData: any = {
|
||||||
role: fromMe ? 'assistant' : 'user',
|
role: fromMe ? 'assistant' : 'user',
|
||||||
@@ -276,18 +281,35 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
|
|||||||
|
|
||||||
// Handle image messages
|
// Handle image messages
|
||||||
if (this.isImageMessage(content)) {
|
if (this.isImageMessage(content)) {
|
||||||
const contentSplit = content.split('|');
|
const media = content.split('|');
|
||||||
const url = contentSplit[1].split('?')[0];
|
|
||||||
|
|
||||||
messageData.content = [
|
if (msg.message.mediaUrl || msg.message.base64) {
|
||||||
{ type: 'text', text: contentSplit[2] || content },
|
let mediaBase64 = msg.message.base64 || null;
|
||||||
{
|
|
||||||
type: 'image_url',
|
if (msg.message.mediaUrl && isURL(msg.message.mediaUrl)) {
|
||||||
image_url: {
|
const result = await axios.get(msg.message.mediaUrl, { responseType: 'arraybuffer' });
|
||||||
url: url,
|
mediaBase64 = Buffer.from(result.data).toString('base64');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mediaBase64) {
|
||||||
|
messageData.content = [
|
||||||
|
{ type: 'text', text: media[2] || content },
|
||||||
|
{ type: 'image_url', image_url: { url: mediaBase64 } },
|
||||||
|
];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const url = media[1].split('?')[0];
|
||||||
|
|
||||||
|
messageData.content = [
|
||||||
|
{ type: 'text', text: media[2] || content },
|
||||||
|
{
|
||||||
|
type: 'image_url',
|
||||||
|
image_url: {
|
||||||
|
url: url,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
];
|
||||||
];
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get thread ID from session or create new thread
|
// Get thread ID from session or create new thread
|
||||||
@@ -376,6 +398,7 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
|
|||||||
openaiBot: OpenaiBot,
|
openaiBot: OpenaiBot,
|
||||||
remoteJid: string,
|
remoteJid: string,
|
||||||
content: string,
|
content: string,
|
||||||
|
msg?: any,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
this.logger.log('Starting processChatCompletionMessage');
|
this.logger.log('Starting processChatCompletionMessage');
|
||||||
|
|
||||||
@@ -468,18 +491,26 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
|
|||||||
// Handle image messages
|
// Handle image messages
|
||||||
if (this.isImageMessage(content)) {
|
if (this.isImageMessage(content)) {
|
||||||
this.logger.log('Found image message');
|
this.logger.log('Found image message');
|
||||||
const contentSplit = content.split('|');
|
const media = content.split('|');
|
||||||
const url = contentSplit[1].split('?')[0];
|
|
||||||
|
|
||||||
messageData.content = [
|
if (msg.message.mediaUrl || msg.message.base64) {
|
||||||
{ type: 'text', text: contentSplit[2] || content },
|
messageData.content = [
|
||||||
{
|
{ type: 'text', text: media[2] || content },
|
||||||
type: 'image_url',
|
{ type: 'image_url', image_url: { url: msg.message.base64 || msg.message.mediaUrl } },
|
||||||
image_url: {
|
];
|
||||||
url: url,
|
} else {
|
||||||
|
const url = media[1].split('?')[0];
|
||||||
|
|
||||||
|
messageData.content = [
|
||||||
|
{ type: 'text', text: media[2] || content },
|
||||||
|
{
|
||||||
|
type: 'image_url',
|
||||||
|
image_url: {
|
||||||
|
url: url,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
];
|
||||||
];
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Combine all messages: system messages, pre-defined messages, conversation history, and current message
|
// Combine all messages: system messages, pre-defined messages, conversation history, and current message
|
||||||
|
|||||||
@@ -318,7 +318,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
|
|||||||
} else if (formattedText.includes('[buttons]')) {
|
} else if (formattedText.includes('[buttons]')) {
|
||||||
await this.processButtonMessage(instance, formattedText, session.remoteJid);
|
await this.processButtonMessage(instance, formattedText, session.remoteJid);
|
||||||
} else {
|
} else {
|
||||||
await this.sendMessageWhatsApp(instance, session.remoteJid, formattedText, settings);
|
await this.sendMessageWhatsApp(instance, session.remoteJid, formattedText, settings, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendTelemetry('/message/sendText');
|
sendTelemetry('/message/sendText');
|
||||||
@@ -393,7 +393,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
|
|||||||
} else if (formattedText.includes('[buttons]')) {
|
} else if (formattedText.includes('[buttons]')) {
|
||||||
await this.processButtonMessage(instance, formattedText, session.remoteJid);
|
await this.processButtonMessage(instance, formattedText, session.remoteJid);
|
||||||
} else {
|
} else {
|
||||||
await this.sendMessageWhatsApp(instance, session.remoteJid, formattedText, settings);
|
await this.sendMessageWhatsApp(instance, session.remoteJid, formattedText, settings, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendTelemetry('/message/sendText');
|
sendTelemetry('/message/sendText');
|
||||||
@@ -642,15 +642,21 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
|
|||||||
|
|
||||||
if (!content) {
|
if (!content) {
|
||||||
if (unknownMessage) {
|
if (unknownMessage) {
|
||||||
await this.sendMessageWhatsApp(waInstance, remoteJid, unknownMessage, {
|
await this.sendMessageWhatsApp(
|
||||||
delayMessage,
|
waInstance,
|
||||||
expire,
|
remoteJid,
|
||||||
keywordFinish,
|
|
||||||
listeningFromMe,
|
|
||||||
stopBotFromMe,
|
|
||||||
keepOpen,
|
|
||||||
unknownMessage,
|
unknownMessage,
|
||||||
});
|
{
|
||||||
|
delayMessage,
|
||||||
|
expire,
|
||||||
|
keywordFinish,
|
||||||
|
listeningFromMe,
|
||||||
|
stopBotFromMe,
|
||||||
|
keepOpen,
|
||||||
|
unknownMessage,
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
);
|
||||||
sendTelemetry('/message/sendText');
|
sendTelemetry('/message/sendText');
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -801,15 +807,21 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
|
|||||||
if (!data?.messages || data.messages.length === 0) {
|
if (!data?.messages || data.messages.length === 0) {
|
||||||
if (!content) {
|
if (!content) {
|
||||||
if (unknownMessage) {
|
if (unknownMessage) {
|
||||||
await this.sendMessageWhatsApp(waInstance, remoteJid, unknownMessage, {
|
await this.sendMessageWhatsApp(
|
||||||
delayMessage,
|
waInstance,
|
||||||
expire,
|
remoteJid,
|
||||||
keywordFinish,
|
|
||||||
listeningFromMe,
|
|
||||||
stopBotFromMe,
|
|
||||||
keepOpen,
|
|
||||||
unknownMessage,
|
unknownMessage,
|
||||||
});
|
{
|
||||||
|
delayMessage,
|
||||||
|
expire,
|
||||||
|
keywordFinish,
|
||||||
|
listeningFromMe,
|
||||||
|
stopBotFromMe,
|
||||||
|
keepOpen,
|
||||||
|
unknownMessage,
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
);
|
||||||
sendTelemetry('/message/sendText');
|
sendTelemetry('/message/sendText');
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -903,15 +915,21 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
|
|||||||
|
|
||||||
if (!content) {
|
if (!content) {
|
||||||
if (unknownMessage) {
|
if (unknownMessage) {
|
||||||
await this.sendMessageWhatsApp(waInstance, remoteJid, unknownMessage, {
|
await this.sendMessageWhatsApp(
|
||||||
delayMessage,
|
waInstance,
|
||||||
expire,
|
remoteJid,
|
||||||
keywordFinish,
|
|
||||||
listeningFromMe,
|
|
||||||
stopBotFromMe,
|
|
||||||
keepOpen,
|
|
||||||
unknownMessage,
|
unknownMessage,
|
||||||
});
|
{
|
||||||
|
delayMessage,
|
||||||
|
expire,
|
||||||
|
keywordFinish,
|
||||||
|
listeningFromMe,
|
||||||
|
stopBotFromMe,
|
||||||
|
keepOpen,
|
||||||
|
unknownMessage,
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
);
|
||||||
sendTelemetry('/message/sendText');
|
sendTelemetry('/message/sendText');
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -40,6 +40,11 @@ export class EventDto {
|
|||||||
useTLS?: boolean;
|
useTLS?: boolean;
|
||||||
events?: string[];
|
events?: string[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
kafka?: {
|
||||||
|
enabled?: boolean;
|
||||||
|
events?: string[];
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function EventInstanceMixin<TBase extends Constructor>(Base: TBase) {
|
export function EventInstanceMixin<TBase extends Constructor>(Base: TBase) {
|
||||||
@@ -82,5 +87,10 @@ export function EventInstanceMixin<TBase extends Constructor>(Base: TBase) {
|
|||||||
useTLS?: boolean;
|
useTLS?: boolean;
|
||||||
events?: string[];
|
events?: string[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
kafka?: {
|
||||||
|
enabled?: boolean;
|
||||||
|
events?: string[];
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { KafkaController } from '@api/integrations/event/kafka/kafka.controller';
|
||||||
import { NatsController } from '@api/integrations/event/nats/nats.controller';
|
import { NatsController } from '@api/integrations/event/nats/nats.controller';
|
||||||
import { PusherController } from '@api/integrations/event/pusher/pusher.controller';
|
import { PusherController } from '@api/integrations/event/pusher/pusher.controller';
|
||||||
import { RabbitmqController } from '@api/integrations/event/rabbitmq/rabbitmq.controller';
|
import { RabbitmqController } from '@api/integrations/event/rabbitmq/rabbitmq.controller';
|
||||||
@@ -17,6 +18,7 @@ export class EventManager {
|
|||||||
private natsController: NatsController;
|
private natsController: NatsController;
|
||||||
private sqsController: SqsController;
|
private sqsController: SqsController;
|
||||||
private pusherController: PusherController;
|
private pusherController: PusherController;
|
||||||
|
private kafkaController: KafkaController;
|
||||||
|
|
||||||
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
|
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
|
||||||
this.prisma = prismaRepository;
|
this.prisma = prismaRepository;
|
||||||
@@ -28,6 +30,7 @@ export class EventManager {
|
|||||||
this.nats = new NatsController(prismaRepository, waMonitor);
|
this.nats = new NatsController(prismaRepository, waMonitor);
|
||||||
this.sqs = new SqsController(prismaRepository, waMonitor);
|
this.sqs = new SqsController(prismaRepository, waMonitor);
|
||||||
this.pusher = new PusherController(prismaRepository, waMonitor);
|
this.pusher = new PusherController(prismaRepository, waMonitor);
|
||||||
|
this.kafka = new KafkaController(prismaRepository, waMonitor);
|
||||||
}
|
}
|
||||||
|
|
||||||
public set prisma(prisma: PrismaRepository) {
|
public set prisma(prisma: PrismaRepository) {
|
||||||
@@ -93,12 +96,20 @@ export class EventManager {
|
|||||||
return this.pusherController;
|
return this.pusherController;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public set kafka(kafka: KafkaController) {
|
||||||
|
this.kafkaController = kafka;
|
||||||
|
}
|
||||||
|
public get kafka() {
|
||||||
|
return this.kafkaController;
|
||||||
|
}
|
||||||
|
|
||||||
public init(httpServer: Server): void {
|
public init(httpServer: Server): void {
|
||||||
this.websocket.init(httpServer);
|
this.websocket.init(httpServer);
|
||||||
this.rabbitmq.init();
|
this.rabbitmq.init();
|
||||||
this.nats.init();
|
this.nats.init();
|
||||||
this.sqs.init();
|
this.sqs.init();
|
||||||
this.pusher.init();
|
this.pusher.init();
|
||||||
|
this.kafka.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async emit(eventData: {
|
public async emit(eventData: {
|
||||||
@@ -119,42 +130,47 @@ export class EventManager {
|
|||||||
await this.sqs.emit(eventData);
|
await this.sqs.emit(eventData);
|
||||||
await this.webhook.emit(eventData);
|
await this.webhook.emit(eventData);
|
||||||
await this.pusher.emit(eventData);
|
await this.pusher.emit(eventData);
|
||||||
|
await this.kafka.emit(eventData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async setInstance(instanceName: string, data: any): Promise<any> {
|
public async setInstance(instanceName: string, data: any): Promise<any> {
|
||||||
if (data.websocket)
|
if (data.websocket) {
|
||||||
await this.websocket.set(instanceName, {
|
await this.websocket.set(instanceName, {
|
||||||
websocket: {
|
websocket: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
events: data.websocket?.events,
|
events: data.websocket?.events,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (data.rabbitmq)
|
if (data.rabbitmq) {
|
||||||
await this.rabbitmq.set(instanceName, {
|
await this.rabbitmq.set(instanceName, {
|
||||||
rabbitmq: {
|
rabbitmq: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
events: data.rabbitmq?.events,
|
events: data.rabbitmq?.events,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (data.nats)
|
if (data.nats) {
|
||||||
await this.nats.set(instanceName, {
|
await this.nats.set(instanceName, {
|
||||||
nats: {
|
nats: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
events: data.nats?.events,
|
events: data.nats?.events,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (data.sqs)
|
if (data.sqs) {
|
||||||
await this.sqs.set(instanceName, {
|
await this.sqs.set(instanceName, {
|
||||||
sqs: {
|
sqs: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
events: data.sqs?.events,
|
events: data.sqs?.events,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (data.webhook)
|
if (data.webhook) {
|
||||||
await this.webhook.set(instanceName, {
|
await this.webhook.set(instanceName, {
|
||||||
webhook: {
|
webhook: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
@@ -165,8 +181,9 @@ export class EventManager {
|
|||||||
byEvents: data.webhook?.byEvents,
|
byEvents: data.webhook?.byEvents,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (data.pusher)
|
if (data.pusher) {
|
||||||
await this.pusher.set(instanceName, {
|
await this.pusher.set(instanceName, {
|
||||||
pusher: {
|
pusher: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
@@ -178,5 +195,15 @@ export class EventManager {
|
|||||||
useTLS: data.pusher?.useTLS,
|
useTLS: data.pusher?.useTLS,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.kafka) {
|
||||||
|
await this.kafka.set(instanceName, {
|
||||||
|
kafka: {
|
||||||
|
enabled: true,
|
||||||
|
events: data.kafka?.events,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { KafkaRouter } from '@api/integrations/event/kafka/kafka.router';
|
||||||
import { NatsRouter } from '@api/integrations/event/nats/nats.router';
|
import { NatsRouter } from '@api/integrations/event/nats/nats.router';
|
||||||
import { PusherRouter } from '@api/integrations/event/pusher/pusher.router';
|
import { PusherRouter } from '@api/integrations/event/pusher/pusher.router';
|
||||||
import { RabbitmqRouter } from '@api/integrations/event/rabbitmq/rabbitmq.router';
|
import { RabbitmqRouter } from '@api/integrations/event/rabbitmq/rabbitmq.router';
|
||||||
@@ -18,5 +19,6 @@ export class EventRouter {
|
|||||||
this.router.use('/nats', new NatsRouter(...guards).router);
|
this.router.use('/nats', new NatsRouter(...guards).router);
|
||||||
this.router.use('/pusher', new PusherRouter(...guards).router);
|
this.router.use('/pusher', new PusherRouter(...guards).router);
|
||||||
this.router.use('/sqs', new SqsRouter(...guards).router);
|
this.router.use('/sqs', new SqsRouter(...guards).router);
|
||||||
|
this.router.use('/kafka', new KafkaRouter(...guards).router);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ export const eventSchema: JSONSchema7 = {
|
|||||||
sqs: {
|
sqs: {
|
||||||
$ref: '#/$defs/event',
|
$ref: '#/$defs/event',
|
||||||
},
|
},
|
||||||
|
kafka: {
|
||||||
|
$ref: '#/$defs/event',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
$defs: {
|
$defs: {
|
||||||
event: {
|
event: {
|
||||||
|
|||||||
414
src/api/integrations/event/kafka/kafka.controller.ts
Normal file
414
src/api/integrations/event/kafka/kafka.controller.ts
Normal file
@@ -0,0 +1,414 @@
|
|||||||
|
import { PrismaRepository } from '@api/repository/repository.service';
|
||||||
|
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||||
|
import { configService, Kafka, Log } from '@config/env.config';
|
||||||
|
import { Logger } from '@config/logger.config';
|
||||||
|
import { Consumer, ConsumerConfig, Kafka as KafkaJS, KafkaConfig, Producer, ProducerConfig } from 'kafkajs';
|
||||||
|
|
||||||
|
import { EmitData, EventController, EventControllerInterface } from '../event.controller';
|
||||||
|
|
||||||
|
export class KafkaController extends EventController implements EventControllerInterface {
|
||||||
|
private kafkaClient: KafkaJS | null = null;
|
||||||
|
private producer: Producer | null = null;
|
||||||
|
private consumer: Consumer | null = null;
|
||||||
|
private readonly logger = new Logger('KafkaController');
|
||||||
|
private reconnectAttempts = 0;
|
||||||
|
private maxReconnectAttempts = 10;
|
||||||
|
private reconnectDelay = 5000; // 5 seconds
|
||||||
|
private isReconnecting = false;
|
||||||
|
|
||||||
|
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
|
||||||
|
super(prismaRepository, waMonitor, configService.get<Kafka>('KAFKA')?.ENABLED, 'kafka');
|
||||||
|
}
|
||||||
|
|
||||||
|
public async init(): Promise<void> {
|
||||||
|
if (!this.status) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async connect(): Promise<void> {
|
||||||
|
try {
|
||||||
|
const kafkaConfig = configService.get<Kafka>('KAFKA');
|
||||||
|
|
||||||
|
const clientConfig: KafkaConfig = {
|
||||||
|
clientId: kafkaConfig.CLIENT_ID || 'evolution-api',
|
||||||
|
brokers: kafkaConfig.BROKERS || ['localhost:9092'],
|
||||||
|
connectionTimeout: kafkaConfig.CONNECTION_TIMEOUT || 3000,
|
||||||
|
requestTimeout: kafkaConfig.REQUEST_TIMEOUT || 30000,
|
||||||
|
retry: {
|
||||||
|
initialRetryTime: 100,
|
||||||
|
retries: 8,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add SASL authentication if configured
|
||||||
|
if (kafkaConfig.SASL?.ENABLED) {
|
||||||
|
clientConfig.sasl = {
|
||||||
|
mechanism: (kafkaConfig.SASL.MECHANISM as any) || 'plain',
|
||||||
|
username: kafkaConfig.SASL.USERNAME,
|
||||||
|
password: kafkaConfig.SASL.PASSWORD,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add SSL configuration if enabled
|
||||||
|
if (kafkaConfig.SSL?.ENABLED) {
|
||||||
|
clientConfig.ssl = {
|
||||||
|
rejectUnauthorized: kafkaConfig.SSL.REJECT_UNAUTHORIZED !== false,
|
||||||
|
ca: kafkaConfig.SSL.CA ? [kafkaConfig.SSL.CA] : undefined,
|
||||||
|
key: kafkaConfig.SSL.KEY,
|
||||||
|
cert: kafkaConfig.SSL.CERT,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
this.kafkaClient = new KafkaJS(clientConfig);
|
||||||
|
|
||||||
|
// Initialize producer
|
||||||
|
const producerConfig: ProducerConfig = {
|
||||||
|
maxInFlightRequests: 1,
|
||||||
|
idempotent: true,
|
||||||
|
transactionTimeout: 30000,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.producer = this.kafkaClient.producer(producerConfig);
|
||||||
|
await this.producer.connect();
|
||||||
|
|
||||||
|
// Initialize consumer for global events if enabled
|
||||||
|
if (kafkaConfig.GLOBAL_ENABLED) {
|
||||||
|
await this.initGlobalConsumer();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.reconnectAttempts = 0;
|
||||||
|
this.isReconnecting = false;
|
||||||
|
|
||||||
|
this.logger.info('Kafka initialized successfully');
|
||||||
|
|
||||||
|
// Create topics if they don't exist
|
||||||
|
if (kafkaConfig.AUTO_CREATE_TOPICS) {
|
||||||
|
await this.createTopics();
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error({
|
||||||
|
local: 'KafkaController.connect',
|
||||||
|
message: 'Failed to connect to Kafka',
|
||||||
|
error: error.message || error,
|
||||||
|
});
|
||||||
|
this.scheduleReconnect();
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async initGlobalConsumer(): Promise<void> {
|
||||||
|
try {
|
||||||
|
const kafkaConfig = configService.get<Kafka>('KAFKA');
|
||||||
|
|
||||||
|
const consumerConfig: ConsumerConfig = {
|
||||||
|
groupId: kafkaConfig.CONSUMER_GROUP_ID || 'evolution-api-consumers',
|
||||||
|
sessionTimeout: 30000,
|
||||||
|
heartbeatInterval: 3000,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.consumer = this.kafkaClient.consumer(consumerConfig);
|
||||||
|
await this.consumer.connect();
|
||||||
|
|
||||||
|
// Subscribe to global topics
|
||||||
|
const events = kafkaConfig.EVENTS;
|
||||||
|
if (events) {
|
||||||
|
const eventKeys = Object.keys(events).filter((event) => events[event]);
|
||||||
|
|
||||||
|
for (const event of eventKeys) {
|
||||||
|
const topicName = this.getTopicName(event, true);
|
||||||
|
await this.consumer.subscribe({ topic: topicName });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start consuming messages
|
||||||
|
await this.consumer.run({
|
||||||
|
eachMessage: async ({ topic, message }) => {
|
||||||
|
try {
|
||||||
|
const data = JSON.parse(message.value?.toString() || '{}');
|
||||||
|
this.logger.debug(`Received message from topic ${topic}: ${JSON.stringify(data)}`);
|
||||||
|
|
||||||
|
// Process the message here if needed
|
||||||
|
// This is where you can add custom message processing logic
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(`Error processing message from topic ${topic}: ${error}`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
this.logger.info('Global Kafka consumer initialized');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(`Failed to initialize global Kafka consumer: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async createTopics(): Promise<void> {
|
||||||
|
try {
|
||||||
|
const kafkaConfig = configService.get<Kafka>('KAFKA');
|
||||||
|
const admin = this.kafkaClient.admin();
|
||||||
|
await admin.connect();
|
||||||
|
|
||||||
|
const topics = [];
|
||||||
|
|
||||||
|
// Create global topics if enabled
|
||||||
|
if (kafkaConfig.GLOBAL_ENABLED && kafkaConfig.EVENTS) {
|
||||||
|
const eventKeys = Object.keys(kafkaConfig.EVENTS).filter((event) => kafkaConfig.EVENTS[event]);
|
||||||
|
|
||||||
|
for (const event of eventKeys) {
|
||||||
|
const topicName = this.getTopicName(event, true);
|
||||||
|
topics.push({
|
||||||
|
topic: topicName,
|
||||||
|
numPartitions: kafkaConfig.NUM_PARTITIONS || 1,
|
||||||
|
replicationFactor: kafkaConfig.REPLICATION_FACTOR || 1,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (topics.length > 0) {
|
||||||
|
await admin.createTopics({
|
||||||
|
topics,
|
||||||
|
waitForLeaders: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.logger.info(`Created ${topics.length} Kafka topics`);
|
||||||
|
}
|
||||||
|
|
||||||
|
await admin.disconnect();
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(`Failed to create Kafka topics: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private getTopicName(event: string, isGlobal: boolean = false, instanceName?: string): string {
|
||||||
|
const kafkaConfig = configService.get<Kafka>('KAFKA');
|
||||||
|
const prefix = kafkaConfig.TOPIC_PREFIX || 'evolution';
|
||||||
|
|
||||||
|
if (isGlobal) {
|
||||||
|
return `${prefix}.global.${event.toLowerCase().replace(/_/g, '.')}`;
|
||||||
|
} else {
|
||||||
|
return `${prefix}.${instanceName}.${event.toLowerCase().replace(/_/g, '.')}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleConnectionLoss(): void {
|
||||||
|
if (this.isReconnecting) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.cleanup();
|
||||||
|
this.scheduleReconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
private scheduleReconnect(): void {
|
||||||
|
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
||||||
|
this.logger.error(
|
||||||
|
`Maximum reconnect attempts (${this.maxReconnectAttempts}) reached. Stopping reconnection attempts.`,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.isReconnecting) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.isReconnecting = true;
|
||||||
|
this.reconnectAttempts++;
|
||||||
|
|
||||||
|
const delay = this.reconnectDelay * Math.pow(2, Math.min(this.reconnectAttempts - 1, 5));
|
||||||
|
|
||||||
|
this.logger.info(
|
||||||
|
`Scheduling Kafka reconnection attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts} in ${delay}ms`,
|
||||||
|
);
|
||||||
|
|
||||||
|
setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
this.logger.info(
|
||||||
|
`Attempting to reconnect to Kafka (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts})`,
|
||||||
|
);
|
||||||
|
await this.connect();
|
||||||
|
this.logger.info('Successfully reconnected to Kafka');
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error({
|
||||||
|
local: 'KafkaController.scheduleReconnect',
|
||||||
|
message: `Reconnection attempt ${this.reconnectAttempts} failed`,
|
||||||
|
error: error.message || error,
|
||||||
|
});
|
||||||
|
this.isReconnecting = false;
|
||||||
|
this.scheduleReconnect();
|
||||||
|
}
|
||||||
|
}, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async ensureConnection(): Promise<boolean> {
|
||||||
|
if (!this.producer) {
|
||||||
|
this.logger.warn('Kafka producer is not available, attempting to reconnect...');
|
||||||
|
if (!this.isReconnecting) {
|
||||||
|
this.scheduleReconnect();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async emit({
|
||||||
|
instanceName,
|
||||||
|
origin,
|
||||||
|
event,
|
||||||
|
data,
|
||||||
|
serverUrl,
|
||||||
|
dateTime,
|
||||||
|
sender,
|
||||||
|
apiKey,
|
||||||
|
integration,
|
||||||
|
}: EmitData): Promise<void> {
|
||||||
|
if (integration && !integration.includes('kafka')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.status) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(await this.ensureConnection())) {
|
||||||
|
this.logger.warn(`Failed to emit event ${event} for instance ${instanceName}: No Kafka connection`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const instanceKafka = await this.get(instanceName);
|
||||||
|
const kafkaLocal = instanceKafka?.events;
|
||||||
|
const kafkaGlobal = configService.get<Kafka>('KAFKA').GLOBAL_ENABLED;
|
||||||
|
const kafkaEvents = configService.get<Kafka>('KAFKA').EVENTS;
|
||||||
|
const we = event.replace(/[.-]/gm, '_').toUpperCase();
|
||||||
|
const logEnabled = configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS');
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
event,
|
||||||
|
instance: instanceName,
|
||||||
|
data,
|
||||||
|
server_url: serverUrl,
|
||||||
|
date_time: dateTime,
|
||||||
|
sender,
|
||||||
|
apikey: apiKey,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const messageValue = JSON.stringify(message);
|
||||||
|
|
||||||
|
// Instance-specific events
|
||||||
|
if (instanceKafka?.enabled && this.producer && Array.isArray(kafkaLocal) && kafkaLocal.includes(we)) {
|
||||||
|
const topicName = this.getTopicName(event, false, instanceName);
|
||||||
|
|
||||||
|
let retry = 0;
|
||||||
|
while (retry < 3) {
|
||||||
|
try {
|
||||||
|
await this.producer.send({
|
||||||
|
topic: topicName,
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
key: instanceName,
|
||||||
|
value: messageValue,
|
||||||
|
headers: {
|
||||||
|
event,
|
||||||
|
instance: instanceName,
|
||||||
|
origin,
|
||||||
|
timestamp: dateTime,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (logEnabled) {
|
||||||
|
const logData = {
|
||||||
|
local: `${origin}.sendData-Kafka`,
|
||||||
|
...message,
|
||||||
|
};
|
||||||
|
this.logger.log(logData);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error({
|
||||||
|
local: 'KafkaController.emit',
|
||||||
|
message: `Error publishing local Kafka message (attempt ${retry + 1}/3)`,
|
||||||
|
error: error.message || error,
|
||||||
|
});
|
||||||
|
retry++;
|
||||||
|
if (retry >= 3) {
|
||||||
|
this.handleConnectionLoss();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Global events
|
||||||
|
if (kafkaGlobal && kafkaEvents[we] && this.producer) {
|
||||||
|
const topicName = this.getTopicName(event, true);
|
||||||
|
|
||||||
|
let retry = 0;
|
||||||
|
while (retry < 3) {
|
||||||
|
try {
|
||||||
|
await this.producer.send({
|
||||||
|
topic: topicName,
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
key: `${instanceName}-${event}`,
|
||||||
|
value: messageValue,
|
||||||
|
headers: {
|
||||||
|
event,
|
||||||
|
instance: instanceName,
|
||||||
|
origin,
|
||||||
|
timestamp: dateTime,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (logEnabled) {
|
||||||
|
const logData = {
|
||||||
|
local: `${origin}.sendData-Kafka-Global`,
|
||||||
|
...message,
|
||||||
|
};
|
||||||
|
this.logger.log(logData);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error({
|
||||||
|
local: 'KafkaController.emit',
|
||||||
|
message: `Error publishing global Kafka message (attempt ${retry + 1}/3)`,
|
||||||
|
error: error.message || error,
|
||||||
|
});
|
||||||
|
retry++;
|
||||||
|
if (retry >= 3) {
|
||||||
|
this.handleConnectionLoss();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async cleanup(): Promise<void> {
|
||||||
|
try {
|
||||||
|
if (this.consumer) {
|
||||||
|
await this.consumer.disconnect();
|
||||||
|
this.consumer = null;
|
||||||
|
}
|
||||||
|
if (this.producer) {
|
||||||
|
await this.producer.disconnect();
|
||||||
|
this.producer = null;
|
||||||
|
}
|
||||||
|
this.kafkaClient = null;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.warn({
|
||||||
|
local: 'KafkaController.cleanup',
|
||||||
|
message: 'Error during cleanup',
|
||||||
|
error: error.message || error,
|
||||||
|
});
|
||||||
|
this.producer = null;
|
||||||
|
this.consumer = null;
|
||||||
|
this.kafkaClient = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
36
src/api/integrations/event/kafka/kafka.router.ts
Normal file
36
src/api/integrations/event/kafka/kafka.router.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import { RouterBroker } from '@api/abstract/abstract.router';
|
||||||
|
import { InstanceDto } from '@api/dto/instance.dto';
|
||||||
|
import { EventDto } from '@api/integrations/event/event.dto';
|
||||||
|
import { HttpStatus } from '@api/routes/index.router';
|
||||||
|
import { eventManager } from '@api/server.module';
|
||||||
|
import { eventSchema, instanceSchema } from '@validate/validate.schema';
|
||||||
|
import { RequestHandler, Router } from 'express';
|
||||||
|
|
||||||
|
export class KafkaRouter extends RouterBroker {
|
||||||
|
constructor(...guards: RequestHandler[]) {
|
||||||
|
super();
|
||||||
|
this.router
|
||||||
|
.post(this.routerPath('set'), ...guards, async (req, res) => {
|
||||||
|
const response = await this.dataValidate<EventDto>({
|
||||||
|
request: req,
|
||||||
|
schema: eventSchema,
|
||||||
|
ClassRef: EventDto,
|
||||||
|
execute: (instance, data) => eventManager.kafka.set(instance.instanceName, data),
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(HttpStatus.CREATED).json(response);
|
||||||
|
})
|
||||||
|
.get(this.routerPath('find'), ...guards, async (req, res) => {
|
||||||
|
const response = await this.dataValidate<InstanceDto>({
|
||||||
|
request: req,
|
||||||
|
schema: instanceSchema,
|
||||||
|
ClassRef: InstanceDto,
|
||||||
|
execute: (instance) => eventManager.kafka.get(instance.instanceName),
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(HttpStatus.OK).json(response);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly router: Router = Router();
|
||||||
|
}
|
||||||
21
src/api/integrations/event/kafka/kafka.schema.ts
Normal file
21
src/api/integrations/event/kafka/kafka.schema.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import { JSONSchema7 } from 'json-schema';
|
||||||
|
import { v4 } from 'uuid';
|
||||||
|
|
||||||
|
import { EventController } from '../event.controller';
|
||||||
|
|
||||||
|
export const kafkaSchema: JSONSchema7 = {
|
||||||
|
$id: v4(),
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
enabled: { type: 'boolean', enum: [true, false] },
|
||||||
|
events: {
|
||||||
|
type: 'array',
|
||||||
|
minItems: 0,
|
||||||
|
items: {
|
||||||
|
type: 'string',
|
||||||
|
enum: EventController.events,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
required: ['enabled'],
|
||||||
|
};
|
||||||
@@ -33,7 +33,7 @@ const bucketExists = async () => {
|
|||||||
try {
|
try {
|
||||||
const list = await minioClient.listBuckets();
|
const list = await minioClient.listBuckets();
|
||||||
return list.find((bucket) => bucket.name === bucketName);
|
return list.find((bucket) => bucket.name === bucketName);
|
||||||
} catch (error) {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -826,7 +826,7 @@ export class ChannelStartupService {
|
|||||||
const msg = message.message;
|
const msg = message.message;
|
||||||
|
|
||||||
// Se só tem messageContextInfo, não é mídia válida
|
// Se só tem messageContextInfo, não é mídia válida
|
||||||
if (Object.keys(msg).length === 1 && 'messageContextInfo' in msg) {
|
if (Object.keys(msg).length === 1 && Object.prototype.hasOwnProperty.call(msg, 'messageContextInfo')) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export class ProxyService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ export class SettingsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -153,6 +153,34 @@ export type Sqs = {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type Kafka = {
|
||||||
|
ENABLED: boolean;
|
||||||
|
CLIENT_ID: string;
|
||||||
|
BROKERS: string[];
|
||||||
|
CONNECTION_TIMEOUT: number;
|
||||||
|
REQUEST_TIMEOUT: number;
|
||||||
|
GLOBAL_ENABLED: boolean;
|
||||||
|
CONSUMER_GROUP_ID: string;
|
||||||
|
TOPIC_PREFIX: string;
|
||||||
|
NUM_PARTITIONS: number;
|
||||||
|
REPLICATION_FACTOR: number;
|
||||||
|
AUTO_CREATE_TOPICS: boolean;
|
||||||
|
EVENTS: EventsRabbitmq;
|
||||||
|
SASL?: {
|
||||||
|
ENABLED: boolean;
|
||||||
|
MECHANISM: string;
|
||||||
|
USERNAME: string;
|
||||||
|
PASSWORD: string;
|
||||||
|
};
|
||||||
|
SSL?: {
|
||||||
|
ENABLED: boolean;
|
||||||
|
REJECT_UNAUTHORIZED: boolean;
|
||||||
|
CA?: string;
|
||||||
|
KEY?: string;
|
||||||
|
CERT?: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export type Websocket = {
|
export type Websocket = {
|
||||||
ENABLED: boolean;
|
ENABLED: boolean;
|
||||||
GLOBAL_EVENTS: boolean;
|
GLOBAL_EVENTS: boolean;
|
||||||
@@ -372,6 +400,7 @@ export interface Env {
|
|||||||
RABBITMQ: Rabbitmq;
|
RABBITMQ: Rabbitmq;
|
||||||
NATS: Nats;
|
NATS: Nats;
|
||||||
SQS: Sqs;
|
SQS: Sqs;
|
||||||
|
KAFKA: Kafka;
|
||||||
WEBSOCKET: Websocket;
|
WEBSOCKET: Websocket;
|
||||||
WA_BUSINESS: WaBusiness;
|
WA_BUSINESS: WaBusiness;
|
||||||
LOG: Log;
|
LOG: Log;
|
||||||
@@ -587,6 +616,68 @@ export class ConfigService {
|
|||||||
TYPEBOT_START: process.env?.SQS_GLOBAL_TYPEBOT_START === 'true',
|
TYPEBOT_START: process.env?.SQS_GLOBAL_TYPEBOT_START === 'true',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
KAFKA: {
|
||||||
|
ENABLED: process.env?.KAFKA_ENABLED === 'true',
|
||||||
|
CLIENT_ID: process.env?.KAFKA_CLIENT_ID || 'evolution-api',
|
||||||
|
BROKERS: process.env?.KAFKA_BROKERS?.split(',') || ['localhost:9092'],
|
||||||
|
CONNECTION_TIMEOUT: Number.parseInt(process.env?.KAFKA_CONNECTION_TIMEOUT || '3000'),
|
||||||
|
REQUEST_TIMEOUT: Number.parseInt(process.env?.KAFKA_REQUEST_TIMEOUT || '30000'),
|
||||||
|
GLOBAL_ENABLED: process.env?.KAFKA_GLOBAL_ENABLED === 'true',
|
||||||
|
CONSUMER_GROUP_ID: process.env?.KAFKA_CONSUMER_GROUP_ID || 'evolution-api-consumers',
|
||||||
|
TOPIC_PREFIX: process.env?.KAFKA_TOPIC_PREFIX || 'evolution',
|
||||||
|
NUM_PARTITIONS: Number.parseInt(process.env?.KAFKA_NUM_PARTITIONS || '1'),
|
||||||
|
REPLICATION_FACTOR: Number.parseInt(process.env?.KAFKA_REPLICATION_FACTOR || '1'),
|
||||||
|
AUTO_CREATE_TOPICS: process.env?.KAFKA_AUTO_CREATE_TOPICS === 'true',
|
||||||
|
EVENTS: {
|
||||||
|
APPLICATION_STARTUP: process.env?.KAFKA_EVENTS_APPLICATION_STARTUP === 'true',
|
||||||
|
INSTANCE_CREATE: process.env?.KAFKA_EVENTS_INSTANCE_CREATE === 'true',
|
||||||
|
INSTANCE_DELETE: process.env?.KAFKA_EVENTS_INSTANCE_DELETE === 'true',
|
||||||
|
QRCODE_UPDATED: process.env?.KAFKA_EVENTS_QRCODE_UPDATED === 'true',
|
||||||
|
MESSAGES_SET: process.env?.KAFKA_EVENTS_MESSAGES_SET === 'true',
|
||||||
|
MESSAGES_UPSERT: process.env?.KAFKA_EVENTS_MESSAGES_UPSERT === 'true',
|
||||||
|
MESSAGES_EDITED: process.env?.KAFKA_EVENTS_MESSAGES_EDITED === 'true',
|
||||||
|
MESSAGES_UPDATE: process.env?.KAFKA_EVENTS_MESSAGES_UPDATE === 'true',
|
||||||
|
MESSAGES_DELETE: process.env?.KAFKA_EVENTS_MESSAGES_DELETE === 'true',
|
||||||
|
SEND_MESSAGE: process.env?.KAFKA_EVENTS_SEND_MESSAGE === 'true',
|
||||||
|
SEND_MESSAGE_UPDATE: process.env?.KAFKA_EVENTS_SEND_MESSAGE_UPDATE === 'true',
|
||||||
|
CONTACTS_SET: process.env?.KAFKA_EVENTS_CONTACTS_SET === 'true',
|
||||||
|
CONTACTS_UPSERT: process.env?.KAFKA_EVENTS_CONTACTS_UPSERT === 'true',
|
||||||
|
CONTACTS_UPDATE: process.env?.KAFKA_EVENTS_CONTACTS_UPDATE === 'true',
|
||||||
|
PRESENCE_UPDATE: process.env?.KAFKA_EVENTS_PRESENCE_UPDATE === 'true',
|
||||||
|
CHATS_SET: process.env?.KAFKA_EVENTS_CHATS_SET === 'true',
|
||||||
|
CHATS_UPSERT: process.env?.KAFKA_EVENTS_CHATS_UPSERT === 'true',
|
||||||
|
CHATS_UPDATE: process.env?.KAFKA_EVENTS_CHATS_UPDATE === 'true',
|
||||||
|
CHATS_DELETE: process.env?.KAFKA_EVENTS_CHATS_DELETE === 'true',
|
||||||
|
CONNECTION_UPDATE: process.env?.KAFKA_EVENTS_CONNECTION_UPDATE === 'true',
|
||||||
|
LABELS_EDIT: process.env?.KAFKA_EVENTS_LABELS_EDIT === 'true',
|
||||||
|
LABELS_ASSOCIATION: process.env?.KAFKA_EVENTS_LABELS_ASSOCIATION === 'true',
|
||||||
|
GROUPS_UPSERT: process.env?.KAFKA_EVENTS_GROUPS_UPSERT === 'true',
|
||||||
|
GROUP_UPDATE: process.env?.KAFKA_EVENTS_GROUPS_UPDATE === 'true',
|
||||||
|
GROUP_PARTICIPANTS_UPDATE: process.env?.KAFKA_EVENTS_GROUP_PARTICIPANTS_UPDATE === 'true',
|
||||||
|
CALL: process.env?.KAFKA_EVENTS_CALL === 'true',
|
||||||
|
TYPEBOT_START: process.env?.KAFKA_EVENTS_TYPEBOT_START === 'true',
|
||||||
|
TYPEBOT_CHANGE_STATUS: process.env?.KAFKA_EVENTS_TYPEBOT_CHANGE_STATUS === 'true',
|
||||||
|
},
|
||||||
|
SASL:
|
||||||
|
process.env?.KAFKA_SASL_ENABLED === 'true'
|
||||||
|
? {
|
||||||
|
ENABLED: true,
|
||||||
|
MECHANISM: process.env?.KAFKA_SASL_MECHANISM || 'plain',
|
||||||
|
USERNAME: process.env?.KAFKA_SASL_USERNAME || '',
|
||||||
|
PASSWORD: process.env?.KAFKA_SASL_PASSWORD || '',
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
SSL:
|
||||||
|
process.env?.KAFKA_SSL_ENABLED === 'true'
|
||||||
|
? {
|
||||||
|
ENABLED: true,
|
||||||
|
REJECT_UNAUTHORIZED: process.env?.KAFKA_SSL_REJECT_UNAUTHORIZED !== 'false',
|
||||||
|
CA: process.env?.KAFKA_SSL_CA,
|
||||||
|
KEY: process.env?.KAFKA_SSL_KEY,
|
||||||
|
CERT: process.env?.KAFKA_SSL_CERT,
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
},
|
||||||
WEBSOCKET: {
|
WEBSOCKET: {
|
||||||
ENABLED: process.env?.WEBSOCKET_ENABLED === 'true',
|
ENABLED: process.env?.WEBSOCKET_ENABLED === 'true',
|
||||||
GLOBAL_EVENTS: process.env?.WEBSOCKET_GLOBAL_EVENTS === 'true',
|
GLOBAL_EVENTS: process.env?.WEBSOCKET_GLOBAL_EVENTS === 'true',
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ import fs from 'fs';
|
|||||||
import i18next from 'i18next';
|
import i18next from 'i18next';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
const __dirname = path.resolve(process.cwd(), 'src', 'utils');
|
|
||||||
|
|
||||||
const languages = ['en', 'pt-BR', 'es'];
|
const languages = ['en', 'pt-BR', 'es'];
|
||||||
const translationsPath = path.join(__dirname, 'translations');
|
const translationsPath = path.join(__dirname, 'translations');
|
||||||
const configService: ConfigService = new ConfigService();
|
const configService: ConfigService = new ConfigService();
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import { prismaRepository } from '@api/server.module';
|
import { prismaRepository } from '@api/server.module';
|
||||||
import { configService, Database } from '@config/env.config';
|
import { configService, Database } from '@config/env.config';
|
||||||
|
import { Logger } from '@config/logger.config';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
|
|
||||||
|
const logger = new Logger('OnWhatsappCache');
|
||||||
|
|
||||||
function getAvailableNumbers(remoteJid: string) {
|
function getAvailableNumbers(remoteJid: string) {
|
||||||
const numbersAvailable: string[] = [];
|
const numbersAvailable: string[] = [];
|
||||||
|
|
||||||
@@ -11,6 +14,11 @@ function getAvailableNumbers(remoteJid: string) {
|
|||||||
|
|
||||||
const [number, domain] = remoteJid.split('@');
|
const [number, domain] = remoteJid.split('@');
|
||||||
|
|
||||||
|
// TODO: Se já for @lid, retornar apenas ele mesmo SEM adicionar @domain novamente
|
||||||
|
if (domain === 'lid' || domain === 'g.us') {
|
||||||
|
return [remoteJid]; // Retorna direto para @lid e @g.us
|
||||||
|
}
|
||||||
|
|
||||||
// Brazilian numbers
|
// Brazilian numbers
|
||||||
if (remoteJid.startsWith('55')) {
|
if (remoteJid.startsWith('55')) {
|
||||||
const numberWithDigit =
|
const numberWithDigit =
|
||||||
@@ -47,35 +55,87 @@ function getAvailableNumbers(remoteJid: string) {
|
|||||||
numbersAvailable.push(remoteJid);
|
numbersAvailable.push(remoteJid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Adiciona @domain apenas para números que não são @lid
|
||||||
return numbersAvailable.map((number) => `${number}@${domain}`);
|
return numbersAvailable.map((number) => `${number}@${domain}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ISaveOnWhatsappCacheParams {
|
interface ISaveOnWhatsappCacheParams {
|
||||||
remoteJid: string;
|
remoteJid: string;
|
||||||
lid?: string;
|
remoteJidAlt?: string;
|
||||||
|
lid?: 'lid' | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
|
export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
|
||||||
if (configService.get<Database>('DATABASE').SAVE_DATA.IS_ON_WHATSAPP) {
|
if (configService.get<Database>('DATABASE').SAVE_DATA.IS_ON_WHATSAPP) {
|
||||||
const upsertsQuery = data.map((item) => {
|
for (const item of data) {
|
||||||
const remoteJid = item.remoteJid.startsWith('+') ? item.remoteJid.slice(1) : item.remoteJid;
|
const remoteJid = item.remoteJid.startsWith('+') ? item.remoteJid.slice(1) : item.remoteJid;
|
||||||
const numbersAvailable = getAvailableNumbers(remoteJid);
|
|
||||||
|
|
||||||
return prismaRepository.isOnWhatsapp.upsert({
|
// TODO: Buscar registro existente PRIMEIRO para preservar dados
|
||||||
create: {
|
const allJids = [remoteJid];
|
||||||
remoteJid: remoteJid,
|
|
||||||
jidOptions: numbersAvailable.join(','),
|
const altJid =
|
||||||
lid: item.lid,
|
item.remoteJidAlt && item.remoteJidAlt.includes('@lid')
|
||||||
|
? item.remoteJidAlt.startsWith('+')
|
||||||
|
? item.remoteJidAlt.slice(1)
|
||||||
|
: item.remoteJidAlt
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (altJid) {
|
||||||
|
allJids.push(altJid);
|
||||||
|
}
|
||||||
|
|
||||||
|
const expandedJids = allJids.flatMap((jid) => getAvailableNumbers(jid));
|
||||||
|
|
||||||
|
const existingRecord = await prismaRepository.isOnWhatsapp.findFirst({
|
||||||
|
where: {
|
||||||
|
OR: expandedJids.map((jid) => ({ jidOptions: { contains: jid } })),
|
||||||
},
|
},
|
||||||
update: {
|
|
||||||
jidOptions: numbersAvailable.join(','),
|
|
||||||
lid: item.lid,
|
|
||||||
},
|
|
||||||
where: { remoteJid: remoteJid },
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
await prismaRepository.$transaction(upsertsQuery);
|
logger.verbose(`Register exists: ${existingRecord ? existingRecord.remoteJid : 'não not found'}`);
|
||||||
|
|
||||||
|
const finalJidOptions = [...expandedJids];
|
||||||
|
|
||||||
|
if (existingRecord?.jidOptions) {
|
||||||
|
const existingJids = existingRecord.jidOptions.split(',');
|
||||||
|
// TODO: Adicionar JIDs existentes que não estão na lista atual
|
||||||
|
existingJids.forEach((jid) => {
|
||||||
|
if (!finalJidOptions.includes(jid)) {
|
||||||
|
finalJidOptions.push(jid);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Se tiver remoteJidAlt com @lid novo, adicionar
|
||||||
|
if (altJid && !finalJidOptions.includes(altJid)) {
|
||||||
|
finalJidOptions.push(altJid);
|
||||||
|
}
|
||||||
|
|
||||||
|
const uniqueNumbers = Array.from(new Set(finalJidOptions));
|
||||||
|
|
||||||
|
logger.verbose(
|
||||||
|
`Saving: remoteJid=${remoteJid}, jidOptions=${uniqueNumbers.join(',')}, lid=${item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (existingRecord) {
|
||||||
|
await prismaRepository.isOnWhatsapp.update({
|
||||||
|
where: { id: existingRecord.id },
|
||||||
|
data: {
|
||||||
|
remoteJid: remoteJid,
|
||||||
|
jidOptions: uniqueNumbers.join(','),
|
||||||
|
lid: item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await prismaRepository.isOnWhatsapp.create({
|
||||||
|
data: {
|
||||||
|
remoteJid: remoteJid,
|
||||||
|
jidOptions: uniqueNumbers.join(','),
|
||||||
|
lid: item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export async function keyExists(sessionId: string): Promise<any> {
|
|||||||
try {
|
try {
|
||||||
const key = await prismaRepository.session.findUnique({ where: { sessionId: sessionId } });
|
const key = await prismaRepository.session.findUnique({ where: { sessionId: sessionId } });
|
||||||
return !!key;
|
return !!key;
|
||||||
} catch (error) {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,7 +38,7 @@ export async function saveKey(sessionId: string, keyJson: any): Promise<any> {
|
|||||||
where: { sessionId: sessionId },
|
where: { sessionId: sessionId },
|
||||||
data: { creds: JSON.stringify(keyJson) },
|
data: { creds: JSON.stringify(keyJson) },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,7 @@ export async function getAuthKey(sessionId: string): Promise<any> {
|
|||||||
if (!register) return null;
|
if (!register) return null;
|
||||||
const auth = await prismaRepository.session.findUnique({ where: { sessionId: sessionId } });
|
const auth = await prismaRepository.session.findUnique({ where: { sessionId: sessionId } });
|
||||||
return JSON.parse(auth?.creds);
|
return JSON.parse(auth?.creds);
|
||||||
} catch (error) {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,7 +59,7 @@ async function deleteAuthKey(sessionId: string): Promise<any> {
|
|||||||
const register = await keyExists(sessionId);
|
const register = await keyExists(sessionId);
|
||||||
if (!register) return;
|
if (!register) return;
|
||||||
await prismaRepository.session.delete({ where: { sessionId: sessionId } });
|
await prismaRepository.session.delete({ where: { sessionId: sessionId } });
|
||||||
} catch (error) {
|
} catch {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -68,7 +68,7 @@ async function fileExists(file: string): Promise<any> {
|
|||||||
try {
|
try {
|
||||||
const stat = await fs.stat(file);
|
const stat = await fs.stat(file);
|
||||||
if (stat.isFile()) return true;
|
if (stat.isFile()) return true;
|
||||||
} catch (error) {
|
} catch {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,7 +119,7 @@ export default async function useMultiFileAuthStatePrisma(
|
|||||||
|
|
||||||
const parsedData = JSON.parse(rawData, BufferJSON.reviver);
|
const parsedData = JSON.parse(rawData, BufferJSON.reviver);
|
||||||
return parsedData;
|
return parsedData;
|
||||||
} catch (error) {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -137,7 +137,7 @@ export default async function useMultiFileAuthStatePrisma(
|
|||||||
} else {
|
} else {
|
||||||
await deleteAuthKey(sessionId);
|
await deleteAuthKey(sessionId);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user