This is the Telegram bot for Champion Trade platform. It handles user interactions and provides access to the trading mini app.
- Menu button "Trade" that opens the mini app
- Welcome message with trading platform access
- About information for Champion Trade
- Direct link to Champion Trade website
- Registry-based command and action management
- Automated menu command generation
- Extensible architecture for adding new commands/actions
- Comprehensive error handling and logging
- Test coverage for components
-
Clone the repository
-
Install dependencies:
npm install
-
Create
.env
file from.env.example
:cp .env.example .env
-
Update
.env
with your configuration:BOT_TOKEN
: Your Telegram bot token from @BotFatherWEBAPP_URL
: The official Telegram Mini App URL (t.me URL) used for direct links and sharingWEBAPP_HOST_URL
: The actual hosting URL where your web application is deployedNODE_ENV
: Set to 'development' for local development
-
Start development server:
npm run dev
-
Set up your production environment (e.g., VPS, cloud server)
-
Clone the repository
-
Install dependencies:
npm install --production
-
Set up environment variables:
- Create
.env
file or set system environment variables - Set
NODE_ENV=production
- Configure production
WEBAPP_URL
(t.me URL) - Configure production
WEBAPP_HOST_URL
(hosting URL) - Set production
BOT_TOKEN
- Create
-
Start the bot:
npm start
-
Build the Docker image:
docker build -t champion-telegram-bot .
-
Run the container:
docker run -d \ --name champion-bot \ -e BOT_TOKEN=your_token \ -e WEBAPP_URL=your_telegram_webapp_url \ -e WEBAPP_HOST_URL=your_hosting_url \ -e NODE_ENV=production \ champion-telegram-bot
The bot can also be deployed to serverless platforms like:
- AWS Lambda
- Google Cloud Functions
- Vercel
- Heroku
Choose the platform that best fits your needs and follow their specific deployment guidelines.
champion-telegram-bot/
├── src/
│ ├── index.js # Application entry point
│ ├── bot/
│ │ ├── ChampionBot.js # Main bot implementation
│ │ ├── commands/
│ │ │ ├── commandRegistry.js # Command management system
│ │ │ ├── baseCommand.js # Base command class
│ │ │ ├── startCommand.js # Start command implementation
│ │ │ ├── tradeCommand.js # Trade command implementation
│ │ │ ├── helpCommand.js # Help command implementation
│ │ │ └── __tests__/ # Command tests
│ │ ├── actions/
│ │ │ ├── actionRegistry.js # Action management system
│ │ │ ├── baseAction.js # Base action class
│ │ │ ├── aboutAction.js # About action implementation
│ │ │ ├── webAppDataHandler.js # Web app data handler
│ │ │ └── __tests__/ # Action tests
│ │ └── middleware/ # Bot middleware
│ ├── config/ # Configuration management
│ ├── constants/ # Application constants
│ ├── errors/ # Error definitions
│ ├── services/
│ │ ├── trade/ # Trading service
│ │ └── validation/ # Validation service
│ ├── types/ # Type definitions
│ └── utils/
│ ├── logger.js # Logging utility
│ └── __tests__/ # Utility tests
├── .env.example # Environment variables template
├── package.json # Project dependencies and scripts
└── README.md # Project documentation
The bot uses registry patterns for managing both Telegram commands and actions. This architecture provides:
- Centralized command management
- Automatic menu command generation
- Metadata support for each command
- Easy command registration and initialization
The command system is built on three main components:
-
BaseCommand: Abstract base class providing:
- Error handling
- User info extraction
- Common command utilities
-
CommandRegistry: Central command management:
- Command registration and initialization
- Menu command generation
- Metadata management
-
Individual Commands:
- Extend BaseCommand
- Implement specific command logic
- Include command-specific error handling
The action system mirrors the command system structure:
-
BaseAction: Abstract base class providing:
- Error handling
- User info extraction
- Callback query handling
-
ActionRegistry: Central action management:
- Action registration and initialization
- Metadata management
- Action type categorization
-
Individual Actions:
- Extend BaseAction
- Implement specific action logic
- Include action-specific error handling
const BaseCommand = require('./baseCommand');
class NewCommand extends BaseCommand {
async handle(ctx) {
try {
// Your command implementation
await ctx.reply('New command response');
} catch (error) {
await this.handleError(ctx, error);
}
}
}
module.exports = NewCommand;
- Register the command in
src/bot/ChampionBot.js
:
// Import your command
const NewCommand = require('./commands/newCommand');
// In registerDefaultCommands method:
commandRegistry.register('newcommand', NewCommand, {
description: 'Description for menu',
usage: '/newcommand'
});
The command will automatically be:
- Added to the bot's command menu
- Initialized with proper error handling
- Available through the /newcommand command
- Create a new action class in
src/bot/actions/
:
const BaseAction = require('./baseAction');
class NewAction extends BaseAction {
async handle(ctx) {
try {
await this.answerCallback(ctx);
// Your action implementation
await ctx.reply('New action response');
} catch (error) {
await this.handleError(ctx, error);
}
}
}
module.exports = NewAction;
- Register the action in
src/bot/ChampionBot.js
:
// Import your action
const NewAction = require('./actions/newAction');
// In registerDefaults method:
actionRegistry.register('newaction', NewAction, {
description: 'Description of the action',
type: 'button'
});
The action will automatically be:
- Initialized with proper error handling
- Available through the specified trigger (e.g., button callback)
- Configured with metadata for management
Commands and actions are registered in ChampionBot through dedicated methods:
class ChampionBot {
registerCommands() {
// Register commands with metadata
}
registerActions() {
// Register actions with metadata
}
registerDefaults() {
this.registerCommands();
this.registerActions();
}
}
Both commands and actions support rich metadata:
description
: Shown in Telegram's command menuusage
: Command usage information- Additional custom metadata as needed
commandRegistry.register('trade', TradeCommand, {
description: 'Open trading interface', // Shown in menu
usage: '/trade', // Usage help
category: 'trading', // Grouping
requiresAuth: true // Auth flag
});
actionRegistry.register('about', AboutAction, {
description: 'About Champion Trade', // Description
type: 'button', // UI type
category: 'info', // Grouping
requiresAuth: false // Auth flag
});
The project includes comprehensive tests:
src/
├── bot/
│ ├── commands/__tests__/ # Command tests
│ ├── actions/__tests__/ # Action tests
└── utils/__tests__/ # Utility tests
Run tests with:
# Run all tests
npm test
# Run with coverage
npm run test:coverage
This bot is intentionally separated from the web app for several reasons:
- Independent scaling - Bot and web app can be scaled separately
- Easier maintenance - Changes to bot don't affect web app and vice versa
- Better deployment flexibility - Can be deployed to different platforms
- Clearer separation of concerns - Bot handles Telegram interactions, web app handles trading interface
-
Bot and web app can be developed independently
-
For local testing:
- Run web app locally (e.g.,
npm run dev
in web app project) - Update bot's
WEBAPP_URL
to point to local web app - Run bot locally (
npm run dev
)
- Run web app locally (e.g.,
-
For production:
- Deploy web app to production host
- Update bot's
WEBAPP_URL
to production URL - Deploy bot using preferred method