This is a production-ready version of the Telegram bot optimized for Google Cloud Run deployment with webhook support and Firestore database.
- Webhook-based architecture (no polling)
- Firestore database integration
- Multi-language support (English/Chinese) with AI translations
- Auto-scaling with Cloud Run
- HTTPS endpoints with valid SSL certificates
- Containerized deployment with Docker
- Google Cloud Project with billing enabled
- Telegram Bot Token from @BotFather
- OpenRouter API Key (for translations)
- Google Cloud SDK installed locally
Enable these APIs in your Google Cloud Console:
gcloud services enable run.googleapis.com
gcloud services enable cloudbuild.googleapis.com
gcloud services enable firestore.googleapis.com
gcloud services enable secretmanager.googleapis.comtelegram-bot-cloud-run/
├── server.js # Express server with webhook handling
├── database.js # Firestore database adapter
├── config.js # Configuration management
├── keyboards.js # Telegram keyboard layouts
├── languages.js # Multi-language support
├── chinese-translator.js # AI translation service
├── handlers/ # Message and callback handlers
│ ├── menu.js
│ ├── products.js
│ └── cart.js
├── scripts/ # Utility scripts
│ ├── set-webhook.js # Configure Telegram webhook
│ ├── delete-webhook.js # Remove Telegram webhook
│ └── migrate-data.js # Migrate SQLite to Firestore
├── Dockerfile # Container configuration
├── cloudbuild.yaml # Cloud Build configuration
├── package.json # Dependencies
├── .env.example # Environment variables template
├── .gcloudignore # Files to ignore in deployment
└── README.md # This file
Create .env file from template:
cp .env.example .envEdit .env with your values:
TELEGRAM_BOT_TOKEN=your_telegram_bot_token
GOOGLE_CLOUD_PROJECT_ID=your-project-id
OPENROUTER_API_KEY=your_openrouter_keynpm install- Go to Google Cloud Console
- Navigate to Firestore
- Create database in Native mode
- Choose a location close to your users
If you have existing SQLite data:
# Make sure GOOGLE_CLOUD_PROJECT_ID is set in .env
node scripts/migrate-data.js# Deploy directly from source
gcloud run deploy telegram-bot \
--source . \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--set-env-vars "NODE_ENV=production"# Submit build to Cloud Build
gcloud builds submit --config cloudbuild.yamlAfter deployment, you'll get a Cloud Run URL like:
https://telegram-bot-abc123-uc.a.run.app
Set up the webhook:
# Update WEBHOOK_URL in .env with your Cloud Run URL
WEBHOOK_URL=https://telegram-bot-abc123-uc.a.run.app
npm run set-webhookSet sensitive environment variables:
gcloud run services update telegram-bot \
--set-env-vars="TELEGRAM_BOT_TOKEN=your_token" \
--set-env-vars="OPENROUTER_API_KEY=your_key" \
--set-env-vars="GOOGLE_CLOUD_PROJECT_ID=your_project"- Create secrets:
echo "your_telegram_token" | gcloud secrets create telegram-bot-token --data-file=-
echo "your_openrouter_key" | gcloud secrets create openrouter-api-key --data-file=-- Update Cloud Run service:
gcloud run services update telegram-bot \
--set-secrets="/secrets/telegram-token=telegram-bot-token:latest" \
--set-secrets="/secrets/openrouter-key=openrouter-api-key:latest"gcloud run services logs read telegram-bot --region us-central1- Go to Cloud Console → Cloud Run → telegram-bot
- View metrics, logs, and scaling behavior
- Set up alerts for errors or high latency
# Install dependencies
npm install
# Start development server (uses polling mode locally)
npm run dev# Use ngrok to expose local server
ngrok http 8080
# Set webhook to ngrok URL
WEBHOOK_URL=https://abc123.ngrok.io npm run set-webhookgcloud run deploy telegram-bot --source .gcloud run services update telegram-bot \
--set-env-vars="KEY=value"gcloud run services update telegram-bot \
--min-instances=0 \
--max-instances=10 \
--concurrency=80-
Webhook not receiving updates
- Check webhook URL is correct and accessible
- Verify SSL certificate is valid
- Run:
npm run set-webhookto reconfigure
-
Database connection errors
- Ensure Firestore is enabled in Google Cloud
- Check service account permissions
- Verify GOOGLE_CLOUD_PROJECT_ID is correct
-
Translation not working
- Check OPENROUTER_API_KEY is valid
- Ensure API has sufficient quota
- Health check:
GET https://your-url.run.app/ - Bot info:
GET https://your-url.run.app/bot-info
# Check current webhook status
curl "https://api.telegram.org/bot$BOT_TOKEN/getWebhookInfo"
# Delete webhook (for debugging)
npm run delete-webhook
# View Cloud Run service details
gcloud run services describe telegram-bot --region us-central1Cloud Run free tier includes:
- 2 million requests/month
- 360,000 GB-seconds memory
- 180,000 vCPU-seconds
- Set
--min-instances=0for zero cost when idle - Use
--memory=512Mifor smaller memory footprint - Monitor usage in Cloud Console
Cloud Run automatically scales based on:
- Incoming requests
- CPU utilization
- Memory usage
Configure scaling:
gcloud run services update telegram-bot \
--min-instances=1 \
--max-instances=100 \
--concurrency=1000- Connect GitHub repository
- Create trigger with
cloudbuild.yaml - Auto-deploy on code push
name: Deploy to Cloud Run
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: google-github-actions/setup-gcloud@v0
- run: gcloud run deploy telegram-bot --source .For issues and questions:
- Check Cloud Run logs
- Verify webhook configuration
- Test database connectivity
- Check environment variables
MIT License - see original bot implementation for details.