Telegram Bot Setup
This guide walks you through creating a Telegram bot using BotFather and integrating it with the TelegramBot API.Overview
Telegram bots are special accounts that don’t require a phone number. They’re controlled programmatically via the Telegram Bot API. BotFather is Telegram’s official bot for creating and managing other bots.The TelegramBot API uses polling to receive messages. The bot continuously checks for new messages from Telegram servers, implemented in
TelegramPollingService.java.Creating Your Bot
Create a new bot
Send the BotFather will respond and ask for a name for your bot.
/newbot command to BotFather.Choose a name
Send your desired bot name. This is the display name users will see.
This name can contain spaces and special characters.
Choose a username
BotFather will ask for a username. This must:
- End with
bot(e.g.,myawesome_botorMyAwesomeBot) - Be unique across all Telegram bots
- Contain only letters, numbers, and underscores
Configuring Your Bot
Adding the Token to Your Application
Add your bot token to the.env file:
.env
Optional: Customize Bot Settings
You can configure additional bot settings through BotFather:Set Bot Description
Set Bot Description
The description appears when users first interact with your bot.Select your bot and send the description text.
Set About Text
Set About Text
Short text shown in the bot’s profile.
Set Bot Picture
Set Bot Picture
Upload a profile picture for your bot.Select your bot and upload an image.
Set Commands
Set Commands
Define a list of commands that appear in the bot’s menu.Example command list:
Testing Your Bot
Find your bot on Telegram
Search for your bot’s username (e.g.,
@myawesome_bot) in Telegram and start a chat.Send a message
Send any message to your bot. You should receive an AI-generated response.
The first response might take a few seconds as the AI processes your message.
How Telegram Integration Works
Polling Architecture
The application uses a polling mechanism to receive messages:src/main/java/com/acamus/telegrm/infrastructure/adapters/in/scheduler/TelegramPollingService.java
Polling vs Webhooks: This implementation uses polling (regularly checking for updates) rather than webhooks (Telegram pushing updates). Polling is simpler to set up and doesn’t require a public HTTPS endpoint.
Message Flow
Update processing
The update is passed to
ProcessTelegramUpdateUseCase which:- Extracts message content and chat information
- Finds or creates a conversation record
- Saves the user’s message
Telegram Configuration in Code
The bot token is configured in the application through:application.yaml
application.yaml:23-24
TelegramConfig
src/main/java/com/acamus/telegrm/infrastructure/config/TelegramConfig.java
TelegramAdapter
The adapter handles all communication with Telegram’s API:src/main/java/com/acamus/telegrm/infrastructure/adapters/out/telegram/TelegramAdapter.java
Troubleshooting
Bot not responding to messages
Bot not responding to messages
Possible causes:
- Invalid token: Check your
TELEGRAM_BOT_TOKENin.env - Application not running: Ensure
docker-compose upis active - Polling service not started: Check logs for scheduler initialization
- Network issues: Verify your server can reach
api.telegram.org
401 Unauthorized error
401 Unauthorized error
Messages received but no AI response
Messages received but no AI response
The Telegram integration is working, but there’s an issue with AI generation.Check:
- OpenRouter API key is valid (AI Configuration)
- AI service logs for error messages
- OpenRouter account has sufficient credits
Duplicate messages
Duplicate messages
If you’re receiving duplicate bot responses:Possible causes:
- Multiple application instances running
- Offset not being updated correctly in polling
Bot Management Commands
Useful BotFather commands for managing your bot:| Command | Description |
|---|---|
/mybots | List all your bots and manage them |
/token | Generate a new token (revokes old one) |
/revoke | Revoke bot token |
/setname | Change bot name |
/setdescription | Change bot description |
/setabouttext | Change bot about text |
/setuserpic | Change bot profile picture |
/setcommands | Set bot command list |
/deletebot | Delete your bot |
Advanced Configuration
Adjusting Poll Frequency
The default polling interval is 1 second. To adjust this, modify:src/main/java/com/acamus/telegrm/infrastructure/adapters/in/scheduler/TelegramPollingService.java
Shorter intervals provide faster response times but increase API calls. Telegram has rate limits, so don’t poll too frequently.
Next Steps
AI Configuration
Configure AI responses and behavior
Authentication
Secure your REST API endpoints
Environment Config
Review all configuration options
API Reference
Explore available API endpoints