Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .github/workflows/publish_ghcr_image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Build and Publish GHCR image

on:
push:
branches:
- main
tags:
- "v*"
workflow_dispatch:

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository_owner }}/evolution-api

jobs:
build-and-push:
name: Build and Push GHCR
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout
uses: actions/checkout@v5
with:
submodules: recursive

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=ref,event=branch
type=ref,event=tag
type=sha
- name: Build and push
id: build-and-push
uses: docker/build-push-action@v6
with:
context: .
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Image digest
run: echo ${{ steps.build-and-push.outputs.digest }}
124 changes: 80 additions & 44 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,10 @@ export class BaileysStartupService extends ChannelStartupService {
}
}

private getUpsertEmittedCacheKey(messageId: string) {
return `upsert_emitted_${this.instanceId}_${messageId}`;
}

private async defineAuthState() {
const db = this.configService.get<Database>('DATABASE');
const cache = this.configService.get<CacheConf>('CACHE');
Expand Down Expand Up @@ -1389,50 +1393,49 @@ export class BaileysStartupService extends ChannelStartupService {
try {
if (isVideo && !this.configService.get<S3>('S3').SAVE_VIDEO) {
this.logger.warn('Video upload is disabled. Skipping video upload.');
// Skip video upload by returning early from this block
return;
}

const message: any = received;

// Verificação adicional para garantir que há conteúdo de mídia real
const hasRealMedia = this.hasValidMediaContent(message);

if (!hasRealMedia) {
this.logger.warn('Message detected as media but contains no valid media content');
} else {
const media = await this.getBase64FromMediaMessage({ message }, true);

if (!media) {
this.logger.verbose('No valid media to upload (messageContextInfo only), skipping MinIO');
return;
const message: any = received;

// Verificação adicional para garantir que há conteúdo de mídia real
const hasRealMedia = this.hasValidMediaContent(message);

if (!hasRealMedia) {
this.logger.warn('Message detected as media but contains no valid media content');
} else {
const media = await this.getBase64FromMediaMessage({ message }, true);

if (!media) {
this.logger.verbose('No valid media to upload (messageContextInfo only), skipping MinIO');
} else {
const { buffer, mediaType, fileName, size } = media;
const mimetype = mimeTypes.lookup(fileName).toString();
const fullName = join(
`${this.instance.id}`,
received.key.remoteJid,
mediaType,
`${Date.now()}_${fileName}`,
);
await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, {
'Content-Type': mimetype,
});

await this.prismaRepository.media.create({
data: {
messageId: msg.id,
instanceId: this.instanceId,
type: mediaType,
fileName: fullName,
mimetype,
},
});

const mediaUrl = await s3Service.getObjectUrl(fullName);

messageRaw.message.mediaUrl = mediaUrl;

await this.prismaRepository.message.update({ where: { id: msg.id }, data: messageRaw });
}
}

const { buffer, mediaType, fileName, size } = media;
const mimetype = mimeTypes.lookup(fileName).toString();
const fullName = join(
`${this.instance.id}`,
received.key.remoteJid,
mediaType,
`${Date.now()}_${fileName}`,
);
await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, { 'Content-Type': mimetype });

await this.prismaRepository.media.create({
data: {
messageId: msg.id,
instanceId: this.instanceId,
type: mediaType,
fileName: fullName,
mimetype,
},
});

const mediaUrl = await s3Service.getObjectUrl(fullName);

messageRaw.message.mediaUrl = mediaUrl;

await this.prismaRepository.message.update({ where: { id: msg.id }, data: messageRaw });
}
} catch (error) {
this.logger.error(['Error on upload file to minio', error?.message, error?.stack]);
Expand Down Expand Up @@ -1478,9 +1481,11 @@ export class BaileysStartupService extends ChannelStartupService {
if (messageRaw.key.remoteJid?.includes('@lid') && messageRaw.key.remoteJidAlt) {
messageRaw.key.remoteJid = messageRaw.key.remoteJidAlt;
}
console.log(messageRaw);
await this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);

this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
if (messageRaw.messageType === 'audioMessage' && !messageRaw.key.fromMe && messageRaw.key.id) {
await this.baileysCache.set(this.getUpsertEmittedCacheKey(messageRaw.key.id), true, 60 * 10);
}

await chatbotController.emit({
instance: { instanceName: this.instance.name, instanceId: this.instanceId },
Expand Down Expand Up @@ -1649,6 +1654,37 @@ export class BaileysStartupService extends ChannelStartupService {
this.logger.warn(`Original message not found for update. Skipping. Key: ${JSON.stringify(key)}`);
continue;
}

if (!key.fromMe && findMessage.messageType === 'audioMessage' && key.id) {
const upsertCacheKey = this.getUpsertEmittedCacheKey(key.id);
const alreadyEmitted = await this.baileysCache.get(upsertCacheKey);

if (!alreadyEmitted) {
const fallbackUpsertPayload = {
key: findMessage.key,
pushName: findMessage.pushName,
status: findMessage.status,
message: findMessage.message,
contextInfo: findMessage.contextInfo,
messageType: findMessage.messageType,
messageTimestamp: findMessage.messageTimestamp,
instanceId: findMessage.instanceId,
source: findMessage.source,
};

try {
await this.sendDataWebhook(Events.MESSAGES_UPSERT, fallbackUpsertPayload);
await this.baileysCache.set(upsertCacheKey, true, 60 * 10);
this.logger.warn(`Fallback messages.upsert emitted for audio message ${key.id}`);
} catch (error) {
this.logger.error([
`Failed to emit fallback messages.upsert for audio message ${key.id}`,
error?.message,
]);
}
}
}

message.messageId = findMessage.id;
}

Expand Down
Loading