For the complete documentation index, see llms.txt. This page is also available as Markdown.

Proxying

Connecting bots using the proxying method.

Proxying

The Telegram Bot API has two operating schemes:

  1. webhook — when a user writes to the bot, the Telegram server automatically sends an HTTPS request to your bot's address

  2. getUpdates (long polling) — your bot independently requests updates from the Telegram server

To make connecting to the platform simple and fast, we developed a special proxying server that sits between the Telegram server and your bot.

If the bot works via Webhook

You can connect the bot manually (without entering the token) — to do this, when adding the bot, select the corresponding option and follow the instructions. You will need to replace your webhook with ours.

If the bot works via getUpdate

You can connect a bot that works via the getUpdate method in two ways: via the API (see the section "Connecting a bot via the API") or by replacing the api.telegram.org address.

Bots of this type independently request updates from Telegram using HTTPS requests via the getUpdate method. To connect the bot to analytics, you need to change the api.telegram.org address to the address provided by the system when connecting.

After changing the address, our platform will proxy all requests to the api.telegram.org address and collect statistics for you. This address can be used not only for getUpdate requests but also for all other types of requests (sending messages, files, etc.) — all requests are proxied to Telegram's servers unchanged.

How to change the connection address in your bot

Changing the api.telegram.org address is supported by the Telegram Bot API, and most libraries provide a way to change this address.

Instructions for various libraries

If you couldn't find instructions for your library, or you're having trouble connecting, contact our technical support or fill out the form at the bottom of this page.

Python

aiogram/aiogram v3

Repositoryhttps://github.com/aiogram/aiogram

Information on how to change the host is in the documentation at https://docs.aiogram.dev/en/dev-3.x/api/session/custom_server.html

To change the connection address, you need to add this code:

And you need to change the Bot function call by adding the session=session parameter

Full code listing

Note lines 11 and 12 — they import the objects. Lines 51-53 set the address. Line 56 adds the , session=session attribute

aiogram/aiogram v2

Information on how to change the host is in the official documentation

To change the connection address, you need to add this code:

And you need to change the Bot function call by adding the server=server parameter

Full code listing

Note line 3 — it imports the object. Line 12 sets the address. Line 15 adds the , server=server attribute

eternnoir/pyTelegramBotAPI

Repositoryhttps://github.com/eternnoir/pyTelegramBotAPI

To change the host you need to import apihelper and set the API_URL parameter. You can read about how to change the host in the official documentation

Full code listing

For a complete example, we used the example from the official repository at https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/echo_bot.py

Note lines 7 and 11. They import the object and change the address

python-telegram-bot/python-telegram-bot

Repository — https://github.com/python-telegram-bot/python-telegram-bot

Information on how to change the host is in the documentation at https://github.com/python-telegram-bot/python-telegram-bot/wiki/Local-Bot-API-Server

You can change the host using the base_url method. For clarity, we used the basic example from the documentation. Note line 16, where the address is changed via .base_url('https://tgrasp.co/bot')

PHP

telegram-bot-sdk/telegram-bot-sdk

Repositoryhttps://github.com/telegram-bot-sdk/telegram-bot-sdk

The library supports working with multiple bots, so two connection options are shown below

If you have one bot

If you have multiple bots

php-telegram-bot/core

Information on how to change the host is in the documentation at https://github.com/php-telegram-bot/core#using-a-custom-bot-api-server

TelegramBot/Api

Information on how to change the host is in the documentation at https://github.com/TelegramBot/Api?tab=readme-ov-file#local-bot-api-server

At the time of writing this documentation, the ability to change the address is only available in the dev version 2.6. Version 2.5 is installed by default

Update your library version to 2.6

Change "^2.6" in composer.json and add the parameters "minimum-stability": "dev", "prefer-stable": true

Changing the address

When creating the BotApi object, pass our address as the fourth parameter

defstudio/telegraph

Repositoryhttps://github.com/defstudio/telegraph

You can change the connection address in the defstudio/telegraph configuration. Replace the value of the telegram_api_url parameter with the desired address (https://tgrasp.co)

More details on the configuration file in the documentation

JavaScript

yagop/node-telegram-bot-api

To change the host, pass the baseApiUrl: 'https://tgrasp.co' parameter in the options argument when creating the new TelegramBot(token, [options]) object

Full code listing

For a complete example, we used the example from the official repository at https://github.com/yagop/node-telegram-bot-api/blob/master/examples/polling.js

Note line 12, where the address is changed

grammyjs/grammY

Instructions on how to change the address are in the official documentation — https://grammy.dev/guide/api#configuring-grammy-to-use-the-local-bot-api-server

To connect, change the creation of the Bot object

Example

As an example, we used the basic example from the official documentation. Note lines 4-7, where the address is changed

telegraf/telegraf

Repository — https://github.com/telegraf/telegraf

To connect, change the creation of the Telegraf object.

Below is an example of a simple bot with a changed address. Note lines 5-8, where the address is changed. Replace the address with the one you received in the Dashboard. After making the changes, restart the bot.

Go

go-telegram-bot-api/telegram-bot-api

Repository — https://github.com/go-telegram-bot-api/telegram-bot-api

Use the SetAPIEndpoint method to change the connection host

Full example

Note line 17, where the address is changed

tucnak/telebot

Repository — https://github.com/tucnak/telebot

To change the connection host, pass the desired address in the URL parameter

Full example

Note line 13, where the address is changed

Java

rubenlagus/TelegramBots

Repository — https://github.com/rubenlagus/TelegramBots

As an example, we used the repository https://github.com/rubenlagus/TelegramBotsExample

To connect incoming traffic, you need to pass a TelegramUrl object to the TelegramBotsLongPollingApplication.registerBot() function

Note line 8 — this is the standard method for connecting to the TG Bot API. In the next line, we added 2 parameters to this function, including passing the new address for the connection. Replace it with the address the system gave you.

Common errors

All errors related to bot operation appear in the bot errors section in the Dashboard.

Code
Description
Solution

403,404

  1. Make sure you're using the correct connection address. Compare it with the address the system gave you. The tgrasp.co address can only be used if a token was provided.

  2. If you're using tgrasp.co, make sure the bot ID (which the token starts with) matches the one you provided to graspil. The current bot ID in graspil is displayed under the connection address field.

409

Conflict: terminated by other getUpdates request; make sure that only one bot instance is running

This error occurs when Telegram's servers detect two simultaneous getUpdates requests. Make sure you only have one data-receiving thread running

502

502 Bad Gateway

Make sure you're sending a request with the correct structure. Sometimes there are issues on Telegram's servers, and they may respond with this error.

No connection to the server

Specify the correct region for your bot. You should choose the region where your bot's server is physically located.

Use the address provided by the system.

If you couldn't find the instructions you need, write to us in the chat on the website or fill out the form below — we write new instructions on request.

Last updated