Skip to content

Repository files navigation

F1 Agent with Telegram and Google Services Integration

A powerful agent that receives Telegram messages, fetches Formula 1 standings, creates Google Sheets with the data, and sends emails with the results.

Features

  • Receives messages from Telegram via Server-Sent Events (SSE)
  • Detects requests for F1 standings
  • Fetches and displays current F1 championship standings
  • Creates Google Sheets with detailed standings data
  • Sends emails with links to the Google Sheets
  • Nicely formatted console output for F1 standings

Overview

The F1 agent connects to a Telegram channel via SSE, monitors for specific trigger phrases, and responds by fetching the latest Formula 1 driver standings. It displays these standings in a formatted table in the console and, when configured with Google API access, creates a Google Sheet with the data and sends an email with a link to the sheet.

Components

  • f1_agent.py: Main agent that processes Telegram messages and coordinates actions
  • gdrive_server.py: Google Drive API integration for creating spreadsheets
  • gmail_server.py: Gmail API integration for sending emails
  • telegram_server.py: Handles the SSE connection to Telegram

Installation

Prerequisites

  • Python 3.7+
  • A Google account (for full features)
  • A running Telegram SSE server at http://127.0.0.1:8000/sse

Setting up the Environment

  1. Install the required dependencies:

    pip install -r requirements.txt

    Key dependencies include:

    • python-dotenv
    • httpx
    • google-api-python-client
    • google-auth
    • google-auth-oauthlib
    • gspread
  2. If using uv instead of pip:

    uv pip install -r requirements.txt

Google API Integration

The F1 agent can work in two modes:

Basic Mode: Without Google API Integration

In this mode, the agent will:

  • Connect to Telegram and receive messages
  • Fetch and display F1 standings in the console
  • Not create Google Sheets or send emails

To run in basic mode (if Google API dependencies are not installed):

python f1_agent.py

Full Mode: With Google API Integration

To enable full functionality with Google Sheets and Gmail:

  1. Install Google API dependencies:

    pip install google-api-python-client google-auth google-auth-oauthlib gspread
  2. Set up Google API credentials:

    • Go to the Google Cloud Console
    • Create a new project
    • Enable the Gmail API, Google Drive API, and Google Sheets API
    • Create OAuth credentials for a desktop application
    • Download the credentials as credentials.json and place in the project root
  3. Generate the authentication token:

    python create_token.py

    This will open a browser window for you to authorize the application.

  4. Set your email in a .env file:

    USER_EMAIL=your.email@example.com
    
  5. Run the F1 agent:

    python f1_agent.py

Usage

  1. Make sure your Telegram SSE server is running at http://127.0.0.1:8000/sse

  2. Run the F1 agent:

    python f1_agent.py
  3. Send a message to your Telegram bot containing the phrase:

    Find the Current Point Standings of F1 Racers
    
  4. The agent will:

    • Detect the request
    • Fetch current F1 standings
    • Display them in the console
    • If Google integration is enabled:
      • Create a Google Sheet with the standings
      • Send an email with a link to the sheet

Troubleshooting

Google API Issues

  1. Missing Google Dependencies:

    ModuleNotFoundError: No module named 'googleapiclient'
    

    Solution: Install the required Google packages:

    pip install google-api-python-client google-auth google-auth-oauthlib gspread
    
  2. Authentication Problems:

    • Ensure credentials.json is in the project root
    • Run create_token.py to refresh your token
    • Check that your Google Cloud project has the necessary APIs enabled
  3. Email Formatting Issues: If you see JSON or raw data in your email instead of formatted content, this has been fixed in the latest version of the code.

Telegram Connection Issues

  • Verify that your Telegram SSE server is running at http://127.0.0.1:8000/sse
  • Check the logs for connection errors
  • The agent will automatically retry connections if they fail

Customization

Changing Google Services Configuration

The F1 agent uses the following server configuration for Google services:

SERVER_CONFIGS = [
    {
        "id": "gdrive",
        "type": "stdio",
        "script": "gdrive_server.py",
        "cwd": os.getcwd()
    },
    {
        "id": "gmail",
        "type": "stdio",
        "script": "gmail_server.py", 
        "cwd": os.getcwd()
    }
]

Changing Email Recipients

To change the email recipient, modify the .env file or update the user_email variable in f1_agent.py.

Project Structure

  • Core files:

    • f1_agent.py: Main application
    • gdrive_server.py: Google Drive integration
    • gmail_server.py: Gmail integration
    • telegram_server.py: Telegram SSE client
    • models.py: Data models for racers and standings
  • Core folder: Contains essential framework components

    • core/session.py: Implements MultiMCP for service communication
    • core/loop.py: Implements AgentLoop for agent lifecycle
    • core/strategy.py: Contains decision-making logic
    • core/context.py: Manages agent context and state
  • Modules folder: Contains functional components

    • modules/tools.py: Tool implementations
    • modules/perception.py: Input processing
    • modules/memory.py: State management
    • modules/decision.py: Decision-making functionality
    • modules/action.py: Action execution
    • modules/model_manager.py: Model management utilities
  • Config folder: Contains configuration files

    • config/profiles.yaml: Agent configuration (identity, strategy, memory settings)
    • config/models.json: Model configurations
  • Configuration files:

    • requirements.txt: Python dependencies
    • .env: Environment variables (create this yourself)
    • credentials.json: Google API credentials (obtain from Google Cloud)
  • Authentication files:

    • token.pickle: Authentication token for Google services

Source Control

When using Git for version control, make sure to exclude the following files:

  • Telegram session files (*.session, *.session-journal)
  • Google authentication files (credentials.json, token.pickle)
  • Environment files (.env)

These files contain sensitive authentication information and should not be shared publicly.

You can use the following in your .gitignore file:

# Telegram sessions
*.session
*.session-journal

# Google authentication
credentials.json
token.pickle

# Environment variables
.env

# Python
__pycache__/
*.py[cod]
*$py.class
.venv/

Cleaning Sensitive Files

To remove sensitive files that shouldn't be committed to Git, you can use the included cleanup script:

python cleanup_sensitive_files.py

This script will:

  • Remove all Telegram session files (*.session, *.session-journal)
  • Delete authentication files (credentials.json, token.pickle)
  • Clean up environment files (.env)
  • Remove Python cache directories

Note: After cleaning, you'll need to set up your credentials again to use Google services.

License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages