Skip to content
Jingzhe

Custom Bot API server

Telegram supports running a local Bot API server instead of using the cloud service at api.telegram.org. Most bots do not need this, but a local server can be useful when you need larger file transfers, HTTP webhooks, local network webhook delivery, or direct access to server-side file paths.

Official documentation: Using a local Bot API server.

Jingzhe connects to a compatible API root through Bot::with_api_url:

use jingzhe::Bot;
let bot = Bot::with_api_url(token, "http://127.0.0.1:8081")?;

The address is the API root. Do not include bot<token> or a method name.

When both reqwest and ureq are compiled, select a backend explicitly:

use jingzhe::{Bot, HttpBackend};
let bot = Bot::with_api_url_and_backend(
token,
"http://127.0.0.1:8081",
HttpBackend::Reqwest,
)?;

The same constructors exist on ApiClient.

The API root must:

  • Use http or https.
  • Contain a host.
  • Not contain credentials, query parameters, or fragments.

A path prefix is supported and preserved:

let bot = Bot::with_api_url(token, "http://127.0.0.1:8081/telegram")?;

Requests are appended under that prefix as bot<token>/<method>.

The official local Bot API server accepts HTTP webhook URLs, local IP addresses, and arbitrary ports. Jingzhe keeps HTTPS as the safe default and requires an explicit opt-in for HTTP:

use jingzhe::{Bot, WebhookConfig};
let webhook = WebhookConfig::new("http://127.0.0.1:3000/telegram")
.allow_http()
.secret_token(secret);
Bot::with_api_url(token, "http://127.0.0.1:8081")?
.run_webhook(webhook)
.await?;

For a lower-level request:

bot.request(
SetWebhook::new("http://127.0.0.1:3000/telegram")
.allow_http()
.secret_token(secret),
).await?;

Only call allow_http() when the API root is a trusted local or compatible self-hosted server.

Jingzhe defaults to a 20 MiB upload and download limit. Increase the client limit when the local server and your deployment can support larger files:

let bot = bot.file_limits(2_000_000_000, 2_000_000_000)?;

The official method and server limits still apply.

InputFile::path reads a file from the application process and uploads it. It does not automatically mean a path on the local Bot API server. If you rely on server-side local file paths, verify the request format against the official local-server behavior.

Follow Telegram’s operational rules when moving a bot between cloud and local servers:

  1. Call logOut before launching the bot on a local server.
  2. Call close before moving the bot from one local server to another.
  3. Delete the webhook before calling close when required.

Telegram does not guarantee update delivery while the bot is being moved. Plan for downtime and idempotent update handling.