| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import axios from "axios";
- import axiosRetry from "axios-retry";
- import { config } from "../config/index.js";
- function client() {
- const instance = axios.create({
- baseURL: `https://graph.facebook.com/${config.whatsapp.apiVersion}`,
- timeout: 15_000,
- headers: { Authorization: `Bearer ${config.whatsapp.token}` }
- });
- axiosRetry(instance, {
- retries: 2,
- retryDelay: axiosRetry.exponentialDelay,
-
- retryCondition: (error) => !error.response && (error.code === "ECONNABORTED" || axiosRetry.isNetworkError(error))
- });
- return instance;
- }
- export async function enviarTexto(paraTelefone, texto) {
- const { data } = await client().post(`/${config.whatsapp.phoneNumberId}/messages`, {
- messaging_product: "whatsapp",
- recipient_type: "individual",
- to: paraTelefone,
- type: "text",
- text: { preview_url: false, body: texto }
- });
- return data;
- }
- export async function obterUrlMidia(mediaId) {
- const { data } = await client().get(`/${mediaId}`);
- return data;
- }
- export async function baixarBytesMidia(url) {
-
- const { data, headers } = await client().get(url, { responseType: "arraybuffer" });
- return { buffer: Buffer.from(data), mimetype: headers["content-type"] ?? null };
- }
- export async function obterInfoNumero() {
- const { data } = await client().get(`/${config.whatsapp.phoneNumberId}`, {
- params: { fields: "verified_name,display_phone_number,quality_rating" }
- });
- return data;
- }
|