23

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?

Seyfi
  • 1,832
  • 1
  • 20
  • 35
Pierre-Antoine
  • 7,939
  • 6
  • 28
  • 36
  • 2
    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) – Seyfi Jul 31 '16 at 18:09
  • That is what I am fearing. What would be the best way to do a feature request? – Pierre-Antoine Jul 31 '16 at 18:59
  • 2
    [read this](https://core.telegram.org/bots/faq#will-you-add-x-to-the-bot-api) – Seyfi Jul 31 '16 at 19:11
  • 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. – jhenriquez Mar 30 '17 at 01:05
  • @jhenriquez 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) – Fabrizio Calderan Jun 05 '17 at 11:53
  • @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. – jhenriquez Jun 08 '17 at 14:28

7 Answers7

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

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
barzin.A
  • 1,554
  • 2
  • 12
  • 20
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 "[​​​​​​​​​​​]".

Bing Han
  • 684
  • 6
  • 13
  • 2
    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]" – Testo Testini Oct 17 '19 at 21:45
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

Eugene Loy
  • 12,224
  • 8
  • 53
  • 79
  • In your 'caption sample' example , Are there multiple images in a single message ? Could you tell me how you did it ? – jonny789 Jul 07 '20 at 06:00
  • Oh, 3 portraits look confusing :-) Sorry! Both samples use ONE image. But both are a combination of AN image and text. –  Jul 13 '20 at 05:20
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)
Amit Ghosh
  • 1,500
  • 13
  • 18
1

You should just add captions

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

In our case captions are text. look this image

1

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)
Ashley Kleynhans
  • 354
  • 2
  • 13
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)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Diego_ht
  • 19
  • 4