Skip to main content
This guide covers deploying grammY bots to various platforms and environments. Choose the platform that best fits your needs.

Platform Comparison

Traditional Servers

VPS, dedicated servers, or containers. Full control, always running.

Serverless

Function-as-a-Service platforms. Pay per use, automatic scaling.

PaaS

Platform-as-a-Service like Heroku or Railway. Easy deployment.

Edge Computing

Cloudflare Workers, Deno Deploy. Global distribution.

Deployment Checklist

Before deploying:
  • Use environment variables for sensitive data (bot token, API keys)
  • Set up error logging and monitoring
  • Choose between long polling (always-on servers) or webhooks (serverless/scalable)
  • Configure production error handling
  • Test your bot thoroughly
  • Set up automatic restarts for long polling bots
  • Consider rate limiting and abuse prevention

Traditional Server Deployment

Using PM2 (Node.js)

ecosystem.config.js for PM2:

Using systemd (Linux)

/etc/systemd/system/telegram-bot.service:
Enable and start:

Docker Deployment

Dockerfile:
docker-compose.yml:
Deploy:

Serverless Platforms

Cloudflare Workers

wrangler.toml:
Deploy:

Deno Deploy

Deploy:

Vercel

api/webhook.ts:
vercel.json:
Deploy:

AWS Lambda

Platform-as-a-Service

Railway

  1. Connect your GitHub repository
  2. Add environment variables (BOT_TOKEN)
  3. Railway auto-detects Node.js and deploys
  4. For webhooks, use the provided domain

Heroku

Procfile:
package.json (add):
Deploy:

Render

  1. Connect GitHub repository
  2. Select “Web Service” for webhooks or “Background Worker” for polling
  3. Set build command: npm install && npm run build
  4. Set start command: node dist/bot.js
  5. Add BOT_TOKEN environment variable

Environment Variables

.env.example:
Loading environment variables:

Production Configuration

Monitoring

Health Check Endpoint

Error Tracking (Sentry)

Security Best Practices

Protect Your Token

Never commit tokens to git. Use environment variables and secrets management.

Validate Webhooks

Use secret tokens to verify webhook requests are from Telegram.

Rate Limiting

Implement rate limiting to prevent abuse.

HTTPS Only

Use HTTPS for webhooks. Telegram requires valid SSL certificates.

Performance Tips

  • Use webhooks instead of polling in production
  • Enable concurrent update processing when appropriate
  • Implement caching for frequently accessed data
  • Use database connection pooling
  • Monitor and optimize slow handlers
  • Set appropriate max_connections for webhooks

Scaling

Horizontal Scaling

For high-traffic bots:
  1. Use webhooks with a load balancer
  2. Run multiple instances behind the load balancer
  3. Use a shared database/cache (Redis, PostgreSQL)
  4. Ensure stateless bot handlers

Database

For persistent storage:

See Also