Ces*_*are 2 php curl telegram-bot
我正在尝试使用此 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);
?>
Run Code Online (Sandbox Code Playgroud)
代码运行没有错误,但我的目标 Telegram bot 中没有显示任何消息。
令牌是什么BotFather给我,当我建立了我的目的地电报机器人(使用此令牌访问HTTP API: <MY_DESTINATION_BOT_TOKEN>)
任何建议将不胜感激......
我已经解决了......在我的原始代码中有两个错误,一个在代码中,另一个是由于我不知道的 Telegram 功能导致的:实际上,电报机器人到机器人的通信是不可能的,如此处所述模拟发送从 url 发送给机器人的消息
所以我修改的代码如下
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
这样一切正常!