UNPKG

@baileys-md/baileys

Version:

Baileys WhatsApp API

81 lines 2.91 kB
//=======================================================// import { extractImageThumb, getHttpStream } from "./messages-media.js"; import { prepareWAMessageMedia } from "./messages.js"; //=======================================================// const THUMBNAIL_WIDTH_PX = 192; //=======================================================// const getCompressedJpegThumbnail = async (url, { thumbnailWidth, fetchOpts }) => { const stream = await getHttpStream(url, fetchOpts); const result = await extractImageThumb(stream, thumbnailWidth); return result; }; //=======================================================// export const getUrlInfo = async (text, opts = { thumbnailWidth: THUMBNAIL_WIDTH_PX, fetchOpts: { timeout: 3000 } }) => { try { const retries = 0; const maxRetry = 5; const { getLinkPreview } = await import("link-preview-js"); let previewLink = text; if (!text.startsWith("https://") && !text.startsWith("http://")) { previewLink = "https://" + previewLink; } const info = await getLinkPreview(previewLink, { ...opts.fetchOpts, followRedirects: "follow", handleRedirects: (baseURL, forwardedURL) => { const urlObj = new URL(baseURL); const forwardedURLObj = new URL(forwardedURL); if (retries >= maxRetry) { return false; } if (forwardedURLObj.hostname === urlObj.hostname || forwardedURLObj.hostname === "www." + urlObj.hostname || "www." + forwardedURLObj.hostname === urlObj.hostname) { retries + 1; return true; } else { return false; } }, headers: opts.fetchOpts?.headers }); if (info && "title" in info && info.title) { const [image] = info.images; const urlInfo = { "canonical-url": info.url, "matched-text": text, title: info.title, description: info.description, originalThumbnailUrl: image }; if (opts.uploadImage) { const { imageMessage } = await prepareWAMessageMedia({ image: { url: image } }, { upload: opts.uploadImage, mediaTypeOverride: "thumbnail-link", options: opts.fetchOpts }); urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail ? Buffer.from(imageMessage.jpegThumbnail) : undefined; urlInfo.highQualityThumbnail = imageMessage || undefined; } else { try { urlInfo.jpegThumbnail = image ? (await getCompressedJpegThumbnail(image, opts)).buffer : undefined; } catch (error) { opts.logger?.debug({ err: error.stack, url: previewLink }, "error in generating thumbnail"); } } return urlInfo; } } catch (error) { if (!error.message.includes("receive a valid")) { throw error; } } }; //=======================================================//