使用 php 向电报机器人发送消息

时间:2021-07-30 17:13:45

标签: php curl

<?php
$parameters = array(
    "chat_id" => "******",
    "text" => "Hello",
    "disable_web_page_preview" => "true",
    "parse_mode" => "Markdown"
);
send($parameters);
function send($parameters)
{
    $bot_token = "*********";
    $url = "https://api.telegram.org/bot$bot_token/sendMessage";
    if (!$ch = curl_init())
    {
        exit();
    }
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $outpot = curl_exec($ch);
    return $outpot;
}
?>

这是我的代码,但它不起作用。

我想向电报机器人发送一些消息。

1 个答案:

答案 0 :(得分:0)

您的代码看起来不错,但是您使用的聊天 ID 不正确。您需要通过 bot getMessages 端点 API 手动获取频道 ID。