Sending message in telegram bot with images
Asked Answered
B

5

19

I have telegram-bot code on php, and reply messages sending by replyWithMessage method.

All command here:

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

How can i add some preview image before text?

Berlyn answered 16/6, 2016 at 13:35 Comment(0)
P
32

You can use /sendphoto and set the captionwhich appears under an image.
https://core.telegram.org/bots/api#sendphoto

Pedroza answered 16/6, 2016 at 16:45 Comment(2)
It has a 0-200 characters constrain. core.telegram.org/bots/api#sendphotoRayborn
Photo caption (may also be used when resending photos by file_id), 0-1024 charactersPedroza
J
9

No, You CAN send a text that contains photo in one single message. Telegram allows you do this but that the way is kinda tricky.

  1. Using https://core.telegram.org/bots/api#sendmessage method, set option disable_web_page_preview => false
  2. In your text data put an image link with invisible character(s) inside your message text.

Example:

$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);
Jorrie answered 12/3, 2019 at 16:58 Comment(2)
an actual information is here: https://mcmap.net/q/525322/-how-to-send-an-embedded-image-along-with-text-in-a-message-via-telegram-bot-api tl dr: 1) put &#8205; inside a-tag instead of whitespaces 2) set disable_web_page_preview=trueMydriasis
@Vladimir, thanks for the heads up, works flawlesslyItinerant
E
5

You can do it using markdown too:

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);
    })
Ednaedny answered 8/11, 2019 at 7:59 Comment(0)
D
2

You can't send a text message that contains both image and text. However If your text contains URL, Telegram displays a preview of the web page by default. Or you can send two message one after another or send a photo with caption.

Disagreement answered 16/6, 2016 at 16:19 Comment(0)
G
0

The best way I found is use /sendPhoto with caption (max len 1024 characters) and then will use /sendMessage, where 4096 characters allowed

Gelignite answered 15/2, 2024 at 21:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.