How to send an Embedded Image along with text in a Message via Telegram Bot API
Asked Answered
T

8

25

Using Telegram Bot API,

I'm aware that it is possible to send an image via https://core.telegram.org/bots/api#sendphoto

However, how can I embed a remote image into a formatted message?

The message I am looking to send, can be compared to a news article with a title in bold, an image, and a longer text with links. I figured out how to create bold text and links with markdown, but I'm failing at inserting images. How can we do that?

Tepic answered 31/7, 2016 at 15:40 Comment(6)
As I know , for your SPECIFIC text and image you can only send an image with some text as caption right now( about 200 character)Reeves
That is what I am fearing. What would be the best way to do a feature request?Tepic
read thisReeves
What's most interesting for me is that IMDB's bot actually loads an image and some icons in what appears to be a text message. I will further investigate this and update this question if I'm successful.Adverse
@Adverse have you discovered how IMDB bot includes icons in text messages or are they simply emojis? The answer below returns me an error telegram.ext.dispatcher - WARNING - A TelegramError was raised while processing the Update. (using v.6.0.3)Carycaryatid
@fcalderan I have not done it myself, but I'm pretty sure they're just emojis. What you're sending is a standard telegram message. So, if your message text includes for instance: :joy: it should render as an emoji. As for the answer bellow, that is correct. It's only an anchor with no text. The reason for this is because telegram parses the message and if there happens to be a media reference it renders it under. So, using an anchor with no actual text works. Personally, I used ​ as the anchor content.Adverse
N
37

you must set ParseMode in HTML and set your Image Url in A tag like this:

<a href="' + image + '">&#8205;</a>

&#8205; -> never show in message

Nutrient answered 30/4, 2017 at 10:12 Comment(2)
also you must set disable_web_page_preview=falseFreehold
can image be a local photo or it must be an URL?Janinejanis
S
17

You can use zero-width space trick. Works for both Markdown and HTML parse mode.

Markdown:

$data = [
    'chat_id'    => $chat_id,
    'parse_mode' => 'markdown',
    'text' => "[​​​​​​​​​​​](https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Stack_Overflow_logo.svg/200px-Stack_Overflow_logo.svg.png) Some text here.",
];

Result:

enter image description here

Note: The zero-width space is in-between the brackets "[​​​​​​​​​​​]".

Selden answered 26/10, 2017 at 8:16 Comment(1)
This, as all the other methods I tried, works as long as disable_web_page_preview=false. Brackets with zero width in java is "[\u200B]"Canyon
F
3

Using sendPhoto rather than sendMessage is a cleaner way of achieving this, depending on your use case, for example:

import io
import json
import requests

telegram_bot_token = 'INSERT_TOKEN_HERE'
chat_id = '@INSERT_CHAT_ID_HERE'

bot_url = 'https://api.telegram.org/bot' + telegram_bot_token + '/sendPhoto'
img_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Stack_Overflow_logo.svg/200px-Stack_Overflow_logo.svg.png'

msg_txt = '<b>Stack Overflow Logo</b>'
msg_txt += '\n\nStack Overflow solves all our problems'

payload = {
  'chat_id': chat_id,
  'caption': msg_txt,
  'parse_mode': 'html'
}

remote_image = requests.get(img_url)
photo = io.BytesIO(remote_image.content)
photo.name = 'img.png'
files = {'photo': photo}

req = requests.post(url=bot_url, data=payload, files=files)
response = req.json()
print(response)
Flambeau answered 23/6, 2022 at 15:40 Comment(0)
B
2

Method using <a href=http://.......jpg>..</a> will show preview of the image below the text. Like this:

a href sample

It will look better if you send an image with a caption.

caption sample

Butanol answered 25/11, 2017 at 12:42 Comment(2)
In your 'caption sample' example , Are there multiple images in a single message ? Could you tell me how you did it ?Kallick
Oh, 3 portraits look confusing :-) Sorry! Both samples use ONE image. But both are a combination of AN image and text.Butanol
E
2
import requests    
text="testing"    
img="http://imageurl.png"       
r = requests.get('https://api.telegram.org/botyour_token_here/sendMessage?chat_id=@your_channel_here&parse_mode=markdown&text='+"[​​​​​​​​​​​]("+img+")"+text)
Estranged answered 29/9, 2019 at 9:27 Comment(0)
N
2

You should just add captions

bot.send_video(user_id, video, caption='some interesting text')

In our case captions are text. look this image

Nollie answered 2/5, 2021 at 0:59 Comment(0)
S
0

Might be useful for someone. I've managed to send a photo+text+parse_mode (text formatting) this way:

files = {
    'chat_id': id,
    'text': text,
    'photo': open('/root/****/****/*****/photo.jpg', 'rb')
}
url = f'https://api.telegram.org/bot{telegramToken}/sendPhoto?chat_id={id}&caption={text}&parse_mode=HTML'
requests.post(url, files=files)
Selma answered 27/5, 2023 at 14:30 Comment(0)
A
0

I simply made a link to the point in the Java code and sent it as a message with a preview.

"<a href=\"https://avandy-news.ru/*.jpg\">.</a>"

messageService.sendMessageWithPreview(chatId, text, Keyboards.getInfoKeyboard());

public void sendMessageWithPreview(long chatId, String textToSend, ReplyKeyboard keyboard) {
        SendMessage message = new SendMessage();
        message.setChatId(chatId);
        if (textToSend != null && !textToSend.isEmpty()) message.setText(textToSend);
        else return;
        message.enableHtml(true);
        message.setParseMode(ParseMode.HTML);
        if (keyboard != null) message.setReplyMarkup(keyboard);
        executeMessage(message);
}

private void executeMessage(SendMessage message) {
        try {
            execute(message);                
        } catch (TelegramApiException e) {
          ..                
        }
        return null;
    }

my bot avandy news

Acidify answered 16/7 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.