使用 PHP 通过 Telegram-Bot API 发送图像

时间:2021-04-06 01:18:32

标签: php telegram-bot

我有这个功能

function suspendido($chat_id,$foo) 
{
  $TOKEN = "blablalbal";
  $TELEGRAM = "https://api.telegram.org:443/bot$TOKEN"; 
  $url.    = "https://zrabogados-pruebas.xyz/bot/404.png";
  $query = http_build_query(array(
    'chat_id'=> $chat_id,
    'photo'=> $url,
    'text'=> $foo,
    'parse_mode'=> "HTML", // Optional: Markdown | HTML
  ));
  $response = file_get_contents("$TELEGRAM/sendMessage?$query");
  return $response;

}

我尝试在不使用 curl 的情况下发送图像,尝试使用 file_get_contents 但没有任何效果。是不是少了什么东西?。

2 个答案:

答案 0 :(得分:0)

您使用了 sendMessage 方法,并且此方法不接受 photo 参数。

并且 $url 不应与另一个链接连接。

要发送照片,请使用 sendPhoto 方法,如下所示:

<?php
function suspendido($chat_id, $url, $foo) 
{
  $TOKEN = "<bot_token>";
  $TELEGRAM = "https://api.telegram.org:443/bot$TOKEN"; 
  $url = "https://zrabogados-pruebas.xyz/bot/404.png";
  $query = http_build_query(array(
    'chat_id'=> $chat_id,
    'photo'=> $url,
    'text'=> $foo,
    'parse_mode'=> 'HTML' // Optional: Markdown | HTML
  ));
  
  # Use sendPhoto here, Not sendMessage
  $response = file_get_contents("$TELEGRAM/sendPhoto?$query");
  return $response;
}

答案 1 :(得分:0)

显然,电报服务器存在一些问题。

如果你把一些图片属性放到 url 中,它就可以工作了。

function suspendido($chat_id,$foo) 
{
  $TOKEN = "blablalbla";
  $TELEGRAM = "https://api.telegram.org:443/bot$TOKEN"; 
  $url    = "https://zrabogados-pruebas.xyz/bot/404.png?center=140.50,36.15&width=1024&height=576";
  $query = http_build_query(array(
    'chat_id'=> $chat_id,
    'photo'=> $url,
    'text'=> $foo,
    'parse_mode'=> "HTML", // Optional: Markdown | HTML
  ));
  $response = file_get_contents("$TELEGRAM/sendMessage?$query");
  return $response;

}

我知道这很愚蠢,但如果您发送没有此内容的图片网址,它会以这种方式工作,那么您将收到 400 错误。