Sponsored by Deepsite.site

Quirox Telegram Bot Backend

Created By
Himank-J7 months ago
This repo demonstrates how to build a telegram bot using SSE Server and implement agentic flow using MCP with tools like Gmail, GDrive
Content

Quirox Telegram Bot Backend

This project provides the backend services for the Quirox Telegram Bot, featuring an AI agent capable of interacting with various tools like Google Sheets, Gmail (currently commented out), and web search via DuckDuckGo. It uses the Model Context Protocol (MCP) to manage communication between the AI model (Google Gemini) and the available tools. The backend is exposed via a FastAPI web server.

Demo

Watch how Mecro works

Features

  • AI Agent: Uses Google Gemini (Flash model) for reasoning and task execution.
  • Tool Integration:
    • Google Sheets: Create spreadsheets, read/write data, manage sheets (list, copy, rename), share. Uses helpers/ghseet.py (via GoogleSheetsService).
    • Web Search: Search the web using DuckDuckGo (helpers/search_tools.py).
    • Web Content Fetching: Fetch and parse text content from URLs (helpers/search_tools.py).
    • (Experimental/Disabled) Gmail: Functions for sending, reading, and managing emails are present in helpers/gmail_tools.py but appear commented out in mcp_server.py.
  • MCP Framework:
    • mcp_server.py: Runs the MCP server, exposing tools (Sheets, Search, Fetch) to the AI model.
    • mcp_client.py: Communicates with the MCP server, sends user queries to the Gemini model, handles tool calls, and manages the conversation flow.
  • FastAPI Interface:
    • main.py: Starts a FastAPI web server, providing a /chat endpoint to interact with the AI agent via HTTP requests.

Project Structure

├── README.md # This file ├── .env # Environment variables (API keys, paths) - MUST BE CREATED ├── .gitignore # Git ignore patterns ├── requirements.txt # Python dependencies ├── main.py # FastAPI application entry point ├── mcp_client.py # MCP client logic interacting with Gemini and MCP Server ├── mcp_server.py # MCP server exposing tools ├── helpers/ # Directory for helper modules and tools │ ├── config.py # Loads Gemini API key │ ├── gsheet_tools.py # (Potentially older) MCP tool definitions for Google Sheets │ ├── ghseet.py # Standalone GoogleSheetsService class │ ├── gmail_tools.py # Gmail service class and MCP tool definitions (currently unused) │ ├── search_tools.py # DuckDuckGo search and Web Content Fetcher classes │ └── init.py ├── telegram_bot/ # (Empty/Placeholder?) Directory for Telegram bot specific code └── bot/ # (Empty/Placeholder?) Another potential bot directory


## Setup

### 1. Prerequisites

*   Python 3.8+
*   Access to Google Cloud Platform for Sheets/Drive API and potentially Gmail API.
*   Google Gemini API Key.

### 2. Clone the Repository

```bash
git clone <your-repository-url>
cd <repository-directory>

3. Install Dependencies

pip install -r requirements.txt

4. Environment Variables

Create a .env file in the root directory by copying .env.example (if it existed) or creating it from scratch. Add the following variables:

# Google Gemini API Key
GEMINI_API_KEY=your_gemini_api_key

# Google Cloud Credentials (Choose ONE method: Service Account or OAuth)

# Method 1: Service Account (Recommended for servers)
# Path to your service account JSON key file
SERVICE_ACCOUNT_PATH=path/to/your/service_account.json
# Optional: Google Drive Folder ID to work within
DRIVE_FOLDER_ID=your_google_drive_folder_id

# Method 2: OAuth 2.0 (Requires user interaction for the first run)
# Path to your OAuth client secrets file (credentials.json)
CREDENTIALS_PATH=path/to/your/credentials.json
# Path where the OAuth token will be stored after authorization
TOKEN_PATH=token.json # Keep default unless you have a reason to change

# Method 3: Base64 Encoded Service Account Config (Alternative)
# CREDENTIALS_CONFIG=your_base64_encoded_service_account_json_string

Important:

  • Ensure the Google Sheets API and Google Drive API are enabled in your Google Cloud project.
  • If using OAuth (CREDENTIALS_PATH), the first time mcp_server.py (or code using GoogleSheetsService/GmailService) runs, it will open a browser window for you to authorize the application. The resulting token.json will be saved.
  • If using a Service Account (SERVICE_ACCOUNT_PATH), ensure the service account email has been granted appropriate permissions (e.g., Editor) on the target Google Sheets or Drive Folder.

5. Google Cloud Authentication Setup

  • Service Account:
    1. Go to the Google Cloud Console -> IAM & Admin -> Service Accounts.
    2. Create a service account or use an existing one.
    3. Create a key (JSON format) for the service account and download it.
    4. Set SERVICE_ACCOUNT_PATH in .env to the path of the downloaded JSON file.
    5. Share any specific Google Drive folders or Sheets with the service account's email address.
  • OAuth 2.0:
    1. Go to Google Cloud Console -> APIs & Services -> Credentials.
    2. Create "OAuth 2.0 Client ID" of type "Desktop app".
    3. Download the client secrets JSON file.
    4. Rename it to credentials.json (or update CREDENTIALS_PATH in .env).

Usage

The application consists of two main parts that need to run concurrently: the MCP Server and the FastAPI frontend (which uses the MCP Client).

1. Run the MCP Server

This server provides the tools (Sheets, Search) to the AI.

python mcp_server.py

Keep this terminal window open.

2. Run the FastAPI Application

This starts the web server that listens for chat requests.

python main.py
# or using uvicorn directly for more options:
# uvicorn main:app --reload --host 0.0.0.0 --port 8000

3. Interact via API

You can now send POST requests to the /chat endpoint:

Example using curl:

curl -X POST "http://localhost:8000/chat" \
     -H "Content-Type: application/json" \
     -d '{"message": "Create an excel sheet named '\''Test Sheet'\'' and add the headers Name, Age, City in the first row."}'

The response will contain the final answer generated by the AI after potentially interacting with the tools via the MCP server.

Key Modules (helpers/)

  • config.py: Loads the GEMINI_API_KEY from the environment.
  • search_tools.py: Contains DuckDuckGoSearcher for web searches and WebContentFetcher for retrieving content from URLs. Includes rate limiting.
  • ghseet.py: Implements GoogleSheetsService providing a class-based interface to Google Sheets and Drive APIs (Create, Read, Update, Batch Update, Add Rows/Cols, List Sheets, Share, etc.). Handles authentication internally.
  • gmail_tools.py: Implements GmailService (similar structure to GoogleSheetsService) for Gmail operations. Contains MCP tool definitions for email tasks, though currently seems disabled in the mcp_server.py.
  • gsheet_tools.py: Contains older, function-based MCP tool definitions for Google Sheets, likely superseded by ghseet.py.

Dependencies

Key dependencies include:

  • fastapi, uvicorn: For the web server.
  • mcp: Model Context Protocol library.
  • google-api-python-client, google-auth-oauthlib, google-auth: Google API libraries.
  • google-generativeai: Google Gemini client library.
  • python-dotenv: For loading environment variables.
  • httpx, beautifulsoup4: For web search and content fetching.
  • rich: For formatted console output.

See requirements.txt for the full list.

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
ChatWiseThe second fastest AI chatbot™
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Serper MCP ServerA Serper MCP Server
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Playwright McpPlaywright MCP server
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
DeepChatYour AI Partner on Desktop
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
WindsurfThe new purpose-built IDE to harness magic
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
CursorThe AI Code Editor
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Tavily Mcp
Amap Maps高德地图官方 MCP Server