15

I was wondering if I could send a message with my bot on telegram bot api, to multiple chat_id, but I cant figure it out. that's totally because of telegram apis are so hard to understand. I have used this for send a message to one chat_id:

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

1
  • 2
    It doesn't look like this is possible with API as it is - if it is a function you'd like, maybe you can request it Commented Sep 6, 2015 at 7:08

5 Answers 5

26


There is no way to make bot to sendMessage to multiple chat id but there is a trick that can fix it for now :)
Why not sending each chat id a message ?!
Let's look at this example in 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);
}
?>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, But first of all, you should use curl for processing the request, and you can't use foreach for this, because php getting timeout, I think if this is the only way, we should use AJAX for processing
@Omid Foreach is a way to do that i use it for several database based bot s & about file_get_contents i know that it'd throw time outs but i wrote a function before for curl and i use it several times. i wrote file_get_contents to make answer simple. Good Luck
Yeah :) you right, but it's not the answer of this question, so I can't just hit the accept button, you should edit your answer and tell me this is not possible and then try to offer me another way, just like your currently answer
Look at first line i said "There is no way to make bot to sendMessage to multiple chat id but ... " so i said that there is no way to do that but there is a simple trick to do :) good luck
hi farsad, when we send multiple message, is it possible to block from telegram?
6

In addition to @farsad 's answer: Add sleep(NUMBER_OF_SECONDS); inside the foreach loop to not get banned by telegram. As there is a limit of 30 messages per second for bots in Telegram API

1 Comment

Telegram will handle that. I have cron jobs running that send messages to around 1,500 users.
4

The problem with foreach or any other massive sendMessage is that the API will not allow more than ~30 messages to different users per second.

According to Bots FAQ in telegram site:

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.

and solution in Bots FAQ page:

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.

Comments

0

there's a mistake in your code check it should be like this

<?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);
}
?>




?>

Comments

-6

Just for your information.

We could input the chat_ids to database. Query and loop the message section for sending the message to multiple chat-id with sleep().

I am not a programmer. So I could not make an example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.