start.pyā¢5.53 kB
#!/usr/bin/env python3
"""
Startup script for the Telegram Bot
Handles environment setup and provides helpful information
"""
import os
import sys
import subprocess
import argparse
def check_dependencies():
"""Check if required dependencies are installed"""
try:
import telegram
import requests
return True
except ImportError as e:
print(f"ā Missing dependency: {e}")
return False
def install_dependencies():
"""Install required dependencies"""
try:
print("š¦ Installing dependencies...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
print("ā
Dependencies installed successfully")
return True
except subprocess.CalledProcessError as e:
print(f"ā Failed to install dependencies: {e}")
return False
def load_env_file(env_file="config.env"):
"""Load environment variables from file"""
if os.path.exists(env_file):
print(f"š Loading environment from {env_file}")
with open(env_file, 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
os.environ[key.strip()] = value.strip()
return True
return False
def check_configuration():
"""Check if required configuration is present"""
required_vars = {
'TELEGRAM_BOT_TOKEN': 'Get from @BotFather on Telegram'
}
optional_vars = {
'AGNO_API_URL': 'http://localhost:7777',
'AGENT_ID': 'expense-tracker-agent',
'DEFAULT_USER_ID': 'MUHAMMAD'
}
missing_required = []
print("š§ Configuration Check:")
print("=" * 50)
# Check required variables
for var, description in required_vars.items():
value = os.getenv(var)
if value:
print(f"ā
{var}: {'*' * min(len(value), 20)}...")
else:
print(f"ā {var}: Missing - {description}")
missing_required.append(var)
# Check optional variables
for var, default in optional_vars.items():
value = os.getenv(var, default)
print(f"ā¹ļø {var}: {value}")
return len(missing_required) == 0, missing_required
def show_setup_help():
"""Show setup instructions"""
print("""
š¤ TELEGRAM BOT SETUP GUIDE
=" * 50
1. Create a Telegram Bot:
⢠Message @BotFather on Telegram
⢠Send /newbot and follow instructions
⢠Copy the bot token
2. Configure Environment:
⢠Copy config.env.example to config.env
⢠Set TELEGRAM_BOT_TOKEN=your_token_here
⢠Adjust other settings if needed
3. Start the Bot:
⢠python start.py --run
⢠Or: python bot.py
4. Test the Bot:
⢠Find your bot on Telegram
⢠Send /start to begin
⢠Try: "What's my balance?"
š Useful Links:
⢠@BotFather: https://t.me/BotFather
⢠Telegram Bot API: https://core.telegram.org/bots/api
⢠Agno Agent Docs: http://localhost:7777/docs
""")
def main():
"""Main entry point"""
parser = argparse.ArgumentParser(description="Telegram Bot for Expense Tracker")
parser.add_argument("--run", action="store_true", help="Run the bot")
parser.add_argument("--install", action="store_true", help="Install dependencies")
parser.add_argument("--setup", action="store_true", help="Show setup instructions")
parser.add_argument("--check", action="store_true", help="Check configuration")
args = parser.parse_args()
if args.setup:
show_setup_help()
return
if args.install:
if install_dependencies():
print("ā
Ready to run! Use --run to start the bot")
return
# Load environment file if it exists
load_env_file()
if args.check or not args.run:
config_ok, missing = check_configuration()
if not config_ok:
print(f"\nā Missing required configuration: {', '.join(missing)}")
print("š” Use --setup for setup instructions")
return
else:
print("\nā
Configuration looks good!")
if not args.run:
print("š” Use --run to start the bot")
return
if args.run:
# Check dependencies
if not check_dependencies():
print("š” Use --install to install dependencies")
return
# Check configuration
config_ok, missing = check_configuration()
if not config_ok:
print(f"\nā Missing required configuration: {', '.join(missing)}")
print("š” Use --setup for setup instructions")
return
# Start the bot
print("\nš Starting Telegram Bot...")
try:
from bot import main as run_bot
run_bot()
except KeyboardInterrupt:
print("\nš Bot stopped by user")
except Exception as e:
print(f"\nā Error starting bot: {e}")
else:
# Default: show help
parser.print_help()
print("\nš” Common commands:")
print(" python start.py --setup # Show setup guide")
print(" python start.py --install # Install dependencies")
print(" python start.py --check # Check configuration")
print(" python start.py --run # Start the bot")
if __name__ == "__main__":
main()