Custom Bot API server
Overview
Section titled “Overview”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.
Explicit HTTP backend
Section titled “Explicit HTTP backend”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.
URL rules and path prefixes
Section titled “URL rules and path prefixes”The API root must:
- Use
httporhttps. - 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>.
HTTP webhooks
Section titled “HTTP webhooks”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.
File limits
Section titled “File limits”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.
Switching servers
Section titled “Switching servers”Follow Telegram’s operational rules when moving a bot between cloud and local servers:
- Call
logOutbefore launching the bot on a local server. - Call
closebefore moving the bot from one local server to another. - Delete the webhook before calling
closewhen required.
Telegram does not guarantee update delivery while the bot is being moved. Plan for downtime and idempotent update handling.