Skip to content

JuniorRaja/tg-ai-bot

Repository files navigation

πŸ€– Telegram AI Assistant Bot

A powerful, serverless Telegram bot powered by multiple AI models (Groq, Gemini) running on Cloudflare Workers. Built for speed, scalability, and easy deployment. Focused on being your Personal Assistant, Coach and Chat buddy who gathers information from normal conversations with the bot. Capable of chatting, reminding, track habits and even store documents and media.

License: MIT Cloudflare Workers Telegram Bot API

✨ Features

  • 🧠 Multi-Model AI Support - Integrates with Groq and Google Gemini for intelligent responses
  • ⚑ Serverless Architecture - Runs on Cloudflare Workers for global low-latency performance
  • πŸ’Ύ Persistent Storage - Uses Cloudflare D1 (SQLite) for conversation history and user data
  • πŸ”„ Context-Aware Conversations - Maintains conversation context for natural interactions
  • 🎯 Command-Based Interface - Easy-to-use commands for bot interaction
  • πŸ”’ Secure - API keys managed through Cloudflare secrets
  • 🌍 Global CDN - Deployed across Cloudflare's edge network

πŸš€ Tech Stack

Runtime & Infrastructure

Database & Storage

AI Models & APIs

Language & Framework

  • JavaScript/TypeScript - Bot logic and API integrations
  • Node.js - Development environment

πŸ“‹ Current Features

  • βœ… Natural language conversations with AI
  • βœ… Multi-turn conversation support with context
  • βœ… User session management
  • βœ… Command-based interactions
  • βœ… Message history storage
  • βœ… Multi-environment support (development/production)
  • βœ… Automatic webhook setup
  • βœ… Error handling and logging

🎯 Future Plans

  • πŸ”œ Image generation capabilities
  • πŸ”œ Voice message support
  • πŸ”œ Multi-language support
  • πŸ”œ User preferences and settings
  • πŸ”œ Rate limiting and usage quotas
  • πŸ”œ Analytics and usage dashboard
  • πŸ”œ Plugin system for extensibility
  • πŸ”œ Group chat support with mentions
  • πŸ”œ Document analysis and summarization
  • πŸ”œ Custom AI model integration

πŸ› οΈ Setup & Installation

Prerequisites

Step 1: Install Wrangler CLI

npm install -g wrangler

Verify installation and login:

wrangler whoami
wrangler login

Step 2: Clone the Repository

git clone https://github.com/yourusername/telegram-ai-bot.git
cd telegram-ai-bot

Step 3: Create Cloudflare Resources

Create D1 Databases

# Production database
wrangler d1 create telegram-ai-bot > d1_output.txt

# Development database
wrangler d1 create telegram-ai-bot-dev > d2_output.txt

Create KV Namespaces

# Production KV
wrangler kv namespace create "telegram-ai-assistant" > kv1_output.txt

# Development KV
wrangler kv namespace create "telegram-ai-assistant-dev" > kv2_output.txt

Step 4: Configure wrangler.toml

Copy the example configuration and update with your resource IDs:

cp wrangler.toml.example wrangler.toml

Open wrangler.toml and fill in the IDs from the output files created in Step 3.

Step 5: Set Secrets

You can set secrets via CLI or Cloudflare Dashboard.

Option A: Using Wrangler CLI

# Production secrets
wrangler secret put TELEGRAM_BOT_TOKEN --env production
wrangler secret put GROQ_API_KEY --env production
wrangler secret put GEMINI_API_KEY --env production

# Development secrets
wrangler secret put TELEGRAM_BOT_TOKEN --env development
wrangler secret put GROQ_API_KEY --env development
wrangler secret put GEMINI_API_KEY --env development

Option B: Using Cloudflare Dashboard

  1. Go to Cloudflare Dashboard
  2. Navigate to Workers & Pages > Your Worker > Settings > Variables
  3. Add the secrets under Environment Variables

Step 6: Run Database Migrations

# Development environment
wrangler d1 migrations apply telegram-ai-bot-dev --env development --remote

# Production environment
wrangler d1 migrations apply telegram-ai-bot --env production --remote

Step 7: Deploy the Worker

# Deploy to development
wrangler deploy --env development

# Deploy to production
wrangler deploy --env production

Step 8: Set Up Telegram Webhook

Replace [TELEGRAM_BOT_TOKEN] with your bot token and [WORKER-URL] with your Worker URL:

# Your worker URL format
https://<your-worker-name>.<your-subdomain>.workers.dev

Set the webhook:

https://api.telegram.org/bot[TELEGRAM_BOT_TOKEN]/setWebhook?url=[WORKER-URL]/webhook

Step 9: Verify Setup

Check if everything is working:

# Verify webhook
https://api.telegram.org/bot[TELEGRAM_BOT_TOKEN]/getWebhookInfo

# Verify bot
https://api.telegram.org/bot[TELEGRAM_BOT_TOKEN]/getMe

πŸ“ Usage

Once deployed, interact with your bot on Telegram:

  • /start - Initialize the bot
  • /help - Show available commands
  • Just type any message to chat with the AI!

πŸ› Debugging

View real-time logs:

wrangler tail --env=development

Check your Worker status:

https://<your-worker-name>.<your-subdomain>.workers.dev

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

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

πŸ™ Acknowledgments

  • Cloudflare Workers for the amazing serverless platform
  • Groq for ultra-fast AI inference for it's generous free tier
  • Google Gemini for advanced AI capabilities and image generation free tier
  • Telegram for the Bot API

πŸ“§ Support

If you have any questions or run into issues, please open an issue on GitHub.


Made with ❀️ for the Open Source Community

About

tg-ai-bot

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published