A modern, scalable chat bot built with NestJS and TypeScript for the Mezon chat platform.
- Modern Architecture: Built with NestJS framework using TypeScript
- Command System: Extensible command system with metadata support
- Event-Driven: Event-driven architecture with proper separation of concerns
- Database Integration: PostgreSQL with TypeORM for data persistence
- Message Queue: Efficient message processing with throttling
- Error Handling: Comprehensive error handling and logging
- Modular Design: Clean, maintainable, and testable code structure
- Node.js 18+
- PostgreSQL 12+
- yarn (recommended) or npm
-
Clone the repository
git clone https://github.com/hoangduy0610/mezon-bot-template.git cd mezon-bot-template
-
Install dependencies
yarn
-
Environment Setup
cp .env.example .env.local
Configure your environment variables:
POSTGRES_HOST=localhost POSTGRES_PORT=5432 POSTGRES_USER=postgres POSTGRES_PASSWORD=your_password POSTGRES_DB=mezon_bot MEZON_TOKEN=your_mezon_bot_token
-
Database Setup
# Run migrations yarn db:run
# Development mode
yarn start:dev
# Production mode
yarn start:prod
# Debug mode
yarn start:debug
Command | Description | Usage |
---|---|---|
!help |
Show available commands | !help [command] |
!ping |
Check bot latency | !ping |
!about |
Bot information | !about |
src/
├── command/ # Bot commands
│ ├── common/ # Command abstractions
│ ├── help.command.ts
│ ├── ping.command.ts
│ └── about.command.ts
├── common/ # Shared constants and utilities
├── config/ # Configuration files
├── decorators/ # Custom decorators
├── dtos/ # Data transfer objects
├── entities/ # Database entities
├── gateway/ # Event gateways
├── listeners/ # Event listeners
├── modules/ # NestJS modules
├── services/ # Business logic services
├── types/ # TypeScript type definitions
└── utils/ # Utility functions
# Unit tests
yarn test
# End-to-end tests
yarn test:e2e
# Test coverage
yarn test:cov
The bot uses PostgreSQL with TypeORM for data persistence. Key entities:
- Create a new command file in
src/command/
- Extend
CommandMessage
class - Use
@Command
decorator with metadata - Register in
BotModule
Example:
@Command('example', {
description: 'An example command',
usage: '!example [args]',
category: 'Utility',
})
export class ExampleCommand extends CommandMessage {
execute(args: string[], message: ChannelMessage) {
return this.replyMessageGenerate({
messageContent: 'Hello World!'
}, message);
}
}
# Lint code
yarn lint
# Format code
yarn format
- BotGateway: Handles Mezon SDK events
- MessageCommand: Processes message queue with throttling
- CommandService: Routes commands to handlers
- MessageQueue: Manages message processing queue
- Command Pattern: For bot commands
- Observer Pattern: For event handling
- Dependency Injection: Via NestJS
- Repository Pattern: With TypeORM
# Build image
docker build -t mezon-bot-template .
# Run container
docker run -d --name mezon-bot-template \
--env-file .env.production \
-p 3000:3000 \
mezon-template
# Build application
yarn build
# Run production
NODE_ENV=production yarn start:prod
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the ISC License.
Nguyen Hoang Duy
- GitHub: @hoangduy0610