A Telegram bot developed with TypeScript, integrated with UnifAI SDK, capable of dynamically calling various tools to help users complete tasks.
- Intelligent Conversation: Supports multi-turn conversations with context memory
- Dynamic Tool Calling: Based on UnifAI SDK, capable of searching and using various tools
- Session Management: Automatically manages user sessions with session expiration cleanup
- Error Handling: Comprehensive error handling and retry mechanisms
- Logging System: Detailed logging with support for different levels
- Health Monitoring: Provides health check and system status endpoints
- Graceful Shutdown: Supports graceful shutdown to ensure no data loss
src/
βββ config/ # Configuration files
β βββ index.ts # Environment variable configuration
βββ services/ # Service layer
β βββ chatSessionManager.ts # Session management
β βββ unifaiService.ts # UnifAI SDK integration
β βββ telegramBot.ts # Telegram Bot service
βββ types/ # TypeScript type definitions
β βββ index.ts
βββ utils/ # Utility functions
β βββ logger.ts # Logging utility
βββ index.ts # Application entry point
- Node.js >= 18.0.0
- npm or yarn
- Telegram Bot Token
- UnifAI Agent API Key
git clone <repository-url>
cd unifai-telegram-bot
npm install
# or
yarn install
Copy the environment variable template file and configure it:
cp env.example .env
Edit the .env
file and fill in the necessary configurations:
# Telegram Bot Configuration
TELEGRAM_BOT_TOKEN=your_telegram_bot_token_here
# UnifAI Configuration
UNIFAI_AGENT_API_KEY=your_unifai_agent_api_key_here
# OpenAI Configuration
ANTHROPIC_API_KEY=your_openai_api_key_here
# Server Configuration
PORT=3000
NODE_ENV=development
LOG_LEVEL=info
# Bot Settings
CHAT_HISTORY_LIMIT=10
MAX_MESSAGE_LENGTH=4096
RESPONSE_TIMEOUT=30000
Development mode:
npm run dev
Production mode:
npm run build
npm start
npm run dev
- Start development server (supports hot reload)npm run build
- Build the projectnpm start
- Start production servernpm test
- Run testsnpm run lint
- Code lintingnpm run lint:fix
- Fix code formatting issues
Users can use the following commands in Telegram:
/start
- Start the bot and view welcome message/help
- View help information/clear
- Clear current conversation history/status
- View system running status/tools
- View currently available tools
After the application starts, the following HTTP endpoints are available:
GET /health
- Health check endpointGET /info
- System information endpoint
{
"status": "ok",
"timestamp": "2024-01-01T00:00:00.000Z",
"uptime": 3600,
"memory": {
"rss": 123456789,
"heapTotal": 123456789,
"heapUsed": 123456789,
"external": 123456789
},
"version": "1.0.0"
}
Variable Name | Description | Default Value | Required |
---|---|---|---|
TELEGRAM_BOT_TOKEN |
Telegram Bot Token | - | β |
UNIFAI_AGENT_API_KEY |
UnifAI Agent API Key | - | β |
OPENAI_API_KEY |
OpenAI API Key | - | β |
PORT |
HTTP service port | 3000 | β |
NODE_ENV |
Runtime environment | development | β |
LOG_LEVEL |
Log level | info | β |
CHAT_HISTORY_LIMIT |
Chat history limit | 10 | β |
MAX_MESSAGE_LENGTH |
Maximum message length | 4096 | β |
RESPONSE_TIMEOUT |
Response timeout (ms) | 30000 | β |
-
Telegram Bot Token:
- Contact @BotFather to create a new bot
- Follow the instructions to get the Bot Token
-
UnifAI Agent API Key:
- Visit UnifAI Official Website
- Register an account and get a free Agent API Key
-
OpenAI API Key (optional):
- Visit OpenAI Official Website
- Create an API Key
User: Hello!
Bot: Hello! I am an intelligent assistant powered by UnifAI that can use various tools to help you complete tasks. How can I help you?
User: What topics are trending on Google today?
Bot: Let me help you check the trending topics on Google today...
[Call tools to get information]
Based on the latest data, the trending topics on Google today include...
The bot will automatically search and call appropriate tools based on user needs:
- Search information
- Get weather data
- Calculate and analyze
- Document processing
- And more...
-
Bot not responding
- Check if the Telegram Bot Token is correct
- Check the log file
logs/error.log
- Confirm network connection is normal
-
UnifAI connection failure
- Verify if the UnifAI API Key is valid
- Check network firewall settings
- Check console error messages
-
Tool calling failure
- Confirm UnifAI service status
- Check tool permission settings
- Check detailed error logs
logs/combined.log
- All logslogs/error.log
- Error logs
Set environment variables to enable debug mode:
LOG_LEVEL=debug npm run dev
- config/: Configuration management, reads configuration from environment variables
- services/: Core business logic
chatSessionManager.ts
: Manages user sessions and message historyunifaiService.ts
: UnifAI SDK integration and tool callingtelegramBot.ts
: Telegram Bot API integration
- types/: TypeScript type definitions
- utils/: Utility functions and helper modules
- Create new service classes in the
services/
directory - Define related types in
types/
- Integrate new services in the main application
- Add corresponding test cases
- Use ESLint for code linting
- Follow TypeScript strict mode
- Use meaningful variable and function names
- Add necessary comments and documentation
This project is licensed under the MIT License. See the LICENSE file for details.
Issues and Pull Requests are welcome!
- Fork this project
- 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
) - Create a Pull Request
If you encounter problems or need help:
- Check the Issues page
- Read the troubleshooting section of this document
- Create a new Issue describing the problem
Enjoy using UnifAI Telegram Bot! π