Skip to content

Latest commit

Β 

History

History
398 lines (305 loc) Β· 11.8 KB

File metadata and controls

398 lines (305 loc) Β· 11.8 KB

πŸš€ nanobot-serverless-deploy

One-Click Serverless Deployment for nanobot AI Assistant

Deploy nanobot to popular serverless platforms with zero local setup required. Access your AI assistant from Telegram anywhere, anytime.

Python License Platform

✨ Features

  • 🎯 One-Command Deployment - Deploy to Google Cloud Run with a single script
  • πŸ’° Free Tier Support - Runs entirely on GCP Always-Free tier
  • πŸ”„ 24/7 Availability - No need to keep your computer running
  • πŸ“± Telegram Integration - Full Telegram bot support via webhooks
  • πŸ’Ύ Persistent Storage - All data stored in Google Cloud Storage (chat history, sessions, files)
  • ⚑ Serverless Optimized - Cold start optimization and automatic scaling

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Google Cloud Run (Serverless)        β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  FastAPI HTTP Handler             β”‚  β”‚
β”‚  β”‚  - Telegram webhook endpoint      β”‚  β”‚
β”‚  β”‚  - Health check endpoint          β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚           β”‚                              β”‚
β”‚           β–Ό                              β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  nanobot Gateway Adapter          β”‚  β”‚
β”‚  β”‚  - Initialize from env vars       β”‚  β”‚
β”‚  β”‚  - Handle webhook events          β”‚  β”‚
β”‚  β”‚  - Sync files to/from GCS         β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚           β”‚                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚
            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   External Services                      β”‚
β”‚  - Google Cloud Storage (persistent)    β”‚
β”‚  - OpenRouter API (LLM)                 β”‚
β”‚  - Brave Search API (optional)          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Tip

Get API keys: OpenRouter (LLM) Β· Brave Search (optional, for web search) You can also change the model to minimax/minimax-m2 for lower cost.

Prerequisites

Before deploying, you'll need:

  1. OpenRouter API Key - Get from OpenRouter.ai
  2. Telegram Bot Token - Create a bot with @BotFather
  3. Telegram User ID - Get from @userinfobot
  4. Google Cloud Platform Account - Create at cloud.google.com

Deploy to Google Cloud Run

1. Clone repository

git clone elliot35/nanobot-serverless-deploy
cd serverlessbot

2. Configure environment

# Copy template
cp .env.example .env

# Edit .env file with your credentials
# Required:
# - NANOBOT_OPENROUTER_API_KEY
# - TELEGRAM_BOT_TOKEN
# - TELEGRAM_ALLOWED_USERS
# Optional:
# - GCS_BUCKET_NAME (auto-created if not set)
# - NANOBOT_MODEL (defaults to anthropic/claude-opus-4-5)
# - BRAVE_SEARCH_API_KEY

3. Set GCP project

export GCP_PROJECT_ID=your-project-id
# Or set it permanently:
gcloud config set project your-project-id

4. Load environment variables

# For bash/zsh:
set -a
source .env
set +a

5. Deploy

./gcp/deploy.sh

The script will:

  • βœ… Enable required GCP APIs
  • βœ… Create GCS bucket for persistent storage
  • βœ… Build and push Docker image
  • βœ… Deploy to Cloud Run
  • βœ… Provide service URL and next steps

6. Set Telegram webhook

After deployment, the script will output your service URL. Then:

curl -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/setWebhook?url=https://your-service-url.run.app/api/webhook/telegram"

7. Test

Send a message to your bot on Telegram - it should respond!

πŸ’Ύ Storage

All data is automatically stored in Google Cloud Storage:

Data Type Storage Location Format
Chat History sessions/{session_key}/chat_history.jsonl JSONL (one message per line)
Sessions sessions/{session_key}/session.json JSON
Agent Actions sessions/{session_key}/agent_actions.jsonl JSONL
Files sessions/{session_key}/files/ Any file type

Automatic syncing:

  • Files are synced from GCS before processing (agent can access previous files)
  • Chat history is loaded automatically (up to 50 previous messages)
  • Files are synced to GCS after processing (persists across invocations)

πŸ“‹ Environment Variables

Required

Variable Description Example
NANOBOT_OPENROUTER_API_KEY OpenRouter API key for LLM access sk-or-v1-xxx
TELEGRAM_BOT_TOKEN Telegram bot token from @BotFather 123456:ABC...
TELEGRAM_ALLOWED_USERS Comma-separated list of allowed user IDs 123456789,987654321

Optional

Variable Description Default
GCS_BUCKET_NAME GCS bucket name for storage {PROJECT_ID}-nanobot-files
GCP_PROJECT_ID GCP project ID Auto-detected from gcloud
NANOBOT_MODEL LLM model identifier anthropic/claude-opus-4-5
BRAVE_SEARCH_API_KEY Brave Search API key -
LOG_LEVEL Logging level (DEBUG, INFO, WARNING, ERROR) INFO

