在带有图像的电报机器人中发送消息

Sending message in telegram bot with images

我在 php 上有电报机器人代码,并通过 replyWithMessage 方法发送回复消息。

此处所有命令:

 $this->replyWithMessage(['text' => $item['title']. "\n\n" . $url]);

如何在文本前添加一些预览图片?

您不能发送同时包含图像和文本的短信。但是,如果您的文本包含 URL,Telegram 默认会显示网页预览。 或者您可以一个接一个地发送两条消息或发送一张带标题的照片。

您可以使用 /sendphoto 并设置显示在图像下方的 caption
https://core.telegram.org/bots/api#sendphoto

不,您可以在一条消息中发送包含照片的文本。 Telegram 允许您这样做,但方法有点棘手。

  1. 使用https://core.telegram.org/bots/api#sendmessage方法,设置选项disable_web_page_preview => false
  2. 在您的 text 数据中,将带有不可见字符的图像 link 放入消息文本中。

示例:

$message = <<<TEXT
*** your content ***
*** somewhere below (or above) a link to your image with invisible character(s) ***
<a href="https://www.carspecs.us/photos/c8447c97e355f462368178b3518367824a757327-2000.jpg"> ‏ </a>
TEXT;

$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);
curl_setopt($ch, CURLOPT_URL, 'https://api.telegram.org/bot<token>/sendMessage');
$postFields = array(
    'chat_id' => '@username',
    'text' => $message,
    'parse_mode' => 'HTML',
    'disable_web_page_preview' => false,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
if(!curl_exec($ch))
    echo curl_error($ch);
curl_close($ch);

您可以使用降价来做到这一点:

var axios = require('axios');

axios.post('https://api.telegram.org/'+telegram_bot_id+'/sendMessage', { chat_id: _target_telegram_channel, parse_mode: 'markdown', text: '[ ‏ ](https://www.amazon.it/gp/product/B07NVCJ3V8?pf_rd_p=ba8c3f2e-eba5-4c79-9599-683af7a49dd1&pf_rd_r=XPRH5A07HN9W62DK1R84)' } )
    .then(response => {
        console.log(response);
    })
    .catch(error => {
        console.log(error);
    })