简体   繁体   English

在电报机器人中发送消息

[英]send message in telegram bot

I write telegram bot with php . 我用php写电报机器人。 I save users chatid for send message ; 我保存用户聊天发送消息的聊天室; use this command for send message : 使用此命令发送消息:

 /admin sendall:hellow 

and in php app use this code : 并在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类:

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函数:

function sendMessage($chatId, $message) {

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

Most of the times it's work fine but sometimes after send message to all users repeats that again and again and don't stop As long as i'm delete database . 在大多数情况下,它工作正常,但有时在向所有用户发送消息之后,一次又一次地重复该操作,并且只要我删除数据库,就不会停止。 what's the problem ? 有什么问题 ?

As i explained in this answer and in Bots FAQ page in telegram site: 正如我在此答案和电报站点的Bots FAQ页面中所解释的:

How can I message all of my bot's subscribers at once? 我如何一次向所有机器人的订阅者发送消息?
Unfortunately, at this moment we don't have methods for sending bulk messages, eg notifications. 不幸的是,目前我们还没有发送大量消息的方法,例如通知。 We may add something along these lines in the future. 我们将来可能会沿着这些思路添加一些内容。
In order to avoid hitting our limits when sending out mass notifications, consider spreading them over longer intervals, eg 8-12 hours. 为了避免在发送大量通知时违反我们的限制,请考虑将它们分散在更长的时间间隔内,例如8-12小时。 The API will not allow more than ~30 messages to different users per second, if you go over that, you'll start getting 429 errors. 该API每秒不允许向不同用户发送超过30条消息,如果超过此限制,您将开始收到429个错误。 You can't send message this way to all user. 您不能通过这种方式向所有用户发送消息。

and solution in Bots FAQ page: Bots FAQ页面中的解决方案和解决方案:

My bot is hitting limits, how do I avoid this? 我的机器人已达到极限,如何避免这种情况?
When sending messages inside a particular chat, avoid sending more than one message per second. 在特定聊天中发送消息时,避免每秒发送多个消息。 We may allow short bursts that go over this limit, but eventually you'll begin receiving 429 errors. 我们可能允许短脉冲超过此限制,但最终您将开始收到429个错误。
If you're sending bulk notifications to multiple users, the API will not allow more than 30 messages per second or so. 如果您要向多个用户发送批量通知,则该API每秒最多不得超过30条消息。 Consider spreading out notifications over large intervals of 8—12 hours for best results. 考虑将通知分散在8到12小时的较大间隔内,以取得最佳效果。
Also note that your bot will not be able to send more than 20 messages per minute to the same group. 另请注意,您的漫游器每分钟最多只能向同一组发送20条以上的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM