FIX: Corrige nome de arquivo em mídias enviadas e recebidas (Bug 2 e 3)
- Passa file.originalname ao sendMediaMessage no controller - Adiciona filename ao payload da Meta API para documentos - Usa filename como bodyText quando não há legenda - Emite mediaFilename via gateway Socket.IO - Persiste media_filename no banco (migration 028) - Adiciona mediaFilename ao repositório de mensagens
This commit is contained in:
parent
980b36fadd
commit
812bedf363
@ -16,12 +16,13 @@ export class MensagensRepository {
|
|||||||
timestamp: number;
|
timestamp: number;
|
||||||
mediaPath?: string;
|
mediaPath?: string;
|
||||||
mediaMimeType?: string;
|
mediaMimeType?: string;
|
||||||
|
mediaFilename?: string;
|
||||||
}) {
|
}) {
|
||||||
await this.database.query(
|
await this.database.query(
|
||||||
`INSERT INTO mensagens (wamid, chat_id, from_me, type, body, sender_name, status, timestamp, media_path, media_mime_type)
|
`INSERT INTO mensagens (wamid, chat_id, from_me, type, body, sender_name, status, timestamp, media_path, media_mime_type, media_filename)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||||||
ON CONFLICT (wamid) DO NOTHING`,
|
ON CONFLICT (wamid) DO NOTHING`,
|
||||||
[msg.wamid, msg.chatId, msg.fromMe, msg.type, msg.body ?? null, msg.senderName ?? null, msg.status ?? null, msg.timestamp, msg.mediaPath ?? null, msg.mediaMimeType ?? null],
|
[msg.wamid, msg.chatId, msg.fromMe, msg.type, msg.body ?? null, msg.senderName ?? null, msg.status ?? null, msg.timestamp, msg.mediaPath ?? null, msg.mediaMimeType ?? null, msg.mediaFilename ?? null],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -114,7 +114,7 @@ export class WhatsappController {
|
|||||||
throw new BadRequestException(`Arquivo muito grande. Limite para ${mediaType}: ${limitMB} MB.`);
|
throw new BadRequestException(`Arquivo muito grande. Limite para ${mediaType}: ${limitMB} MB.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.whatsappService.sendMediaMessage(to, file.buffer, file.mimetype, senderName, caption);
|
return this.whatsappService.sendMediaMessage(to, file.buffer, file.mimetype, senderName, caption, file.originalname);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation({ summary: 'Inicia atendimento ativo' })
|
@ApiOperation({ summary: 'Inicia atendimento ativo' })
|
||||||
|
|||||||
@ -106,6 +106,7 @@ export class WhatsappService {
|
|||||||
mimeType: string,
|
mimeType: string,
|
||||||
senderName?: string,
|
senderName?: string,
|
||||||
caption?: string,
|
caption?: string,
|
||||||
|
filename?: string,
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const { access_token: token, phone_number_id: phoneNumberId, api_version: apiVersion } =
|
const { access_token: token, phone_number_id: phoneNumberId, api_version: apiVersion } =
|
||||||
await this.configService.getConfig();
|
await this.configService.getConfig();
|
||||||
@ -136,6 +137,7 @@ export class WhatsappService {
|
|||||||
|
|
||||||
const mediaPayload: any = { id: mediaId };
|
const mediaPayload: any = { id: mediaId };
|
||||||
if (caption && mediaType !== 'audio') mediaPayload.caption = senderName ? `*Atendente: ${senderName}*\n\n${caption}` : caption;
|
if (caption && mediaType !== 'audio') mediaPayload.caption = senderName ? `*Atendente: ${senderName}*\n\n${caption}` : caption;
|
||||||
|
if (mediaType === 'document' && filename) mediaPayload.filename = filename;
|
||||||
|
|
||||||
const messageResponse = await fetch(
|
const messageResponse = await fetch(
|
||||||
`https://graph.facebook.com/${apiVersion}/${phoneNumberId}/messages`,
|
`https://graph.facebook.com/${apiVersion}/${phoneNumberId}/messages`,
|
||||||
@ -162,7 +164,9 @@ export class WhatsappService {
|
|||||||
this.logger.log(`[Meta] Mídia (${mediaType}) enviada para ${to}: ${wamid}`);
|
this.logger.log(`[Meta] Mídia (${mediaType}) enviada para ${to}: ${wamid}`);
|
||||||
|
|
||||||
const senderLabel = senderName ?? 'Sistema';
|
const senderLabel = senderName ?? 'Sistema';
|
||||||
const bodyText = caption ? (senderName ? `*Atendente: ${senderLabel}*\n\n${caption}` : caption) : `[${mediaType}]`;
|
const bodyText = caption
|
||||||
|
? (senderName ? `*Atendente: ${senderLabel}*\n\n${caption}` : caption)
|
||||||
|
: (filename || `[${mediaType}]`);
|
||||||
const timestamp = Math.floor(Date.now() / 1000);
|
const timestamp = Math.floor(Date.now() / 1000);
|
||||||
|
|
||||||
if (wamid) {
|
if (wamid) {
|
||||||
@ -177,6 +181,7 @@ export class WhatsappService {
|
|||||||
timestamp,
|
timestamp,
|
||||||
mediaPath,
|
mediaPath,
|
||||||
mediaMimeType: mimeType,
|
mediaMimeType: mimeType,
|
||||||
|
mediaFilename: filename,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,6 +196,7 @@ export class WhatsappService {
|
|||||||
hasMedia: true,
|
hasMedia: true,
|
||||||
mediaUrl: this.mediaStorageService.getUrl(mediaPath),
|
mediaUrl: this.mediaStorageService.getUrl(mediaPath),
|
||||||
mediaMimeType: mimeType,
|
mediaMimeType: mimeType,
|
||||||
|
mediaFilename: filename ?? null,
|
||||||
});
|
});
|
||||||
|
|
||||||
return messageResult;
|
return messageResult;
|
||||||
@ -437,6 +443,8 @@ export class WhatsappService {
|
|||||||
let mediaPath: string | undefined;
|
let mediaPath: string | undefined;
|
||||||
let mediaMimeType: string | undefined;
|
let mediaMimeType: string | undefined;
|
||||||
|
|
||||||
|
let mediaFilename: string | undefined;
|
||||||
|
|
||||||
if ((MEDIA_TYPES as readonly string[]).includes(messageType)) {
|
if ((MEDIA_TYPES as readonly string[]).includes(messageType)) {
|
||||||
const mediaObj = message[messageType];
|
const mediaObj = message[messageType];
|
||||||
if (mediaObj?.id) {
|
if (mediaObj?.id) {
|
||||||
@ -444,6 +452,7 @@ export class WhatsappService {
|
|||||||
const { buffer, mimeType } = await this.downloadMetaMedia(mediaObj.id, mediaObj.mime_type);
|
const { buffer, mimeType } = await this.downloadMetaMedia(mediaObj.id, mediaObj.mime_type);
|
||||||
mediaPath = await this.mediaStorageService.upload(buffer, mimeType);
|
mediaPath = await this.mediaStorageService.upload(buffer, mimeType);
|
||||||
mediaMimeType = mimeType;
|
mediaMimeType = mimeType;
|
||||||
|
mediaFilename = mediaObj.filename || undefined;
|
||||||
this.logger.log(`[Meta] Mídia salva: ${mediaPath}`);
|
this.logger.log(`[Meta] Mídia salva: ${mediaPath}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error(`[Meta] Falha ao baixar mídia ${mediaObj.id} de ${from}:`, err);
|
this.logger.error(`[Meta] Falha ao baixar mídia ${mediaObj.id} de ${from}:`, err);
|
||||||
@ -451,16 +460,19 @@ export class WhatsappService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const bodyToSave = messageBody || mediaFilename || undefined;
|
||||||
|
|
||||||
await this.mensagensRepository.save({
|
await this.mensagensRepository.save({
|
||||||
wamid: messageId,
|
wamid: messageId,
|
||||||
chatId: from,
|
chatId: from,
|
||||||
fromMe: false,
|
fromMe: false,
|
||||||
type: messageType,
|
type: messageType,
|
||||||
body: messageBody || undefined,
|
body: bodyToSave,
|
||||||
senderName: contactName,
|
senderName: contactName,
|
||||||
timestamp,
|
timestamp,
|
||||||
mediaPath,
|
mediaPath,
|
||||||
mediaMimeType,
|
mediaMimeType,
|
||||||
|
mediaFilename,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.gateway.emitNewMessage({
|
this.gateway.emitNewMessage({
|
||||||
@ -474,6 +486,7 @@ export class WhatsappService {
|
|||||||
hasMedia: !!mediaPath,
|
hasMedia: !!mediaPath,
|
||||||
mediaUrl: mediaPath ? this.mediaStorageService.getUrl(mediaPath) : null,
|
mediaUrl: mediaPath ? this.mediaStorageService.getUrl(mediaPath) : null,
|
||||||
mediaMimeType: mediaMimeType ?? null,
|
mediaMimeType: mediaMimeType ?? null,
|
||||||
|
mediaFilename: mediaFilename ?? null,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (messageBody || mediaPath) {
|
if (messageBody || mediaPath) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user