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

对着背影说爱祢 提交于 2020-01-12 05:01:05

问题


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?


回答1:


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




回答2:


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 "[​​​​​​​​​​​]".




回答3:


Method using http://.......jpg>.. 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




回答4:


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)


来源:https://stackoverflow.com/questions/38685619/how-to-send-an-embedded-image-along-with-text-in-a-message-via-telegram-bot-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!