4

I'm trying to send a message to a Telegram Bot using CURL in this PHP code ...

 <?php
  $botToken="<MY_DESTINATION_BOT_TOKEN_HERE>";

  $website="https://api.telegram.org/bot".$botToken;
  $chatId=1234567;  //Receiver Chat Id
  $params=[
      'chat_id'=>$chatId,
      'text'=>'This is my message !!!',
  ];
  $ch = curl_init($website . '/sendMessage');
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $result = curl_exec($ch);
  curl_close($ch);
?>

The code runs with no error but no message is shown in my destination Telegram bot.

The token is what the BotFather give me when I created my destination Telegram bot (Use this token to access the HTTP API: <MY_DESTINATION_BOT_TOKEN>)

Any suggestion will be appreciated ...

Cesare
  • 1,629
  • 9
  • 30
  • 72
  • 1
    Output the result with `var_dump($result);` to see what the Telegram API is returning. – Marco Jan 16 '18 at 21:48
  • 1
    I just tested the code and it seems to be working. It's most likely a telegram specific issue. If I recall correctly you have to enable messaging first (with @BotFather) before your bot is able to post messages in groups. – Marco Jan 16 '18 at 21:50
  • the Telegram API return `string(73) "{"ok":false,"error_code":400,"description":"Bad Request: chat not found"}"` Something about `$chatId=1234567;` in my code? – Cesare Jan 16 '18 at 21:53
  • 1
    Well, the error is pretty self explanatory, isn't it? There is no chat with id `1234567`. – Marco Jan 16 '18 at 21:55
  • How may I generate a correct chat id? – Cesare Jan 16 '18 at 21:57
  • 1
    Does it need to be dynamic/flexible ? If not then you can simply change the chat id and you are good to go. – Marco Jan 16 '18 at 21:59

5 Answers5

10

I've solved .... In my original code there were two errors, one in the code and one due on a Telegram feature that I didn't know: actually, telegram bot to bot communication is not possible as explained here Simulate sending a message to a bot from url

So my code revised is the follow

 <?php
  $botToken="<MY_DESTINATION_BOT_TOKEN_HERE>";

  $website="https://api.telegram.org/bot".$botToken;
  $chatId=1234567;  //** ===>>>NOTE: this chatId MUST be the chat_id of a person, NOT another bot chatId !!!**
  $params=[
      'chat_id'=>$chatId, 
      'text'=>'This is my message !!!',
  ];
  $ch = curl_init($website . '/sendMessage');
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $result = curl_exec($ch);
  curl_close($ch);
?>

In this way all works fine!

Cesare
  • 1,629
  • 9
  • 30
  • 72
2

You can obtain chat ID from @RawDataBot, it will be .message.chat.id.

For instance, in this response will be 109780439.

Martin
  • 22,212
  • 11
  • 70
  • 132
Sean Wei
  • 7,433
  • 1
  • 19
  • 39
1

if you want to send telegram using CURL, you need to download file cacert.pem and copy to your web server and then call them from your PHP script.

You can download file cacert.pem from

https://drive.google.com/open?id=1FCLH88MpKNLDXZg3pJUSAZ0BbUbNmBR2

I have tutorial video that can give you the clearly answer, include how to create bot, get token, get user chat_id, troubleshooting SSL and Proxy in CURL, etc:

Here is the link of my tutorial video https://youtu.be/UNERvcCz-Hw

Here is the complete script:

<?php

// using GET URL Parameter -> message
$pesan = urlencode($_GET["message"]);

$token = "bot"."<token>";
$chat_id = "<chat_id>";
$proxy = "<ip_proxy>:<port>";

$url = "https://api.telegram.org/$token/sendMessage?parse_mode=markdown&chat_id=$chat_id&text=$pesan";

$ch = curl_init();

if($proxy==""){
    $optArray = array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CAINFO => "C:\cacert.pem"   
    );
}
else{ 
    $optArray = array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_PROXY => "$proxy",
        CURLOPT_CAINFO => "C:\cacert.pem"   
    );  
}

curl_setopt_array($ch, $optArray);
$result = curl_exec($ch);

$err = curl_error($ch);
curl_close($ch);    

if($err<>"") echo "Error: $err";
else echo "Message SENT";

?>
1

use this will work

$TOKEN="your bot token";
$url="https://api.telegram.org/bot{$TOKEN}/sendMessage";

$request=curl_init($url);

$query=http_build_query([

    'chat_id'=>"your id",
    'text'=>$msg,
    'parse_mode'=>'MarkDown'

]);

curl_setopt_array($request,[

    CURLOPT_POST=>1,
    CURLOPT_POSTFIELDS=>$query,

]);

curl_exec($request);
  • 1
    while this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Shanteshwar Inde Aug 18 '22 at 06:40
0

I've tested it and its worked. you only need to us chatId like below:

$chatId='1234567';