使用whatsapp-web.js发送WhatsApp消息时如何启用URL预览?
WhatsApp消息无法显示URL预览问题
我通过以下Node.js代码搭建服务,借助curl命令能成功向指定手机号发送WhatsApp文本消息,但消息中的URL始终无法显示预览效果,即使调整linkPreview相关参数也无法解决。
服务代码
const { Client } = require('whatsapp-web.js'); const qrcode = require('qrcode-terminal'); const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.json()); const client = new Client({ puppeteer: { args: ['--no-sandbox'], } }); client.on('qr', (qr) => { console.log('QR Code received, scan it with your phone.'); qrcode.generate(qr, { small: true }); }); client.on('ready', () => { console.log('Client is ready!'); }); client.initialize(); app.post('/send-message', async (req, res) => { const { number, text } = req.body; if (!number || !text) { return res.status(400).json({ error: 'Missing number or text' }); } try { await client.sendMessage(`${number}@c.us`, text, { linkPreview: true }); console.log(`Message sent to ${number}: ${text}`); res.status(200).json({ success: 'Message sent' }); } catch (error) { console.error('Error sending message:', error); res.status(500).json({ error: 'Error sending message' }); } }); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });
执行的curl命令
curl -H "Content-Type: application/json" -d '{"number": "1234567890", "text": "text http://stackoverflow.com "}'
已尝试的解决方法
- 传递参数
{ linkPreview: true }调用sendMessage - 传递参数
{ linkPreview: { includePreview: true }}调用sendMessage - 不传递任何
linkPreview参数,直接调用sendMessage
以上三种方式均无法让消息中的URL显示预览,实际效果截图:
内容的提问来源于stack exchange,提问作者Mévatlavé Kraspek
相关产品推荐
相关产品推荐

