Telegram Bot Api,向多个 chat_id 发送消息

Telegram BotApi, Send message to multiple chat_id

我想知道我是否可以在电报机器人 api 上用我的机器人向多个 chat_id 发送消息,但我无法弄清楚。这完全是因为电报 api 太难理解了。 我用它向 chat_id:

发送了一条消息

https://api.telegram.org/botTOKKEN/sendMessage?chat_id=xxxxxxx&text=Hi+John


没有办法让机器人向多个聊天 ID 发送消息,但有一个技巧可以暂时修复它:)
为什么不向每个聊天 ID 发送一条消息?!
让我们看看 PHP 中的这个例子:

<?php
$message = "Hi John";
$chatIds = array("xxx","xxx","xxx"); // AND SOME MORE
foreach($chatIds as $chatId) {
    // Send Message To chat id
    file_get_contents("https://api.telegram.org/botTOKKEN/sendMessage?chat_id=$chatId&text=".$message);
}
?>

仅供参考。

我们可以将chat_ids输入数据库。查询并循环消息部分,以使用 sleep().

将消息发送给多个 chat-id

我不是程序员。所以没法举例。

foreach 或任何其他大量 sendMessage 的问题是 API 不允许每秒向不同用户发送超过 ~30 条消息。

根据电报站点中的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, e.g. 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, e.g. 8-12 hours. 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. You can't send message this way to all user.

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

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.
If you're sending bulk notifications to multiple users, the API will not allow more than 30 messages per second or so. Consider spreading out notifications over large intervals of 8—12 hours for best results.
Also note that your bot will not be able to send more than 20 messages per minute to the same group.

除了@farsad 的回答:在 foreach 循环中添加 sleep(NUMBER_OF_SECONDS); 以免被电报禁止。因为 Telegram 中的机器人每秒有 30 条消息的限制 API