在频道中检测新帖子并将其转发到其他频道-使用setwebhook的电报机器人-php代码

时间:2018-12-30 18:48:38

标签: php telegram-bot

我想使用通道中的admin的电报bot检测电报通道中新帖子的帖子ID,并将新帖子转发到我已经指定的其他频道

我尝试使用以下php代码获取该机器人转发到频道的帖子ID

function bot($method, $datas = [])
{
    $url = "https://api.telegram.org/bot" . API_KEY . "/" . $method;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
    $res = curl_exec($ch);
    if (curl_error($ch)) {
        var_dump(curl_error($ch));
    } else {
        return json_decode($res);
    }
}

$msg_id = bot('ForwardMessage', [    
'chat_id' => $channel,    
'from_chat_id' => $chat_id,    
'message_id' => $message_id    
])->result->message_id;  

但是我需要php代码,如果新帖子是从其他管理员发送的,则机器人可以查找该频道,机器人会找到帖子ID,并将帖子转发到我已经在两个属于机器人管理员的频道中指定的其他频道

我阅读了电报的api机器人,其中说到如何在此链接中获取发布渠道:https://core.telegram.org/bots/api#update

在我的机器人中,我使用setwebhook,$ channel_post或$ chid中的哪一个是正确的?

$update = json_decode(file_get_contents('php://input'));
$channel_post = $update->message->channel_post;
$chid = $update->channel_post->message->message_id;

1 个答案:

答案 0 :(得分:0)

遵循以下代码:

if( isset($update->channel_post) && $update->channel_post )
{

  if(isset($update->channel_post->text) && $update->channel_post->text)
  {
      $apiToken = "my_bot_api_token";

      $data = [
          'chat_id' => '@targetChannel',
          'text' => $update->channel_post->text
      ];





file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data) );
}


}