为什么我的电报机器人在使用cronjob时不将消息发送到频道?

问题描述 投票:1回答:1

大家好,我有一个电报机器人,我正在尝试向我的频道发送消息。我想安排并在特定时间发送帖子。我尝试使用cronjob,当我使用cronjob时,发生了一些事情,它没有发送消息,但是当我手动调用该url(主机中的cronjob文件)时,它完美地发送了消息这是我的代码,我从数据库获取消息并将其发送!

while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
    $User_ID = $row["user_id"];
    $Caption = $row["body"] . "\n\n";
    $ChannelID = $row["channel_id"];

    $telegram->mSendMessageToChannel($ChannelID, $Caption);
    $telegram->mSendSimpleMessage($User_ID, "post sent.");

}

一切都很好,直到这里我可以从数据库获取帖子,并且一切都很好。这是我的mSendMessageToChannel方法

 public function mSendMessageToChannel($channelID, $postText)
{
    $url = 'https://api.telegram.org/bot' . $this->token . '/sendMessage';

    $post_fields = array(
        'chat_id' => "$channelID",
        'text' => $postText
    );

    $this->executeCURL($url, $post_fields);
}

这是我的mSendSimpleMessage方法

 public function mSendSimpleMessage($userId, $text)
{
    $url = "https://api.telegram.org/bot" . $this->token . "/sendMessage";
    $post_fields = array(
        "chat_id" => $userId,
        'text' => $text
    );
    $this->executeCURL($url, $post_fields);
}

这是我的executeCURL方法:

 public function executeCURL($url, $post_fields)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Content-Type:multipart/form-data"
    ));
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    $output = curl_exec($ch);
}

这是错误,我无法识别我的频道:

[[15-Mar-2019 07:13:01 UTC] PHP警告:curl_setopt()[function.curl-setopt]:无法在线访问/home1/derakhtc/public_html/telegramBot/HafezederakhshanBot/telegram.php中的branio_ir 85

第85行是curl_setopt($ ch,CURLOPT_POSTFIELDS,$ post_fields);

我的机器人是我的频道的完全管理员tnx帮助

php cron telegram-bot sendmessage php-telegram-bot
1个回答
-1
投票

您必须将chat_id从@channelname更改为数字ID。有机器人为您做到这一点

因此,找到您的频道ID(即以“-”开头的数字,然后用该数字替换@channelname)>

© www.soinside.com 2019 - 2024. All rights reserved.