Building a Telegram Bot
In this tutorial, we will walk through how to build a fully functional Telegram Bot using Flame and the Flamer backend framework.
You will learn how to:
- Initialize a new Flame project.
- Add external package dependencies (like
flamer). - Set up an asynchronous web server.
- Intercept incoming Telegram messages and respond via HTTP.
Prerequisites
Section titled “Prerequisites”Before starting, ensure you have:
- The Flame toolchain installed (
cargo run --bin flamelangorflame). - A Telegram Bot Token (obtained by talking to the BotFather on Telegram).
-
Create a New Project
Section titled “Create a New Project”First, initialize a new Flame project using the CLI:
Terminal window flame new telegram_botcd telegram_botThis will generate a standard
flame.tomlmanifest file and asrc/main.fmentry point. -
Add the Flamer Dependency
Section titled “Add the Flamer Dependency”We need the Flamer web backend to handle asynchronous routing and listening. Add
flameras a dependency by using theflame addcommand:Terminal window flame add https://github.com/shoya-129/flamerAlternatively, manually add it to your
flame.toml:[dependencies]flamer = "https://github.com/shoya-129/flamer" -
Setup the Application Entry Point
Section titled “Setup the Application Entry Point”Open
src/main.fmand set up the foundation. We will importstd.net.http,std.json, and ourflamerpackage.import std.net.httpimport std.jsonimport flamer// Replace with your actual Bot Token from BotFatherlet bot_token = "YOUR_BOT_TOKEN_HERE"@Flamer(port: 3000)async fn main() {println("--- Flame Telegram Bot Webhook Server ---")println("Server listening on port 3000.")println("Point your Telegram webhook to: /webhook")// Setup routingflamer.post("/webhook", webhook)// Start listening asynchronouslyawait flamer.listen()}await main() -
Write the Webhook Handler
Section titled “Write the Webhook Handler”Next, we will define our asynchronous
webhookfunction. This function will be triggered byflamerevery time Telegram sends a new message to our/webhookendpoint.async fn webhook(body: Formula) -> Formula {// Parse the incoming JSON request body from Telegramlet data = json.parse(body)// Extract the sender's Chat ID and the message textlet chat_id = data.message.chat.idlet text = data.message.textprintln($"Received from {chat_id}: {text}")let mut reply_text = ""// Simple command routingif text == "/start" {reply_text = "Welcome to Flame Bot! Send me a message."} else {reply_text = $"You said: {text}"}// Build the Telegram API request urllet send_url = $"https://api.telegram.org/bot{bot_token}/sendMessage"// Prepare the JSON payloadlet payload = {chat_id: chat_id,text: reply_text}// Fire the HTTP POST request to Telegram!let send_res = await http.post(send_url, payload)// Print the API response for debuggingprintln($"Telegram response: {send_res.text()}")// Return a successful HTTP 200 responsereturn {ok: true}} -
Setup Webhooks
Section titled “Setup Webhooks”Webhooks Setup (Production)
If you have a public IP or are using a tunnel (likengrok), you can tell Telegram to push updates directly to your running server:Terminal window curl -X POST https://api.telegram.org/bot<YOUR_TOKEN>/setWebhook -d "url=https://your-ngrok-url.app/webhook" -
Run the Bot
Section titled “Run the Bot”Start your application:
Terminal window flame run
Congratulations!
Section titled “Congratulations!”You’ve successfully built a concurrent, native-speed Telegram Bot using Flame. Because Flame integrates with standard asynchronous paradigms (tokio under the hood) and native non-blocking HTTP clients, your webhook handler easily handles thousands of concurrent requests natively without skipping a beat!
