php 在带有图像的电报机器人中发送消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37860889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending message in telegram bot with images
提问by SergioZhidkov
I have telegram-bot code on php, and reply messages sending by replyWithMessage
method.
我在 php 上有电报机器人代码,并通过replyWithMessage
方法回复消息发送。
All command here:
所有命令在这里:
$this->replyWithMessage(['text' => $item['title']. "\n\n" . $url]);
How can i add some preview image before text?
如何在文本前添加一些预览图像?
回答by Maak
You can use /sendphoto
and set the caption
which appears under an image.
https://core.telegram.org/bots/api#sendphoto
您可以使用/sendphoto
和设置caption
出现在图像下方的 。
https://core.telegram.org/bots/api#sendphoto
回答by ariaby
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.
您不能发送同时包含图像和文本的短信。但是,如果您的文本包含 URL,则 Telegram 默认显示网页预览。或者您可以一个接一个地发送两条消息或发送带有标题的照片。
回答by Dias
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.
不,您可以在一条消息中发送包含照片的文本。Telegram 允许你这样做,但这种方式有点棘手。
- Using https://core.telegram.org/bots/api#sendmessagemethod, set option
disable_web_page_preview
=>false
- In your
text
data put an image link with invisible character(s) inside your message text.
- 使用https://core.telegram.org/bots/api#sendmessage方法,设置选项
disable_web_page_preview
=>false
- 在您的
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);
回答by alexgenovese
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);
})