Cesare
Cesare

Reputation: 1749

Send a message to a Telegram bot using PHP

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 ...

Upvotes: 4

Views: 27965

Answers (5)

Hello World
Hello World

Reputation: 11

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);

Upvotes: 1

Mahoni Monster
Mahoni Monster

Reputation: 36

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";

?>

Upvotes: 1

farzad noroozy
farzad noroozy

Reputation: 1

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

$chatId='1234567';

Upvotes: 0

Cesare
Cesare

Reputation: 1749

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!

Upvotes: 11

Sean Wei
Sean Wei

Reputation: 8015

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

For instance, in this response will be 109780439.

Upvotes: 2

Related Questions