Skip to content

LicenseChain/LicenseChain-TG-Bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

LicenseChain Telegram Bot

License Node.js Telegraf TypeScript

Official Telegram Bot for LicenseChain - License management and customer support through Telegram.

πŸš€ Features

  • πŸ” License Management - Validate, create, and manage licenses
  • πŸ‘€ User Support - Handle customer inquiries and support tickets
  • πŸ“Š Analytics - View usage statistics and performance metrics
  • πŸ”” Notifications - Real-time license events and alerts
  • 🎫 Ticket System - Create and manage support tickets
  • πŸ“ˆ Reporting - Generate reports and analytics
  • πŸ›‘οΈ Security - Secure authentication and authorization
  • πŸ› οΈ Easy Setup - Simple configuration and deployment

πŸ“¦ Installation

Method 1: npm (Recommended)

# Clone the repository
git clone https://github.com/LicenseChain/LicenseChain-TG-Bot.git
cd LicenseChain-TG-Bot

# Install dependencies
npm install

# Start the bot
npm start

Method 2: Docker

# Build the Docker image
docker build -t licensechain-telegram-bot .

# Run the container
docker run -p 3000:3000 licensechain-telegram-bot

Method 3: Manual Installation

  1. Download the latest release from GitHub Releases
  2. Extract to your project directory
  3. Install dependencies: npm install
  4. Configure environment variables
  5. Start the bot: npm start

πŸš€ Quick Start

Basic Setup

# Clone the repository
git clone https://github.com/LicenseChain/LicenseChain-TG-Bot.git
cd LicenseChain-TG-Bot

# Install dependencies
npm install

# Copy environment file
cp .env.example .env

# Edit environment variables
nano .env

# Start the bot
npm start

Environment Configuration

Create a .env file with the following variables:

# Telegram Configuration
TELEGRAM_TOKEN=your-telegram-bot-token
TELEGRAM_WEBHOOK_URL=your-webhook-url

# LicenseChain API
LICENSECHAIN_API_KEY=your-api-key
LICENSECHAIN_APP_NAME=your-app-name
LICENSECHAIN_APP_VERSION=1.0.0
LICENSECHAIN_BASE_URL=https://api.licensechain.com

# Bot Configuration
BOT_DEBUG=false
BOT_OWNER_ID=your-telegram-user-id

# Database Configuration
DATABASE_URL=your-database-url

# Webhook Configuration
WEBHOOK_URL=your-webhook-url
WEBHOOK_SECRET=your-webhook-secret

πŸ“š Commands

License Commands

# Validate a license
/validate <license-key>

# Get license information
/info <license-key>

# List user's licenses
/m licenses

# Create a new license
/create <user-id> <features> <expires>

# Update a license
/update <license-key> <field> <value>

# Revoke a license
/revoke <license-key>

User Commands

# Get user information
/user <user-id>

# Get user's licenses
/licenses <user-id>

# Get user's analytics
/analytics <user-id>

# Ban a user
/ban <user-id> <reason>

# Unban a user
/unban <user-id>

Support Commands

# Create a support ticket
/ticket <subject> <description>

# List support tickets
/tickets

# Get ticket details
/ticket <ticket-id>

# Update ticket status
/update <ticket-id> <status>

# Close a ticket
/close <ticket-id>

Analytics Commands

# Get usage analytics
/usage [timeframe]

# Get license analytics
/licenses [timeframe]

# Get performance metrics
/performance

# Get error statistics
/errors

Admin Commands

# Get bot status
/status

# Get bot statistics
/stats

# Reload commands
/reload

# Set bot status
/setstatus <status>

# Get bot logs
/logs [lines]

πŸ”§ Configuration

Bot Configuration

Configure the bot through environment variables or a configuration file:

