How to Connect Telegram to an AI Agent (2026)
A Telegram bot token sends as the bot, and the platform refuses to let it message someone who hasn't opened the chat first. Here's how Swytchcode restricts sends to chats the bot has actually seen, keeps the token out of the prompt, and backs bursts of messages with policy.
Key takeaways
- -The bot token is the identity. Anyone holding it controls the bot, and there's no Slack-style workspace boundary anywhere in this API.
- -A bot cannot start a private chat. Swytchcode can restrict sends to chat ids the bot has actually seen in an update, but it can't make Telegram accept a message to someone who hasn't opted in.
- -Privacy mode hides ordinary group chatter from the bot by default. Turning it off is a data decision, not a debugging convenience.
- -Bots never see messages from other bots, in any mode. Two agents in the same group can't read each other without a human bridging them.
- -Telegram's send API has no documented idempotency key, so a retried send is just a new message. Duplicate protection has to come from the calling code, not the platform.
A Telegram bot can't message someone who hasn't started a conversation with it first, no matter how confident an agent sounds about having "texted" them. That's not a bug to work around. It's the platform's permission model, and an agent that doesn't know about it will report success on messages nobody receives.
The agent tells a customer "I texted you on Telegram." The customer sees nothing at all. The bot had never been opened by that person, so Telegram simply refused the private message. In the group where the bot does sit, a later send shows the bot's name, not the employee's. There's no workspace to hide behind here. The token is the identity.
The general problem is that the bot token is the only identity Telegram knows, and the platform refuses to let that identity speak first. Swytchcode's fix is to only let an agent message chats it has actually been let into, and to keep the token itself out of the model's hands entirely.
The problem: a bot can't start the conversation, and the token is the whole identity
Telegram's bot introduction is direct about this. Bots are special accounts, created through @BotFather, which hands back a token. Anyone holding that token has full control of the bot. There's no Slack-style workspace boundary anywhere in this model. A Telegram bot token simply is the bot, in every chat it happens to be in. If an agent and a human both use the same token, they're the exact same sender, and the customer has no way to tell which process actually typed.
Telegram's rule is firm on top of that: bots cannot start conversations with users. A person has to add the bot to a group, or message the bot first themselves, before a private message has anywhere legal to go. An agent that tries to message someone before that happens doesn't fail quietly either. It gets a clear refusal, and retrying that exact send doesn't open the chat for you. The only fix is asking the person to open the bot first.
How Swytchcode solves it
Swytchcode downloads the Telegram method catalog and lets you enable the send-message method on its own, scoped to chats the bot has actually seen in an update, not an open invitation to message any id the model happens to produce. That's a meaningful restriction here specifically because there's no separate "the agent" identity to fall back on; the token is the only sender Telegram recognizes.
There's no .env file in this project. Swytchcode keeps the bot token in its own local store and reads it when a call runs. The token belongs there, not in the model's context, because the model would otherwise be able to send as the bot entirely on its own, without anyone in the loop at all.
Setting up Telegram with Swytchcode
swy search telegram
swy get telegram
swy list methodsThe Telegram catalog page is the method list once the bundle is local. Enable the send method only for chats you've already actually seen in an update, and store the token with swy auth connect rather than anywhere in the prompt.
Policies: build agents you can trust with who it messages and how often
By default, a bot sitting in a group only sees messages that are actually relevant to it — commands aimed directly at it, messages sent via the bot, and replies to its own messages, per Telegram's own FAQ. It never sees the ambient chatter around it. Turning that privacy mode off so the agent "has context" means the bot now receives the entire group's conversation instead, which is a data decision, not a debugging convenience, and deserves to be reviewed as one rather than flipped by default.
Bots also can't see messages from other bots, in any mode. Telegram built it that way specifically to stop loops, so two agents sitting in one group simply won't observe each other's posts. One bot has to be the speaker, or a human has to bridge the two. Design around that instead of assuming it'll work.
Telegram also publishes firm ceilings on how fast a bot can send, in a single chat, in a group, and in bulk, and crossing them doesn't get more forgiving if you retry harder. Treat a refusal to send as "they haven't opened the bot yet" for a new private chat, and a rate refusal as "you crossed the ceiling," never as a reason to hammer the call again. A policy that caps how many sends an agent can fire in a burst, or holds a bulk notification for approval in Slack or Telegram before it goes out, is a better backstop than trusting the model to pace itself.
Telegram's Bot API lets an agent send as a single identity, the bot, into any chat that token has been let into, and there's no username-and-password layer or workspace boundary sitting on top of it the way there is with other chat platforms. The risk isn't that the API is unreliable, it's that a bot can't speak first, can't be told apart from a human using the same token, and can be pushed past a rate ceiling by a loop that doesn't know when to stop. Swytchcode solves that by only allowing the send method for chats the bot has actually seen in an update, keeping the token out of the model's context, and backing bursts of messages with policy instead of trusting the agent to pace itself.
Related reading
- Human approval for a tool call. A hold worth putting before a burst of send calls, on Business and Enterprise.
- A security checklist before company data. What actually changes if you disable privacy mode in a group.
- Log every API call an agent makes. Where a refused send and its chat id should actually be visible.
- API governance tools for AI agents. Why the bot token is really an allowlist problem, not a prompt problem.
FAQ
Can the agent Telegram a customer who has never opened the bot?
No. Telegram is explicit that a bot cannot start the conversation. The person must message the bot or add it to a group first. A private send attempted before that simply fails, and retrying it doesn't open the chat for you.
Will the message appear as the employee?
It appears as the bot, not as any particular person. The token is the bot's identity. Telegram Business can attach a bot to a business account, which is a separate switch in @BotFather, but a standard bot token never impersonates a person on its own.
Why can't the bot see the other agent's message in the group?
Bots don't receive messages sent by other bots, full stop. Privacy mode also hides ordinary group chatter from a bot that was only mentioned occasionally. If two bots genuinely need to cooperate, design a path that doesn't depend on them reading each other directly.
What is the actual send limit before a rate refusal?
Roughly one message a second in a single chat, about 20 a minute in a group, and around 30 a second for bulk notifications. Paid broadcasts cost 0.1 Stars per message above that free bulk rate, and require both a large bot balance and a large monthly audience to even turn on.
How do I avoid processing the same Telegram update twice?
Updates come from either long polling or a webhook, never both at once. The polling call returns the oldest 100 unconfirmed updates, and you confirm them by setting an offset to the last processed update id plus one. Forget that step and the bot processes the same update forever, and may happily send the same reply forever right along with it.
What does it take to unlock paid broadcasts?
Paid broadcasts, enabled in @BotFather, raise the send ceiling toward 1,000 messages a second. Only messages above the free 30-a-second rate are charged, at 0.1 Stars each, drawn from the bot's own balance, and only successful sends get charged. The bot needs at least 100,000 Stars and 100,000 monthly active users before the feature even becomes available.
Can I build an AI agent on top of Telegram with Swytchcode?
Yes. swy get telegram downloads the integration, swy list methods shows the real method IDs, and swy add method turns on the send method for chats the bot has actually seen. The agent calls exec; it never holds the bot token itself.
Can Swytchcode stop an agent from messaging someone who hasn't opted in, even if it's asked to?
Yes, up to a point. A policy can require that the chat id being messaged has actually appeared in an update the bot received, and reject anything else before it reaches Telegram. What Swytchcode can't do is make Telegram accept a message to someone who truly never opened the bot. That refusal comes from Telegram itself, and no policy changes it.
If an agent retries a Telegram send after a timeout, will Swytchcode let it duplicate a message?
This is the one place Telegram's own API doesn't give you a built-in safety net the way a payments API does: there's no documented idempotency key for sending a message, so a resent call is just a new message like any other. Swytchcode's role here is narrower: keep the token out of the prompt and restrict which chats can be messaged. Avoiding duplicate sends on retry has to be handled in the calling code, for instance by not retrying a call that already came back successful.
Use Swytchcode so this doesn't happen to you
Use Swytchcode so an agent never sends as the wrong identity, or hammers a chat past Telegram's rate limit, because it built a chat_id or a retry on its own.
Swytchcode is an execution kernel that sits between your AI agents and the APIs they call. It resolves the right method, injects auth, checks policy, retries safely with idempotency, and logs every call locally.
Start with the MCP quickstart, or browse the docs for the full reference. Free is 100 live commands a month forever; see pricing for Pro and Business.
More content
How to Stop an AI Agent From Booking the Same Meeting Twice
A scheduling agent that checks availability and then books the slot has a gap between those two steps — and a retry can fall straight into it. Here's how Swytchcode closes that gap.
How to Stop an AI Agent From Creating Duplicate Records
A retried "create" call doesn't update an existing contact, ticket, or row — it makes a second one. Here's why that happens across CRMs, databases, and ticketing tools, and how Swytchcode prevents it.
How to Stop an AI Agent From Sending the Same Email Twice
Most email APIs have no idempotency key to catch a repeat send, which makes a duplicate email a harder problem than a duplicate charge. Here's how Swytchcode handles the gap.
