development
Development Guide
Development Environment Setup
Prerequisites
Local Development Setup
1.
2.
3.
.env.example
to .env
(if available).env
file with the required variables:BOT_TOKEN=your_telegram_bot_token
BOT_USER=https://t.me/your_bot_username
COPPERX_API_KEY=your_copperx_api_key
REDIS_DATABASE_URL=your_redis_connection_string
SERVER_URL=https://your-webhook-url.com
PORT=3000
NODE_ENV=development
PUSHER_KEY=your_pusher_key
PUSHER_CLUSTER=your_pusher_cluster
COPPERX_API_URL=https://income-api.copperx.io
4.
.env
file with the ngrok HTTPS URL:SERVER_URL=https://your-ngrok-url.ngrok.io
5.
Project Structure
src/
├── app.ts # Express application setup
├── server.ts # Server startup
├── bot/ # Telegram bot-specific code
│ ├── webhook.ts # Webhook handler
│ ├── handlers/ # Command handlers
│ ├── messages/ # Message templates
│ ├── operations/ # Command and callback operations
│ └── utils/ # Bot-specific utilities
├── constants/ # Application constants
├── services/ # Business logic services
├── types/ # TypeScript type definitions
└── utils/ # Shared utilities
├── config/ # Configuration management
├── copperxApi/ # CopperX API clients
├── database/ # Database utilities
├── exceptions/ # Custom exceptions
├── logger/ # Logging utilities
├── middleware/ # Express middleware
├── rate-limit/ # Rate limiting utilities
├── session/ # Session management
└── telegram/ # Telegram API utilities
Key Components
1. Webhook Handler
src/bot/webhook.ts
) is the entry point for all Telegram updates. It:2. Command System
1.
src/bot/operations/command.operations.ts
): Maps command strings to handler functions2.
src/bot/handlers/
): Implement the logic for each command3.
src/constants/bot-commands.ts
): Defines available commands1.
CommandsEnum
2.
src/bot/handlers/
3.
commandOperations
in src/bot/operations/command.operations.ts
4.
BotCommands
to make it visible in the Telegram command menu5.
npm run setup
to update the bot commands in Telegram
3. Callback System
1.
src/bot/operations/callback.operations.ts
): Maps callback data to handler functions2.
3.
src/constants/callback.enum.ts
): Defines available callbacks1.
CallbackEnum
2.
3.
callbackOperations
or handle it in handleCallback
handleCallback
function:
4. Services
5. Session Management
SessionService
using Redis:
UserState
enum in src/types/session.types.ts
.Testing
Unit Testing
Manual Testing
1.
npm run dev
2.
3.
npm run setup
4.
Deployment
Railway Deployment
1.
2.
3.
railway.json
for deploymentrailway.json
:{
"build": {
"builder": "NIXPACKS",
"buildCommand": "npm run build"
},
"deploy": {
"startCommand": "npm run start",
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}
Best Practices
Code Style
Error Handling
Logging
LoggerService
for consistent logging:
Security Considerations
Contributing
1.
main
2.
3.
4.
5.
6.
Additional Resources
Modified at 2025-03-23 17:03:35