See .env.example for a complete example.

πŸ§ͺ Testing

Health Check

curl https://your-service-url.run.app/api/health

Expected response:

{
  "status": "healthy",
  "checks": {
    "bus": true,
    "agent_loop": true,
    "session_manager": true,
    "config": true,
    "workspace": true,
    "storage_manager": true,
    "gcs": true
  }
}

View Logs

gcloud run services logs read nanobot-gateway \
  --region us-central1 \
  --project your-project-id \
  --limit 50

πŸ—‘οΈ Cleanup

To remove all deployed resources:

export GCP_PROJECT_ID=your-project-id
./gcp/destroy.sh

This will delete:

  • βœ… Telegram webhook
  • βœ… Cloud Run service
  • βœ… Container images
  • ⚠️ GCS bucket (optional, with confirmation)

Note: GCS data (sessions, chat history, files) will be preserved unless you choose to delete the bucket.

πŸ’° Free Tier Limits

Google Cloud Run (Always-Free)

  • βœ… 2 million requests/month
  • βœ… 360,000 GB-seconds compute/month
  • βœ… 180,000 vCPU-seconds/month
  • βœ… 1 GB egress/month
  • ⚠️ Requires billing account (but stays within free tier for small usage)

Google Cloud Storage (Always-Free)

  • βœ… 5GB storage/month
  • βœ… 1GB egress/month
  • βœ… Automatic scaling
  • ⚠️ Requires billing account (but stays within free tier for small usage)

πŸ› Troubleshooting

Health check returns "unhealthy"
  1. Check logs for detailed error messages:

    gcloud run services logs read nanobot-gateway --region us-central1 --project your-project-id --limit 50
  2. Common issues:

    • Missing environment variables (especially GCS_BUCKET_NAME)
    • Invalid API keys or tokens
    • GCS bucket permissions issues
    • Import errors (check that nanobot is installed correctly)
Bot not responding
  1. Check webhook is set correctly:

    curl "https://api.telegram.org/bot<TOKEN>/getWebhookInfo"
  2. Verify webhook URL matches your deployment URL exactly

  3. Check logs for errors:

    gcloud run services logs read nanobot-gateway --region us-central1
  4. Verify environment variables are set correctly in Cloud Run

  5. Check user ID is in TELEGRAM_ALLOWED_USERS (comma-separated, no spaces)

GCS storage errors
  1. Verify bucket exists:

    gsutil ls gs://your-bucket-name
  2. Check bucket permissions - Cloud Run service account needs storage.objectAdmin role:

    gsutil iam ch serviceAccount:PROJECT_ID@appspot.gserviceaccount.com:roles/storage.objectAdmin gs://your-bucket-name
  3. Verify GCS_BUCKET_NAME environment variable is set correctly

  4. Check GCS API is enabled:

    gcloud services enable storage-api.googleapis.com --project your-project-id
Cold start delays
  • First request may take 5-10 seconds (cold start)
  • Subsequent requests are faster (warm instances)
  • Consider increasing min instances for production:
    gcloud run services update nanobot-gateway \
      --min-instances 1 \
      --region us-central1

πŸ“ Project Structure

serverlessbot/
β”œβ”€β”€ README.md                    # This file
β”œβ”€β”€ .env.example                 # Environment variables template
β”œβ”€β”€ gcp/
β”‚   β”œβ”€β”€ main.py                 # Cloud Run FastAPI app
β”‚   β”œβ”€β”€ requirements.txt        # Python dependencies
β”‚   β”œβ”€β”€ Dockerfile              # Docker image for Cloud Run
β”‚   β”œβ”€β”€ cloudbuild.yaml         # Cloud Build configuration
β”‚   β”œβ”€β”€ deploy.sh               # One-command deployment script
β”‚   └── destroy.sh              # Cleanup script
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ adapter.py              # nanobot gateway adapter
β”‚   β”œβ”€β”€ config.py               # Environment-based config loader
β”‚   β”œβ”€β”€ storage.py              # GCS persistent storage
β”‚   └── handlers.py             # HTTP request handlers
└── scripts/
    └── setup_mongodb.py        # (Deprecated - no longer needed)

πŸ”§ Local Development

# Clone repository
git clone <YOUR_REPO_URL>
cd serverlessbot

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r gcp/requirements.txt

# Copy environment template
cp .env.example .env

# Edit .env file with your credentials

# Load environment variables
set -a
source .env
set +a

# Run locally
python -m uvicorn gcp.main:app --host 0.0.0.0 --port 8080

# Test health endpoint
curl http://localhost:8080/api/health

πŸ“š Additional Resources

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Built for nanobot by HKUDS
  • Inspired by the need for easy serverless deployment

Made with ❀️ for the nanobot community

Get Started β€’ Troubleshooting β€’ Cleanup