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
|
* 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
|
* 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
|
* @returns the URL info required to generate link preview
|
||||||
*/
|
*/
|
||||||
export const getUrlInfo = async(
|
export const getUrlInfo = async(
|
||||||
@@ -27,8 +27,11 @@ export const getUrlInfo = async(
|
|||||||
): Promise<WAUrlInfo | undefined> => {
|
): Promise<WAUrlInfo | undefined> => {
|
||||||
try {
|
try {
|
||||||
const { getLinkPreview } = await import('link-preview-js')
|
const { getLinkPreview } = await import('link-preview-js')
|
||||||
|
let previewLink = text
|
||||||
const info = await getLinkPreview(text, { timeout: opts.timeoutMs })
|
if (!text.startsWith('https://') && !text.startsWith('http://')) {
|
||||||
|
previewLink = 'https://' + previewLink
|
||||||
|
}
|
||||||
|
const info = await getLinkPreview(previewLink, { timeout: opts.timeoutMs })
|
||||||
if(info && 'title' in info) {
|
if(info && 'title' in info) {
|
||||||
const [image] = info.images
|
const [image] = info.images
|
||||||
|
|
||||||
@@ -38,7 +41,7 @@ export const getUrlInfo = async(
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
'canonical-url': info.url,
|
'canonical-url': info.url,
|
||||||
'matched-text': info.url,
|
'matched-text': text,
|
||||||
title: info.title,
|
title: info.title,
|
||||||
description: info.description,
|
description: info.description,
|
||||||
jpegThumbnail
|
jpegThumbnail
|
||||||
|
|||||||
@@ -248,9 +248,10 @@ export const generateWAMessageContent = async(
|
|||||||
const extContent = { text: message.text } as WATextMessage
|
const extContent = { text: message.text } as WATextMessage
|
||||||
|
|
||||||
let urlInfo = message.linkPreview
|
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 {
|
try {
|
||||||
urlInfo = await options.getUrlInfo(message.text)
|
urlInfo = await options.getUrlInfo(matchedUrls[0])
|
||||||
} catch(error) { // ignore if fails
|
} catch(error) { // ignore if fails
|
||||||
options.logger?.warn({ trace: error.stack }, 'url generation failed')
|
options.logger?.warn({ trace: error.stack }, 'url generation failed')
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user