mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Preview link fix (#1548)
* Preview link fix * Preview link refactor * Deleted empty line * Redundant unnecessary changes * Added checker for http/https prefix in link Co-authored-by: Ilya Borodin <ilya.borodin@botconversa.com.br>
This commit is contained in:
@@ -18,7 +18,7 @@ export type URLGenerationOptions = {
|
||||
/**
|
||||
* Given a piece of text, checks for any URL present, generates link preview for the same and returns it
|
||||
* Return undefined if the fetch failed or no URL was found
|
||||
* @param text the text containing URL
|
||||
* @param text first matched URL in text
|
||||
* @returns the URL info required to generate link preview
|
||||
*/
|
||||
export const getUrlInfo = async(
|
||||
@@ -27,8 +27,11 @@ export const getUrlInfo = async(
|
||||
): Promise<WAUrlInfo | undefined> => {
|
||||
try {
|
||||
const { getLinkPreview } = await import('link-preview-js')
|
||||
|
||||
const info = await getLinkPreview(text, { timeout: opts.timeoutMs })
|
||||
let previewLink = text
|
||||
if (!text.startsWith('https://') && !text.startsWith('http://')) {
|
||||
previewLink = 'https://' + previewLink
|
||||
}
|
||||
const info = await getLinkPreview(previewLink, { timeout: opts.timeoutMs })
|
||||
if(info && 'title' in info) {
|
||||
const [image] = info.images
|
||||
|
||||
@@ -38,7 +41,7 @@ export const getUrlInfo = async(
|
||||
|
||||
return {
|
||||
'canonical-url': info.url,
|
||||
'matched-text': info.url,
|
||||
'matched-text': text,
|
||||
title: info.title,
|
||||
description: info.description,
|
||||
jpegThumbnail
|
||||
|
||||
@@ -248,9 +248,10 @@ export const generateWAMessageContent = async(
|
||||
const extContent = { text: message.text } as WATextMessage
|
||||
|
||||
let urlInfo = message.linkPreview
|
||||
if(!urlInfo && !!options.getUrlInfo && message.text.match(URL_REGEX)) {
|
||||
const matchedUrls = message.text.match(URL_REGEX)
|
||||
if(!urlInfo && !!options.getUrlInfo && matchedUrls) {
|
||||
try {
|
||||
urlInfo = await options.getUrlInfo(message.text)
|
||||
urlInfo = await options.getUrlInfo(matchedUrls[0])
|
||||
} catch(error) { // ignore if fails
|
||||
options.logger?.warn({ trace: error.stack }, 'url generation failed')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user