A smart Telegram & Zalo bot powered by OpenAI that provides AI-powered conversation and evaluation capabilities with context awareness, rate limiting, and content moderation.
- π€ OpenAI Integration: Uses GPT-4 for intelligent responses
- π¬ Context Awareness: Maintains conversation context through reply messages
- π¦ Rate Limiting: Prevents spam and manages usage quotas
- π‘οΈ Content Moderation: Built-in content filtering and safety checks
- π± Flexible Triggers: Responds to mentions, prefixes, or direct replies
- ποΈ Clean Architecture: Uses dependency injection with Uber FX
- π Webhook Support: Efficient real-time message processing
- π Multi-Channel: Unified handling for Telegram and Zalo conversations
- β‘ High Performance: Concurrent message handling with proper synchronization
- Go 1.24 or higher
- Telegram Bot Token (from @BotFather)
- Zalo Official Account token and webhook secret
- OpenAI API Key
# Clone the repository
git clone https://github.com/nduyhai/valjean.git
cd valjean
# Install dependencies
go mod tidy
# Build the project
go build ./cmd/bot
Create a .env
file or set environment variables:
# Telegram Configuration
TELEGRAM_BOT_TOKEN=your_bot_token_here
TELEGRAM_BOT_USERNAME=your_bot_username
TELEGRAM_WEBHOOK_SECRET=your_webhook_secret
TELEGRAM_REQUIRED_MENTION=true
# Zalo Configuration
ZALO_BOT_TOKEN=your_zalo_oa_token
ZALO_BOT_USERNAME=your_zalo_display_name
ZALO_WEBHOOK_SECRET=your_zalo_webhook_secret
# OpenAI Configuration
OPENAI_KEY=your_openai_api_key
# HTTP Server
HTTP_PORT=8080
make run
docker build -t valjean .
docker run -p 8080:8080 --env-file .env valjean
Deploy the service with HTTPS and configure the Telegram webhook:
# Set webhook URL
curl -X POST \
"https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/setWebhook?url=https://YOUR.DOMAIN/telegram/webhook/$WEBHOOK_SECRET"
Configure the Zalo webhook with the /zl/webhook
endpoint:
curl -X POST "https://openapi.zalo.me/v3.0/oa/webhook" \
-H "access_token: $ZALO_BOT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"oaid": "YOUR_OFFICIAL_ACCOUNT_ID",
"callback_url": "https://YOUR.DOMAIN/zl/webhook",
"secret_key": "YOUR_WEBHOOK_SECRET"
}'
The bot responds to messages in several ways:
- Direct Mention:
@your_bot_username Hello!
- Reply to Bot: Reply to any bot message for continued conversation
- Private Messages: All messages in private chats
Zalo Official Account subscribers are supported through the configured webhook and receive the same evaluation experience.
User: @valjean What is artificial intelligence?
Bot: Artificial intelligence (AI) refers to the simulation of human intelligence...
valjean/
βββ cmd/bot/ # Application entry point
βββ internal/
β βββ adapters/ # External integrations
β β βββ http/ # HTTP handlers
β β βββ llm/openai/ # OpenAI client
β β βββ limiter/ # Rate limiting
β β βββ producer/ # Message producers for Telegram & Zalo
β βββ app/ # Business logic
β β βββ entities/ # Domain entities
β β βββ service/ # Domain services
β β βββ usecase/ # Application use cases
β βββ infra/ # Infrastructure
β β βββ config/ # Configuration
β β βββ fxmodules/ # Dependency injection modules
β β βββ httpserver/ # HTTP server setup
β βββ ports/ # Interface definitions
βββ docs/ # Documentation
βββ Dockerfile # Container configuration
βββ Makefile # Build automation
Environment Variable | Default | Description |
---|---|---|
TELEGRAM_BOT_TOKEN |
- | Your Telegram bot token |
TELEGRAM_BOT_USERNAME |
valjean |
Bot username for mentions |
TELEGRAM_WEBHOOK_SECRET |
- | Secret for webhook security |
TELEGRAM_REQUIRED_MENTION |
true |
Require @mention in groups |
OPENAI_KEY |
- | OpenAI API key |
ZALO_BOT_TOKEN |
- | Zalo Official Account access token |
ZALO_BOT_USERNAME |
valjean |
Display name used for Zalo responses |
ZALO_WEBHOOK_SECRET |
- | Secret for validating Zalo webhook requests |
ZALO_BLOCKED_USERS |
- | Optional list of blocked Zalo user IDs |
ZALO_ALLOWED_USERS |
- | Optional list of allowed Zalo user IDs |
HTTP_PORT |
8080 |
HTTP server port |
# set webhook
curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/setWebhook" \
-H "Content-Type: application/json" \
-d '{
"url": "https://YOUR.DOMAIN/telegram/webhook/SECRET",
"secret_token": "YOUR_HEADER_SECRET"
}'
# check
curl -s "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/getWebhookInfo" | jq
# delete
curl -s "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/deleteWebhook"
# name
curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/setMyName" \
-d "name=Valjean"
# short description (shown in profiles)
curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/setMyShortDescription" \
--data-urlencode "short_description=Reply-and-tag me to analyze messages."
# long description (shown on /start)
curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/setMyDescription" \
--data-urlencode "description=I analyze the message you replied to when you tag me in groups."
Group Privacy Mode (ON/OFF) β must use @BotFather β Bot Settings β Group Privacy β Turn off (Required if you want the bot to trigger on mentions like @YourBot.)
make test
make lint
make build
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Telegram Bot API for bot functionality
- OpenAI for AI capabilities
- Uber FX for dependency injection