简体   繁体   English

使用 PHP 向 Telegram 机器人发送消息

[英]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 代码中使用 CURL 向 Telegram Bot 发送消息...

 <?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> )令牌是我创建目标 Telegram 机器人时 BotFather 给我的(使用此令牌访问 HTTP API: <MY_DESTINATION_BOT_TOKEN>

Any suggestion will be appreciated...任何建议将不胜感激...

You can obtain chat ID from @RawDataBot , it will be .message.chat.id . 您可以从@RawDataBot获取聊天ID,它将是.message.chat.id

For instance, in this response will be 109780439 . 例如, 此响应将为109780439

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! 这样,一切正常!

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. 如果要使用CURL发送电报,则需要下载文件cacert.pem并复制到Web服务器,然后从PHP脚本中调用它们。

You can download file cacert.pem from 您可以从以下位置下载文件cacert.pem

https://drive.google.com/open?id=1FCLH88MpKNLDXZg3pJUSAZ0BbUbNmBR2 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: 我有一个教程视频,可以为您提供明确的答案,包括如何创建漫游器,获取令牌,获取用户chat_id,对CURL中的SSL和代理进行故障排除等:

Here is the link of my tutorial video https://youtu.be/UNERvcCz-Hw 这是我的教程视频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";

?>

I've tested it and its worked. 我已经对其进行了测试和工作。 you only need to us chatId like below: 您只需要像下面这样给我们chatId:

$chatId='1234567'; $ chatId ='1234567';

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM