A Telegram bot built with the Embabel framework
Built with Spring Boot 3.5.9, Embabel 0.3.1, and MySQL.
- A Telegram bot token and chat ID (see setup instructions below)
- MySQL database (for survey functionality)
- Create a MySQL database:
CREATE DATABASE telegram_bot_db;- Configure database credentials in
src/main/resources/application.properties:
spring.datasource.username=your_db_user
spring.datasource.password=your_db_passwordOr set via environment variables:
export DB_USERNAME=your_db_user
export DB_PASSWORD=your_db_passwordThe database tables will be created automatically on first run.
- Open Telegram and search for @BotFather
- Send
/newbotand follow the instructions to create a bot - Copy the bot token provided by BotFather
Set your bot token via environment variable:
export TELEGRAM_BOT_TOKEN="your_bot_token_here"Or edit src/main/resources/application.properties:
telegram.bot.token=your_bot_token_hereTo find your chat ID (needed for sending surveys):
- Start a conversation with your bot in Telegram
- Send any message to the bot
- Visit:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates - Look for
"chat":{"id":...}in the response - this is your chat ID
Note:
- Individual chat IDs are positive numbers (e.g.,
8360446449) - Group chat IDs are negative numbers (e.g.,
-123456789)
"Failed to send message: Forbidden"
- The bot doesn't have permission. User must start a conversation with the bot first and send at least one message.
"Failed to send message: Bad Request: chat not found"
- The chat ID is incorrect. Double-check using the
getUpdatesAPI endpoint above.
Start the Embabel Spring Shell:
./scripts/shell.shOr use Maven directly:
mvn clean spring-boot:runAsk questions and collect responses from Telegram users:
# Ask one person
x "Ask user 8360446449 what their favourite colour is"
# Ask multiple people in a group
x "Ask 5 users in group -123456789 what their favorite food is"
# Survey with specific count
x "Survey 3 users in chat -987654321 about their preferred programming language"How it works:
- Survey question is sent to the specified chat
- Users respond in Telegram with their answers
- Responses are automatically collected and stored
- When all expected responses are received, a summary is displayed in the shell:
================================================================================
π SURVEY COMPLETE - ID: 2
================================================================================
Question: What is your favourite colour?
Responses (3/3):
1. Alice: Blue
2. Bob: Red
3. Charlie: Green
================================================================================
The bot uses two specialized agents that handle different parts of the survey workflow:
Handles survey creation and initiation:
- Understands natural language survey requests
- Extracts chat ID, question, and expected response count using AI
- Creates survey records in the database
- Sends the survey question to Telegram
- Returns a SurveyInitiated object with the survey details
Keywords: ask, question, survey, poll, create, send
Handles survey completion and results processing:
- Triggered when a user submits a survey response
- Checks if the survey has received all expected responses
- When complete, analyzes responses using AI to extract insights
- Publishes formatted results back to Telegram
- Marks the survey as completed in the database
Keywords: process, results, analyze, complete
User Request (Natural Language)
β
SurveyInitiationAgent
ββ Parse request with AI
ββ Create survey in database
ββ Send question via Telegram
ββ Return SurveyInitiated
β
[Users respond in Telegram]
β
TelegramBotListener (receives responses)
ββ Store response in database
ββ Trigger SurveyResultsAgent
β
SurveyResultsAgent
ββ Check if survey is complete
ββ If complete: analyze responses with AI
ββ Generate insights summary
ββ Publish results to Telegram
- id (PK)
- chatId
- question
- status (ACTIVE, COMPLETED, CANCELLED)
- expectedCount
- createdAt, completedAt
- summary
- id (PK)
- surveyId (FK)
- userId
- userName
- response
- respondedAt
- id (PK)
- surveyId (FK)
- userId
- oldResponse
- newResponse
- requestedAt
If the bot isn't receiving messages in group chats:
-
Disable privacy mode via @BotFather:
/setprivacy [Select your bot] Disable -
Remove bot from group and re-add it
-
Alternatively, make the bot an admin (admins always receive all messages)
- Check that the correct number of unique users have responded
- Each user can only respond once per survey
- Verify responses are text messages (not photos, stickers, etc.)
- Check logs for any errors
src/main/kotlin/com/embabel/template/
βββ agent/
β βββ SurveyInitiationAgent.kt # Creates and sends surveys
β βββ SurveyResultsAgent.kt # Processes responses and publishes results
βββ bot/
β βββ TelegramBotListener.kt # Receives Telegram updates
βββ domain/
β βββ SurveyCheckInput.kt # Input for checking survey completion
β βββ SurveyInitiated.kt # Survey initiation result
β βββ SurveyResults.kt # Survey results data
βββ entity/
β βββ PendingResponseChange.kt # Pending response modifications
β βββ Survey.kt # Survey entity
β βββ SurveyResponse.kt # Response entity
β βββ SurveyStatus.kt # Status enum
βββ repository/
β βββ PendingResponseChangeRepository.kt # Pending change data access
β βββ SurveyRepository.kt # Survey data access
β βββ SurveyResponseRepository.kt # Response data access
βββ service/
β βββ SurveyService.kt # Survey business logic
βββ tools/
βββ TelegramTools.kt # Telegram messaging
mvn clean installEnsure your database is running and configured, then:
mvn spring-boot:runApache License 2.0 - see LICENSE file for details.
