使用 PHP 发送消息电报

Send message telegram using PHP

我在 PHP 上写了电报机器人代码,它有很多命令。除一个命令外,所有命令都可以正常工作。 这是这段代码。它必须向机器人用户发送消息。

if($text == '/send' and isset($reply_to_message) and $chat_id == $config['admin_id']){
$message_id = $reply_to_message->message_id;
$db->query("UPDATE `bot_settings` SET `send_status` = '1', `message_id` = '$message_id' WHERE `id` = '1'");
$telegram->sendMessage($chat_id, [
    'text'=>"Message sending is started!"
]);

}

这里是发送函数

        $db = new MySQL($config['hostname'], $config['username'], $config['password'], $config['dbname']);
    $telegram = new Runner($config['api_key']);

    $settings = $db->query("SELECT * FROM `bot_settings` WHERE `id`='1'")->fetch_assoc();

    $message_id = $settings['message_id'];
    $status = $settings['send_status'];
    if($status == 0){
        exit;
    }else{
        $offset = $settings['send_offset'];
        $users = $db->query("SELECT * FROM `bot_users` LIMIT 60 OFFSET ".$offset);
        if($users->num_rows > 0){
            $offset += 60;
            $db->query("UPDATE `bot_settings` SET `send_offset` = '$offset' WHERE `id`='1'");
            while($row = $users->fetch_assoc()){
                $telegram->forwardMessage($row['user_id'], $config['admin_id'], $message_id);
                usleep(200000);
            }
        }else{
            $telegram->sendMessage($config['admin_id'], [
                'text'=>"Message sending is finished!"
            ]);
            $db->query("UPDATE `bot_settings` SET `send_offset` = '0', `send_status` = '0' WHERE `id`='1'");
            exit;
        }
    }

这是表格

    -- Table `bot_settings`

    CREATE TABLE IF NOT EXISTS `bot_settings` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `send_offset` int(10) NOT NULL,
    `reminder_offset` int(10) NOT NULL,
    `send_status` tinyint(1) NOT NULL,
    `reminder_status` tinyint(1) NOT NULL,
    `message_id` int(10) NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

    -- --------------------------------------------------------
    -- Table `bot_users`

    CREATE TABLE IF NOT EXISTS `bot_users` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `user_id` int(20) NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

问题: 当我 运行 这段代码时,它说“消息发送已开始!”并在数据库中进行更改,但没有一个用户没有收到消息。

可能是什么问题,我该如何解决。在此先感谢您的帮助

这是示例机器人:@UssdYordamchiBot

错误很简单。 我没有调用发送功能。问题已通过调用发送函数解决

if($text == '/send' and isset($reply_to_message) and $chat_id == $config['admin_id']){
$message_id = $reply_to_message->message_id;
$db->query("UPDATE `bot_settings` SET `send_status` = '1', `message_id` = '$message_id' WHERE `id` = '1'");
$telegram->sendMessage($chat_id, [
    'text'=>"Message sending started!"
]);
sendpost(); // This line is added
}

并如下更改此功能。

        function sendpost() 
    {
        global $config;
        global $db;
        global $telegram;
        $settings = $db->query("SELECT * FROM `bot_settings` WHERE `id`='1'")->fetch_assoc();

        $message_id = $settings['message_id'];
        $status = $settings['send_status'];
        if($status == 0){
            exit;
        }else{
            $offset = $settings['send_offset'];
            $users = $db->query("SELECT * FROM `bot_users` LIMIT 60 OFFSET ".$offset);
            if($users->num_rows > 0){
                $offset += 60;
                $db->query("UPDATE `bot_settings` SET `send_offset` = '$offset' WHERE `id`='1'");
                while($row = $users->fetch_assoc()){
                    $telegram->forwardMessage($row['user_id'], $config['admin_id'], $message_id);
                    usleep(200000);
                }
                $telegram->sendMessage($config['admin_id'], [
                    'text'=>"Message sending finished!"
                ]);
            }else{
                $telegram->sendMessage($config['admin_id'], [
                    'text'=>"Nothing to send!"
                ]);
                $db->query("UPDATE `bot_settings` SET `send_offset` = '0', `send_status` = '0' WHERE `id`='1'");
                exit;
            }
        }
    }