FEAT: Implementação do envio de mensagens via Meta Graph API
Some checks are pending
Deploy Dev / deploy (push) Waiting to run
Some checks are pending
Deploy Dev / deploy (push) Waiting to run
- Implementação do método sendMessage para enviar mensagens via Meta Graph API. - Atualização do método reply para utilizar o sendMessage ao invés de apenas logar a mensagem
This commit is contained in:
parent
cfd6f2deae
commit
33e06669a0
@ -84,8 +84,45 @@ export class WhatsappService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendMessage(to: string, message: string, _media?: { data: string; mimetype: string; filename?: string }, _senderName?: string) {
|
async sendMessage(to: string, message: string, _media?: { data: string; mimetype: string; filename?: string }, senderName?: string) {
|
||||||
throw new NotImplementedException(`Envio via Meta API não implementado ainda. Destinatário: ${to}, mensagem: "${message}"`);
|
const token = process.env.META_ACCESS_TOKEN;
|
||||||
|
const phoneNumberId = process.env.META_PHONE_NUMBER_ID;
|
||||||
|
const apiVersion = process.env.META_API_VERSION || 'v25.0';
|
||||||
|
|
||||||
|
if (!token || !phoneNumberId) {
|
||||||
|
throw new Error('META_ACCESS_TOKEN e META_PHONE_NUMBER_ID são obrigatórios.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = senderName ? `*Atendente: ${senderName}*\n\n${message}` : message;
|
||||||
|
|
||||||
|
const response = await fetch(
|
||||||
|
`https://graph.facebook.com/${apiVersion}/${phoneNumberId}/messages`,
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
messaging_product: 'whatsapp',
|
||||||
|
recipient_type: 'individual',
|
||||||
|
to,
|
||||||
|
type: 'text',
|
||||||
|
text: { preview_url: false, body },
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({}));
|
||||||
|
this.logger.error(`[Meta] Falha ao enviar mensagem para ${to}:`, error);
|
||||||
|
throw new Error(`Meta API erro ${response.status}: ${JSON.stringify(error)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await response.json();
|
||||||
|
this.logger.log(`[Meta] Mensagem enviada para ${to}: ${(result as any)?.messages?.[0]?.id}`);
|
||||||
|
await this.assignmentService.clearTransferNote(to);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async startAttendance(
|
async startAttendance(
|
||||||
@ -189,8 +226,7 @@ export class WhatsappService {
|
|||||||
|
|
||||||
const fakeMsg = {
|
const fakeMsg = {
|
||||||
reply: async (text: string) => {
|
reply: async (text: string) => {
|
||||||
// TODO Fase 2: enviar via Meta Graph API
|
await this.sendMessage(from, text);
|
||||||
this.logger.warn(`[Meta] Bot reply pendente de implementação. Mensagem para ${from}: "${text}"`);
|
|
||||||
},
|
},
|
||||||
_data: { notifyName: contactName },
|
_data: { notifyName: contactName },
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user