【问题标题】:send message in telegram bot在电报机器人中发送消息
【发布时间】:2016-03-11 09:31:59
【问题描述】:

我用 php 编写电报机器人。我保存用户聊天ID以发送消息;使用此命令发送消息:

 /admin sendall:hellow 

并在 php 应用程序中使用此代码:

 case '/admin':
                if ($chat_id == 'my chatid') {
                    $array = str_replace('/admin', '', $message);
                    $array = trim($array);
                    $array = explode(':', $array);
                    $Admin = new AdminCommand();
                    $Admin->getCommand($array[0], $array[1]);
                } else {
                    sendMessage($chat_id, 'block ');
                }
                break;

AdminCommand 类:

class AdminCommand extends Database {

    public function getCommand($command, $action = null) {
        switch ($command) {
            case 'sendall':
                $this->sendall($action);
                break;
            default:
                # code...
                break;
        }
    }

    public function sendall($message) {
        $sql = $this->con->prepare('SELECT * FROM `users`');
        $sql->execute();
        $res = $sql->fetchAll();
        foreach ($res as $row) {
            sendMessage($row['chatid'], $message);
        }
        exit();
    }

}

sendMessage 函数:

function sendMessage($chatId, $message) {

    $url = WEBSITE . "/sendMessage?chat_id=" . $chatId . "&text=" . urlencode($message);
    file_get_contents($url);
}

大多数时候它工作正常,但有时在向所有用户发送消息后会一次又一次地重复,只要我删除数据库就不会停止。 有什么问题?

【问题讨论】:

  • 我认为你需要关闭连接。

标签: php telegram telegram-bot


【解决方案1】:

正如我在这个answer 和电报网站的Bots FAQ 页面中解释的那样:

如何一次向我的机器人的所有订阅者发送消息?
很遗憾,目前我们没有发送批量消息的方法,例如通知。我们将来可能会在这些方面添加一些内容。
为了避免在发送大量通知时达到我们的限制,请考虑将它们分散在更长的时间间隔内,例如8-12 小时。 API 不允许每秒向不同用户发送超过约 30 条消息,如果超过此限制,您将开始收到 429 错误。 您不能以这种方式向所有用户发送消息。

机器人常见问题页面中的解决方案:

我的机器人已达到极限,我该如何避免这种情况?
在特定聊天中发送消息时,请避免每秒发送超过一条消息。我们可能允许超过此限制的短脉冲,但最终您将开始收到 429 错误。
如果您向多个用户发送批量通知,API 将不允许超过每秒 30 条左右的消息。考虑在 8 到 12 小时的较大间隔内分散通知以获得最佳效果。
另请注意,您的机器人每分钟向同一组发送的消息不能超过 20 条。

【讨论】:

    猜你喜欢
    • 2017-03-19
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 2016-11-11
    • 2019-10-30
    • 2019-12-31
    • 2017-05-11
    相关资源
    最近更新 更多