JuanFernandoz
JuanFernandoz

Reputation: 799

Sending Image via Telegram-Bot API with PHP

I have this function

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;

}

I try to sending an Image without using curl, tried to use file_get_contents but nothing works. Is something missing?.

Upvotes: 0

Views: 3225

Answers (2)

JuanFernandoz
JuanFernandoz

Reputation: 799

Apparently, there is some problem with telegram servers.

If you put some image attributes into url it works.

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;

}

I know its just silly but it works that way if you send the image url without this then you will receive a 400 error.

Upvotes: 1

Muaath
Muaath

Reputation: 613

You used sendMessage method, And this method isn't accept photo parameter.

And $url shouldn't be concatenated with another link.

To send photo use sendPhoto method like this:

<?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;
}

Upvotes: 0

Related Questions