igorsavinkin/ai-diet-planner:基于 DeepSeek 的 Telegram 营养计算机器人
igorsavinkin/ai-diet-planner
GitHub 项目 igorsavinkin/ai-diet-planner 是一个 Telegram 营养机器人,用 Harris-Benedict 公式计算用户的 BMR 与 TDEE,并给出减重、维持或增重的个性化热量目标。
Nutrition Bot for Telegram
A comprehensive Telegram bot that helps users calculate their daily caloric needs and generate personalized diet plans using AI-powered menu generation. Now with enhanced user experience and administrative features.
Main Menu
- 1. Features
- 2. Extra Features & Improvements
- 3. Prerequisites
- 4. Installation
- 5. Usage
- 6. Enhanced Usage Flow
- 7. Commands
- 8. Project Structure
- 9. Configuration Details
- 10. Admin Configuration
- 11. Admin Commands
- 12. Data Privacy
- 13. Customization
- 14. Future Enhancement Ideas
- 15. Troubleshooting
- 16. License
- 17. Support
- 18. Disclaimer
1. Features
- Body Data Collection: Collects user information including gender, age, weight, height, activity level, and fitness goals
- BMR & TDEE Calculation: Uses the Harris-Benedict equation to calculate Basal Metabolic Rate and Total Daily Energy Expenditure
- Calorie Target Recommendation: Provides personalized calorie targets based on user goals (weight loss, maintenance, or weight gain)
- AI-Powered Menu Generation: Integrates with DeepSeek AI to generate personalized weekly meal plans
- Case-Insensitive Inputs: Supports user inputs in any case for better usability
- Web & Mobile Compatible: Works seamlessly across Telegram web and mobile apps
2. Extra Features & Improvements
2.1 Enhanced User Experience
- Persistent Data Storage: Your nutritional information is now stored (inside bot memory), allowing you to generate new menus without re-entering your data
- Smart Start Command: The bot recognizes returning users and offers options to generate a new menu or update information
- Multiple Menu Generation: Generate as many menus as you want using your stored data
- Data Management: Clear your stored data at any time with the
/clear_datacommand
2.2 Admin Features
- Access Control: Restricted commands for bot administrators only
- Usage Statistics: View bot usage metrics with
/statscommand - User Management: Get detailed information about specific users with
/userinfo <user_id> - Broadcast Messages: Prepare messages to be sent to all users with
/broadcast <message> - Admin Help: Access admin command documentation with
/admin_help
3. Prerequisites
- Python 3.8 or higher
- A Telegram Bot Token from @BotFather
- (Optional) A DeepSeek API key from DeepSeek Platform for AI menu generation
4. Installation
- Clone or download this project to your local machine
- Install the required dependencies:
pip install python-telegram-bot openai
- Create a
config.pyfile in the project directory with your API tokens:
# config.py # Telegram Bot Token from @BotFather HTTP_API_BOT_TOKEN = "your_telegram_bot_token_here" # DeepSeek API Key (optional - for AI menu generation) DEEPSEEK_API_KEY = "your_deepseek_api_key_here" # Other configuration settings (optional) BOT_ADMINS = [] # Add Telegram user IDs of bot admins if needed LOG_LEVEL = "INFO" # DEBUG, INFO, WARNING, ERROR, CRITICAL
- Ensure your
config.pyfile is included in.gitignoreto prevent accidental exposure of your API keys
5. Usage
- Run the bot:
python main.py
-
Open Telegram and search for your bot username
-
Start a conversation with
/startcommand -
Follow the bot's prompts to provide your information:
- Gender (Male/Female)
- Age
- Weight (kg)
- Height (cm)
- Activity level (No activity, Minimal activity, Medium activity, etc.)
- Goal (Lose weight, Maintain weight, Gain weight)
-
The bot will calculate your BMR, TDEE, and recommended daily calorie intake
-
Choose to generate a personalized weekly menu using AI
6. Enhanced Usage Flow
6.1 For New Users:
- Start with
/startcommand - Provide your body information (gender, age, weight, height)
- Select your activity level and goal
- Receive your calorie calculations
- Generate your personalized weekly menu
- Your data is automatically saved for future use
6.2 For Returning Users:
- Start with
/startcommand - Choose to "Generate New Menu" with existing data or "Update My Information"
- If generating a new menu, receive a fresh menu immediately
- If updating information, go through the data collection process again
7. Commands
/start- Begin the conversation and provide your body data/diet- Generate a basic diet plan after providing your data/weekly_menu- Generate a detailed weekly menu after providing your data/cancel- Cancel the current operation at any time
8. Project Structure
nutrition-bot/
├── main.py # Main bot application
├── config.py # Configuration file (API keys, not in repo)
├── .gitignore # Git ignore file to exclude config.py
└── README.md # This file
9. Configuration Details
The bot uses a separate config.py file for security reasons:
- HTTP_API_BOT_TOKEN: Your Telegram bot token obtained from @BotFather
- DEEPSEEK_API_KEY: (Optional) API key for DeepSeek AI integration
- BOT_ADMINS: (Optional) List of Telegram user IDs who can access admin features
- LOG_LEVEL: (Optional) Logging level for debugging purposes
Note: Remember to keep your API keys secure and never commit them to version control systems. The provided .gitignore file helps prevent accidental exposure of your sensitive information.
10. Admin Configuration
To use admin features, add administrator user IDs to your config.py:
# config.py HTTP_API_BOT_TOKEN = "your_telegram_bot_token_here" DEEPSEEK_API_KEY = "your_deepseek_api_key_here" # Add Telegram user IDs of administrators BOT_ADMINS = [123456789, 987654321] # Replace with actual user IDs LOG_LEVEL = "INFO"
10.1 Finding Your Telegram User ID
- Start a conversation with @userinfobot on Telegram
- Send any message to the bot
- It will reply with your user ID
11. Admin Commands
/stats- View bot usage statistics and metrics/broadcast <message>- Prepare a message to be sent to all users/userinfo <user_id>- Get detailed information about a specific user/admin_help- Show help for all admin commands
Note: The admin features provide powerful tools for managing your bot. Use them responsibly and keep your admin user IDs secure. Always inform users about data collection practices and provide options to delete their data.
12. Data Privacy
- User data is stored in memory and will be lost when the bot restarts
- Users can clear their data at any time with
/clear_data - Admin features only work for users whose IDs are in the
BOT_ADMINSlist - For production use, consider implementing a proper database solution
13. Customization
You can customize the bot by:
- Modifying the activity levels and their multipliers in the
ACTIVITY_LEVELSdictionary - Adjusting the calorie deficit/surplus values in the goal calculation
- Changing the macronutrient distribution ratios in the diet plan
- Customizing the AI prompt in the
generate_weekly_menufunction
14. Future Enhancement Ideas
- Database Integration: Replace in-memory storage with a proper database
- Menu History: Keep a history of generated menus for each user
- Progress Tracking: Allow users to track their progress over time
- Food Database: Integrate with a food database for more accurate calorie counting
- Multi-language Support: Add support for multiple languages
15. Troubleshooting
15.1 Common Issues
-
Buttons not visible in Telegram Web:
- The web version of Telegram has limited support for custom keyboards
- The bot includes text instructions as a fallback for web users
-
API errors:
- Ensure your DeepSeek API key is valid and has sufficient credits
- Check your internet connection
-
Message too long:
- The bot automatically splits long messages to comply with Telegram's limits
-
Admin commands not working:
- Ensure your user ID is correctly added to the BOT_ADMINS list in config.py
- Restart the bot after updating the configuration
-
Data not persisting after bot restart:
- This is expected behavior with the current in-memory storage
- Consider implementing a database for production use
-
Menu generation taking too long:
- The AI processing time depends on the DeepSeek API response time
- Large menus may take several seconds to generate
15.2 Logs
The bot generates logs that can help with debugging. Check the console output for any error messages.
16. License
This project is open source and available under the MIT License.
17. Support
If you encounter any issues or have questions about the bot, please check the troubleshooting section above or create an issue in the project repository.
18. Disclaimer
This bot provides nutritional information for educational purposes only. It is not a substitute for professional medical advice, diagnosis, or treatment. Always seek the advice of qualified health providers with questions about medical conditions.
Author: Igor Savinkin, webscraping.pro
来源:GitHub · github.com