Reputation: 8109
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?
Upvotes: 25
Views: 66754
Reputation: 121
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;
}
Upvotes: 0
Reputation: 19
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)
Upvotes: 0
Reputation: 405
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)
Upvotes: 3
Reputation:
Method using <a href=http://.......jpg>..</a>
will show preview of the image below the text.
Like this:
It will look better if you send an image with a caption.
Upvotes: 2
Reputation: 21
You should just add captions
bot.send_video(user_id, video, caption='some interesting text')
In our case captions are text. look this image
Upvotes: 2
Reputation: 1606
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)
Upvotes: 2
Reputation: 684
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:
Note: The zero-width space is in-between the brackets "[]".
Upvotes: 17
Reputation: 1582
you must set ParseMode in HTML and set your Image Url in A tag like this:
<a href="' + image + '">‍</a>
‍
-> never show in message
Upvotes: 37