Mostafa
Mostafa

Reputation: 23

telegram bot send message from server failed (PHP)

I need a simple bot to send message via telegram. I use this php code for the reason:

header('Content-Type: text/html; charset=utf-8');
$message= file_get_contents("php://input");
$arrayMessage= json_decode($message, true);
$token= "*********";
$chat_id= $arrayMessage['message']['from']['id'];
$command= $arrayMessage['message']['text'];
    if($command == '/start'){
      $text= "Hello";
      $url= "https://api.telegram.org/bot".$token."/sendMessage?chat_id=".$chat_id."&text=".$text;
      file_put_contents("message.txt", $url);
      //file_get_contents ($url);
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      $output = curl_exec($ch);   
      curl_close($ch);
      }
  1. When i use file_get_contents i receive http bad request 400 ....
  2. With curl nothing changed
  3. I put the $url in text file and i used it in the browser and it works great

any suggestion?

i use cloudflare for ssl certificate and the webhook method is initiated.

Upvotes: 0

Views: 1462

Answers (1)

Ravistm
Ravistm

Reputation: 2213

Maybe you need to add

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

and other debugging code to find the error as follows:

var_dump ( curl_getinfo ( $ch ) );
if (curl_errno ( $ch )) {
    print curl_error ( $ch );
} else {
    curl_close ( $ch );
}

Upvotes: 1

Related Questions