Telegram 机器人 API 响应 403 Forbidden - 向机器人发送直接消息时

Telegram bots API responds 403 Forbidden - when send direct message to bot

$apiToken = "5232750517:AAF7gbXCwZdfhSQeZZb53-2NhTm2xxNuT3U";

          $data = [
              'chat_id' => '5232750517',
              'text' => 'Hello World'
          ];
          $response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" .
                                         http_build_query($data) );

错误信息

Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden

然后我按照这个解决方案 -

我的机器人用户名是@test247_bot并将代码更改为:

$apiToken = "5232750517:AAF7gbXCwZdfhSQeZZb53-2NhTm2xxNuT3U";

  $data = [
      'chat_id' => '5232750517',
      'text' => 'Hello World'
  ];
  $response = file_get_contents("https://api.telegram.org/@test247_bot$apiToken/sendMessage?" .
                                 http_build_query($data) );

但是还是不行,报错信息

Failed to open stream: HTTP request failed! HTTP/1.1 404

请帮忙

这不是构建 URL 的正确方法。

删除您的机器人用户名。它只是“bot”后跟 api 令牌。

尝试:

file_get_contents("https://api.telegram.org/bot5232750517:AAF7gbXCwZdfhSQeZZb53-2NhTm2xxNuT3U/sendMessage?" .http_build_query($data) );