// config/bot.js
module.exports = {
  telegram: {
    token: process.env.TELEGRAM_TOKEN,
    webhookUrl: process.env.TELEGRAM_WEBHOOK_URL
  },
  licensechain: {
    apiKey: process.env.LICENSECHAIN_API_KEY,
    appName: process.env.LICENSECHAIN_APP_NAME,
    version: process.env.LICENSECHAIN_APP_VERSION,
    baseUrl: process.env.LICENSECHAIN_BASE_URL
  },
  bot: {
    debug: process.env.BOT_DEBUG === 'true',
    ownerId: process.env.BOT_OWNER_ID
  }
};

Command Configuration

Configure commands and their permissions:

// config/commands.js
module.exports = {
  'validate': {
    permission: 'user',
    cooldown: 5000,
    description: 'Validate a license key'
  },
  'create': {
    permission: 'admin',
    cooldown: 10000,
    description: 'Create a new license'
  },
  'status': {
    permission: 'owner',
    cooldown: 0,
    description: 'Get bot status'
  }
};

Database Configuration

The bot supports multiple database types:

// PostgreSQL
DATABASE_URL=postgresql://username:password@localhost:5432/licensechain

// MySQL
DATABASE_URL=mysql://username:password@localhost:3306/licensechain

// SQLite
DATABASE_URL=sqlite://./database.sqlite

πŸ›‘οΈ Security Features

Authentication

  • Telegram user verification
  • Role-based command permissions
  • User authentication system
  • Secure API key management

Authorization

  • Command-level permissions
  • User role validation
  • Admin-only commands
  • Owner-only functions

Data Protection

  • Input validation and sanitization
  • SQL injection prevention
  • XSS protection
  • Secure logging

πŸ“Š Analytics and Monitoring

Command Analytics

// Track command usage
bot.on('message', async (ctx) => {
  if (ctx.message.text.startsWith('/')) {
    analytics.track('command_used', {
      command: ctx.message.text.split(' ')[0],
      user: ctx.from.id,
      chat: ctx.chat.id,
      timestamp: new Date()
    });
  }
});

Performance Monitoring

// Monitor command execution time
const start = Date.now();
await command.execute(ctx);
const duration = Date.now() - start;
metrics.record('command_execution_time', duration);

Error Tracking

// Track command errors
try {
  await command.execute(ctx);
} catch (error) {
  errorTracker.captureException(error, {
    command: ctx.message.text.split(' ')[0],
    user: ctx.from.id,
    chat: ctx.chat.id
  });
}

πŸ”„ Error Handling

Custom Error Types

// Custom error classes
class CommandError extends Error {
  constructor(message) {
    super(message);
    this.name = 'CommandError';
  }
}

class PermissionError extends Error {
  constructor(message) {
    super(message);
    this.name = 'PermissionError';
  }
}

Error Middleware

// Global error handler
bot.catch((err, ctx) => {
  console.error('Bot error:', err);
  errorTracker.captureException(err);
  
  ctx.reply('An error occurred. Please try again later.');
});

process.on('unhandledRejection', (reason, promise) => {
  console.error('Unhandled Rejection at:', promise, 'reason:', reason);
  errorTracker.captureException(reason);
});

πŸ§ͺ Testing

Unit Tests

# Run tests
npm test

# Run tests with coverage
npm run test:coverage

# Run tests in watch mode
npm run test:watch

Integration Tests

# Test with real Telegram API
npm run test:integration

End-to-End Tests

# Test complete command flows
npm run test:e2e

πŸ“ Examples

See the examples/ directory for complete examples:

  • basic-setup.js - Basic bot setup
  • custom-commands.js - Custom command examples
  • webhook-integration.js - Webhook handling
  • deployment.js - Deployment configuration

🀝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

  1. Clone the repository
  2. Install Node.js 16 or later
  3. Install dependencies: npm install
  4. Set up environment variables
  5. Start development server: npm run dev

πŸ“„ License

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

πŸ†˜ Support

πŸ”— Related Projects


Made with ❀️ for the Telegram community

About

Advanced Telegram integration for LicenseChain license management

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published