Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 37 ยท Messages and Embeds

Message Length and Rate Limits

Split long bot messages at safe boundaries for Telegram (4096) and Discord (2000), keep code fences intact, and build a rate-limited send queue with per-chat and global limits for broadcasts.

Two practical limits shape every bot that talks a lot. First, a message has a maximum length: exceed it and the API answers 400. Second, you may send only so many messages per second: exceed that and you get 429, or worse, a temporary ban. Neither limit is exotic, but ignoring them is the most common reason a promising bot falls over the first time a user asks for a long report or an admin broadcasts to ten thousand people. This lesson builds the two tools that solve both: a splitter and a send queue.

The numbers

Limit Telegram Discord
Text per message 4096 characters 2000 characters (content)
Caption / embed text 1024 characters caption 4096 description, 6000 per message
Messages to one chat about 1 per second (bursts allowed) per-channel bucket, roughly 5 per 5 seconds
Messages to one group at most about 20 per minute per-channel bucket
Broadcast to many users about 30 messages per second overall global limit about 50 requests per second

These are guidelines from the documentation and from experience; the platforms may change them, and Discord reports the exact numbers in response headers (see Discord REST API and Snowflakes). Design your code so that the numbers are configuration, not constants buried in logic. Telegram's limits are measured in characters of the text after entity parsing; Discord counts UTF-16 characters of content. A single emoji can count as two.

Splitting long text

The naive split, text.match(/.{1,2000}/gs), cuts words, sentences and code in half. A good splitter prefers boundaries in this order: paragraph break, line break, sentence or space, and only as a last resort a hard cut. It must also never exceed the limit, even for a single 5,000-character line without spaces.

Node.js A boundary-aware message splitter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The 30% threshold stops the splitter from choosing a break that is so early it wastes most of the message. Keep this function pure: text and limit in, array out, easy to test with awkward inputs such as empty strings and a single huge word.

Keeping code blocks intact

If a chunk boundary falls inside a fenced code block, the first message ends with an unclosed fence and the second starts without one, so both render wrongly. The fix is to track whether you are inside a fence at the cut. If you are, close the fence at the end of the chunk and reopen it (with the same language) at the start of the next.

Node.js Closing and reopening code fences across chunks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Every chunk has an even number of fence lines, so each renders as a complete code block. When you send the chunks, send them in order, one at a time (awaiting each), because parallel sends can arrive out of order.

A send queue with rate limits

Instead of calling sendMessage directly everywhere, push messages into one queue that spaces them out. The queue enforces two constraints at once: a global rate (for example 30 per second) and a per-chat rate (for example 1 per second). Whichever limit is tighter at the moment decides when a message may leave.

The example below uses a virtual clock so that the output is deterministic and instant: the scheduler computes the send time of each message instead of waiting. A real queue would await sleep() until that time.

Node.js Scheduling sends under global and per-chat limits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The broadcast of 90 messages takes about three seconds, and no one-second window holds more than the 30 messages allowed. That is the behaviour you want for a "message all users" feature: it takes a little longer but never trips the limiter. For a big audience, also persist the queue (a database table of pending messages) so that a restart does not lose it.

Handling failures in a broadcast

A broadcast meets three kinds of failure and treats each differently:

  • 429 Too Many Requests: wait for retry_after seconds, then retry the same message. Also slow the scheduler down for a while.
  • 403 blocked or deactivated: mark the user inactive and do not retry.
  • 5xx or network error: retry a few times with backoff, then give up and log it.

Record a result for every recipient (sent, blocked, failed), so a restarted broadcast resumes instead of messaging everyone twice. Never keep an in-memory list as your only progress record.

Ignoring retry_after is the fastest way to a temporary ban. If a platform asks you to wait 30 seconds, wait 30 seconds; hammering the API in a loop makes the penalty longer.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is wrong with text.match(/.{1,2000}/gs) as a message splitter?

    A fixed-size cut ignores structure. Prefer paragraph, line and space boundaries, and keep fences balanced.
  2. 2. A chunk boundary falls inside a fenced code block. What should the splitter do?

    Closing and reopening the fence lets each chunk render as a complete code block.
  3. 3. Why does a send queue enforce both a global and a per-chat limit?

    Each limit can be the binding one, so a message may leave only when both allow it.
  4. 4. A broadcast gets 403 for a recipient. What should you do?

    403 means the user blocked the bot or is deactivated. Retrying cannot succeed.
  5. 5. Why send message chunks one at a time, awaiting each?

    Sequential awaits keep the chunks in order.

Summary

  • Telegram allows 4096 characters per message, Discord 2000; keep limits in configuration.
  • Split at paragraph, line and space boundaries, with a hard cut only as a last resort, and balance code fences across chunks.
  • Route all outgoing messages through a queue that respects a global rate and a per-chat rate.
  • On 429 wait retry_after; on 403 stop messaging that user; retry 5xx with backoff.
  • Persist broadcast progress so restarts do not duplicate or lose messages.

Next: Keeping Bot State in Memory