A sophisticated Telegram bot that integrates with the Phyxie AI assistant API from Dify, providing conversational AI capabilities with file upload support.
- π€ AI-Powered Conversations: Chat with the Phyxie AI assistant
- π File Support: Upload images and documents for AI analysis
- π¬ Conversation Management: Create, manage, and clear conversations
- π User Isolation: Each Telegram user has their own conversation context
- π Multiple File Types: Support for images (JPG, PNG, etc.) and documents (PDF, DOCX, XLSX, etc.)
- β‘ Async Operations: Built with async/await for optimal performance
- Python 3.8 or higher
- Telegram Bot Token (from @BotFather)
- Phyxie API Key and Endpoint
- Clone the repository:
git clone https://github.com/shamspias/telegram-dify-bot.git
cd phyxie-telegram-bot- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Configure environment variables:
cp .env.example .envEdit .env and add your credentials:
TELEGRAM_BOT_TOKEN=your_telegram_bot_token_here
PHYXIE_API_KEY=your_phyxie_api_key_here
PHYXIE_API_BASE_URL=http://agents.algolyzerlab.com/v1python main.py/start- Show welcome message/new- Start a new conversation/clear- Delete current conversation and start fresh/help- Show help message
- Start a conversation with
/new - Send text messages for AI responses
- Upload images or documents with optional captions
- Use
/clearto reset the conversation context
The bot supports uploading:
- Images: JPG, JPEG, PNG, GIF, WEBP, SVG
- Documents: PDF, TXT, MD, HTML, XLSX, XLS, DOCX, CSV, PPT, PPTX, XML, EPUB, and more
Maximum file size: 15MB (configurable)
dify-telegram-bot/
βββ bot/
β βββ handlers/ # Command, message, and file handlers
β βββ services/ # API and conversation management
β βββ models/ # Data models and schemas
β βββ utils/ # Helper functions and decorators
βββ config/ # Configuration settings
βββ logs/ # Log files
βββ main.py # Entry point
Additional settings in .env:
LOG_LEVEL=INFO # Logging level (DEBUG, INFO, WARNING, ERROR)
MAX_FILE_SIZE_MB=15 # Maximum file size in MB
ALLOWED_FILE_EXTENSIONS=[jpg,jpeg,png,pdf,docx,xlsx] # Comma-separated listThe bot follows a clean architecture pattern:
- Handlers: Process Telegram updates (commands, messages, files)
- Services: Handle business logic and API communication
- Models: Define data structures and schemas
- Utils: Provide helper functions and decorators
- ConversationManager: Manages user sessions and conversation states
- PhyxieService: Handles API communication with retry logic
- CommandHandlers: Process bot commands
- MessageHandlers: Handle text messages
- FileHandlers: Process file uploads
The bot includes comprehensive error handling:
- Retry logic for API calls
- User-friendly error messages
- Detailed logging for debugging
- Graceful degradation on failures
Logs are stored in the logs/ directory with:
- Console output for development
- File output for production
- Structured logging with contextual information
- API keys are stored in environment variables
- User isolation ensures privacy
- File validation prevents malicious uploads
- Size limits protect against resource exhaustion
- Create new handlers in
bot/handlers/ - Add service methods in
bot/services/ - Define models in
bot/models/schemas.py - Register handlers in
bot/bot.py
# Run with debug logging
LOG_LEVEL=DEBUG python main.py- "TELEGRAM_BOT_TOKEN is required": Ensure
.envfile exists with valid token - "API error: 401": Check your Phyxie API key
- File upload fails: Verify file size and type restrictions
- Connection errors: Check network and API endpoint
Enable debug logging:
LOG_LEVEL=DEBUG python main.py- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For issues and questions:
- Check the logs in
logs/bot.log - Review error messages in the console
- Ensure all environment variables are set correctly
- Built with python-telegram-bot
- Powered by Algolyzer Lab
- Uses async/await for optimal performance