Telegram Bot Api: How to Send a Photo Using PHP

Telegram BOT Api: how to send a photo using PHP?

This is my working solution, but it requires PHP 5.5:

$bot_url    = "https://api.telegram.org/bot<bot_id>/";
$url = $bot_url . "sendPhoto?chat_id=" . $chat_id ;

$post_fields = array('chat_id' => $chat_id,
'photo' => new CURLFile(realpath("/path/to/image.png"))
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type:multipart/form-data"
));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$output = curl_exec($ch);

Sending Image via Telegram-Bot API with PHP

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.

upload photo and text from form input and sending to telegram bot with php

First of all you must store the file uploaded.

in your html code you have error. you must add enctype='multipart/form-data' to support file input.

So your html code must be like this:

<form action="process.php" method="post" enctype="multipart/form-data">
<input type="text" name="name">
<input type="file" name="photo">
<input type="submit" value="Submit">
</form>

In your php file you must first save file uploaded.

define ('url',"https://api.telegram.org/bot****/");

$info = pathinfo($_FILES['photo']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = "newname.".$ext;

$target = 'images/'.$newname; // the path you want to upload your file
move_uploaded_file( $_FILES['photo']['tmp_name'], $target);

After that you can send this file to telegram api.

$chat_id = '123456'; // telegram user id
$url = url."sendPhoto?chat_id=$chat_id";

$params = [
'chat_id'=>$chat_id,
'photo'=>'Your site address/'.$target,
'caption'=>$_POST['name'],
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);
curl_close($ch);

echo $server_output;

Upload a photo to telegram API using Guzzle

You need to use multipart if you want guzzle to post the file:

$client->request('POST', 'sendPhoto', [
'multipart' => [
[ 'name' => 'chat_id', 'contents' => 'xxxxx'],
[
'name' => 'photo',
'contents' => fopen('img.png', 'r')
]
]
]);

If you have a lot of parameters, you could use a helper function that maps your aassociative array:

function toMultiPart(array $arr) {
$result = [];
array_walk($arr, function($value, $key) use(&$result) {
$result[] = ['name' => $key, 'contents' => $value];
});
return $result;
}

$client->request('POST', 'sendPhoto', [
'multipart' => toMultiPart([
'chat_id'=> 'xxxxx',
'photo'=> fopen('img.png', 'r')
])
});

Send a photo from php url in telegram bot

Nevermind, I managed to do it saving the file and uploading it with

urllib.request.urlretrieve("link.php", "./file.png")
bot.sendPhoto(chat_id, photo=open('./file.png', 'rb'))

php telegram sendPhoto not working (url & file location)

Excuse, I don't usually use curl, so I can give you another option:

function sendPhoto($id, $photo, $text = null){
GLOBAL $token;
$url = 'https://api.telegram.org/bot'.$token."/sendPhoto?
chat_id=$id&photo=$photo&parse_mode=HTML&caption=".urlencode($text);
file_get_contents($url);
}

Just declare the sendPhoto function in this way, put the variabile in which you stored the token instead of "$token" and use the parameters in this way:

  • $id = the id of the user (the one you declared like this: $id = $update['message']['from']['id'];)
  • $photo = absolute path of the image you want to send
  • $text = OPTIONAL caption for the image

Sending message in telegram bot with images

You can use /sendphoto and set the captionwhich appears under an image.

https://core.telegram.org/bots/api#sendphoto



Related Topics



Leave a reply



Submit