Skip to content

Latest commit

ย 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒ™ Telegram Quran Ayah Bot

A professional-grade NestJS Telegram bot that delivers daily Quran Ayahs with translations in multiple languages. Built with queue processing, scheduled tasks, and a clean architecture.

๐ŸŒŸ Features

  • Daily Scheduled Ayah: Sends a random Quran Ayah every day at 9:00 AM
  • Multi-Language Support:
    • ๐Ÿ‡ฎ๐Ÿ‡ท Farsi (Persian)
    • ๐Ÿ‡ฌ๐Ÿ‡ง English
  • Smart Queue System: Bull-based queue for reliable message delivery
  • Translation Support: Automatically fetches Ayah translations in selected language
  • User Sessions: Maintains user language preferences
  • Interactive UI: Inline buttons for easy navigation
  • Error Handling: Comprehensive error handling and logging
  • Monitoring: Queue statistics and health checks

๐Ÿš€ Quick Start

Prerequisites

  • Node.js 16+
  • Redis Server (for queue processing)
  • Telegram Bot Token (from @BotFather)

Installation

# Install dependencies
npm install

# Create .env file
cp .env.example .env

# Edit .env with your configuration
TELEGRAM_BOT_TOKEN=your_token_here
REDIS_HOST=localhost
REDIS_PORT=6379

Run Development

npm run start:dev

Run Production

npm run build
npm run start:prod

๐Ÿ“ Project Structure

src/
โ”œโ”€โ”€ config/              # Configuration management
โ”œโ”€โ”€ interfaces/          # TypeScript interfaces
โ”œโ”€โ”€ modules/
โ”‚   โ”œโ”€โ”€ telegram/       # Telegram bot handlers and service
โ”‚   โ”œโ”€โ”€ quran/          # Quran API service and user management
โ”‚   โ”œโ”€โ”€ queue/          # Bull queue producers and consumers
โ”‚   โ””โ”€โ”€ scheduler/      # Cron job scheduling
โ”œโ”€โ”€ services/           # Business logic services
โ”œโ”€โ”€ app.module.ts       # Root module
โ””โ”€โ”€ main.ts            # Application entry point

๐Ÿ”ง Configuration

Environment Variables

# Telegram Configuration
TELEGRAM_BOT_TOKEN=your_bot_token

# Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DB=0

# Application Settings
NODE_ENV=development
LOG_LEVEL=debug

# Daily Ayah Schedule
SEND_AYAH_HOUR=9
SEND_AYAH_MINUTE=0

๐ŸŽฏ Usage

Start the Bot

/start

The bot will welcome you and ask to select language:

  • ๐Ÿ‡ฎ๐Ÿ‡ท Farsi
  • ๐Ÿ‡ฌ๐Ÿ‡ง English

Get a Random Ayah

After selecting language, the bot sends a random Ayah from the Quran with translation.

Send Another Ayah

Use the "Send Another Ayah" button to get another random Ayah.

Daily Broadcast

The bot automatically sends a random Ayah to all users every day at 9:00 AM (Tehran timezone).

๐Ÿ—๏ธ Architecture

Modules

Telegram Module

  • Handles all Telegram interactions
  • Command and callback query processing
  • Message formatting

Quran Module

  • Quran API integration
  • Random Ayah selection
  • Translation management

Queue Module

  • Bull queue processing
  • Job producers and consumers
  • Retry logic with exponential backoff

Scheduler Module

  • Daily broadcast scheduling
  • Cron job management

๐Ÿ“Š Queue System

The bot uses Bull (Redis-backed queue) for reliable message delivery:

  • Job Priority: Daily broadcasts (priority 10), User requests (priority 5)
  • Retry Policy: 3 attempts with exponential backoff
  • Job Cleanup: Completed jobs automatically removed

Queue Job Structure

{
  chatId: number;
  language: 'fa' | 'en';
  type: 'daily' | 'user_requested';
  timestamp: number;
}

๐ŸŒ API Integration

The bot integrates with the Quran.com API:

  • Endpoint: https://api.quran.com/api/v4
  • Automatic Surah caching
  • Translation support (Farsi & English)

๐Ÿ›ก๏ธ Error Handling

  • Comprehensive error logging
  • Graceful fallbacks
  • Queue job retry mechanism
  • User-friendly error messages

๐Ÿ“ Logging

// Service logging
Logger.log('Information message');
Logger.error('Error message', error);
Logger.debug('Debug message');
Logger.warn('Warning message');

๐Ÿ” Monitoring

Queue Statistics

# Returns queue statistics
- Total jobs
- Active jobs
- Delayed jobs
- Failed jobs
- Completed jobs

Health Checks

  • Bot connectivity
  • Redis connection
  • Quran API availability

๐Ÿš€ Deployment

Docker

FROM node:18-alpine

WORKDIR /app

COPY package*.json ./
RUN npm install --production

COPY dist ./dist

EXPOSE 3000

CMD ["npm", "run", "start:prod"]

Environment Setup

# Production
NODE_ENV=production
LOG_LEVEL=error
SEND_AYAH_HOUR=9
SEND_AYAH_MINUTE=0

๐Ÿงช Testing

npm run test
npm run test:watch
npm run test:cov

๐Ÿ“ฆ Dependencies

  • @nestjs/core - NestJS framework
  • @nestjs/bull - Queue management
  • @nestjs/schedule - Task scheduling
  • telegraf - Telegram Bot API
  • bull - Queue library
  • axios - HTTP client
  • redis - Redis client

๐Ÿ› Troubleshooting

Bot Not Receiving Messages

  • Verify TELEGRAM_BOT_TOKEN is correct
  • Check if Redis is running
  • Review logs for connection errors

Queue Jobs Not Processing

  • Ensure Redis is accessible
  • Check Redis connection settings
  • Review consumer logs

Missing Translations

  • Verify Quran API is accessible
  • Check language code mapping
  • Ensure translation IDs are correct

๐Ÿ“ž Support

For issues and questions:

  1. Check the logs
  2. Verify environment configuration
  3. Review NestJS documentation

๐Ÿ“„ License

MIT

๐Ÿ‘จโ€๐Ÿ’ป Author

Built with โค๏ธ by a Senior Backend Developer


Note: This bot follows production-grade best practices including:

  • Clean architecture
  • SOLID principles
  • Comprehensive error handling
  • Scalable queue system
  • Type-safe implementation
  • Professional logging

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages