Reputation: 1
I have created a simple project to test sending photo to telegram bot in php 7, but nothing sends after starting the bot, however this project runs in php 5.3! Is there anything different in php 7 that I should use it?
$message = file_get_contents("php://input");
$result = json_decode($message,true);
$token = "My_Robot_token";
if($result['message']['text']=='/start')
{
$target_url = "https://api.telegram.org/bot".$token."/sendPhoto";
$file_path = "./img/img1.jpg";
$file_name_with_full_path = realpath($file_path);
$chat_id = $result['message']['chat']['id'];
$photo_send = array(
'chat_id' => $chat_id,
'photo' => '@'.$file_name_with_full_path,
'caption' => 'photo sent!'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $photo_send);
$result=curl_exec ($ch);
file_put_contents('res.txt', $ch);
}
Upvotes: 0
Views: 2850
Reputation: 11
define('API_KEY','API Token');
function bot($method,$datas=[]){
$url = "https://api.telegram.org/bot" . API_KEY . "/" . $method;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$datas);
$res = curl_exec($ch);
if(curl_error($ch)){
var_dump(curl_error($ch));
}else{
return json_decode($res);
}
}
function sendphoto($ChatId, $photo, $caption){
bot('sendphoto',[
'chat_id'=>$ChatId,
'photo'=>$photo,
'caption'=>$caption
]);
}
$update = json_decode(file_get_contents('php://input'));
var_dump($update);
$message=$update->message;
$chat_id=$message->chat->id;
$text =$message->text;
if ($text=='/start'){
sendphoto($chat_id,"Your file_id","photo sent!"); // send photo by file_id
sendphoto($chat_id,new CURLFILE("file path"),"photo sent!"); // send photo by url
}
Upvotes: 1