A NestJS-based Telegram bot that integrates with OpenAI's Assistant API to provide intelligent conversational responses. The bot maintains conversation context per user using Redis for thread persistence.
- 🤖 OpenAI Assistant Integration - Leverages OpenAI's Assistant API for intelligent responses
- 💬 Telegram Bot - Full-featured Telegram bot using Telegraf
- 🔄 Conversation Persistence - Maintains conversation threads per user with Redis
- 📝 Markdown Support - Formatted responses with Telegram markdown and emojis
- 🏗️ Modular Architecture - Clean separation of concerns with NestJS modules
src/
├── openai/ # OpenAI Assistant API integration
│ ├── openai.module.ts
│ └── openai.service.ts
├── telegram/ # Telegram bot implementation
│ ├── telegram.module.ts
│ └── telegram.service.ts
├── app.module.ts # Root application module
├── app.controller.ts # Basic HTTP endpoint
├── app.service.ts # Application service
└── main.ts # Application entry point
- Node.js (v18 or higher)
- npm or yarn
- Redis server running locally or remotely
- OpenAI API key
- Telegram Bot Token (from @BotFather)
- OpenAI Assistant ID
npm installCreate a .env file in the root directory with the following environment variables:
# OpenAI Configuration
OPENAI_API_KEY=your-openai-api-key
ASSISTANT_ID=your-openai-assistant-id
# Telegram Configuration
TELEGRAM_BOT_TOKEN=your-telegram-bot-token- OpenAI API Key: Get from OpenAI Platform
- OpenAI Assistant ID: Create an assistant in OpenAI Playground and copy its ID
- Telegram Bot Token: Message @BotFather on Telegram and create a new bot
npm run start:devnpm run build
npm run start:prodnpm run start:debug- Start the application
- Open Telegram and find your bot
- Send any text message to the bot
- The bot will forward your message to the OpenAI assistant and reply with the response
- Conversation context is maintained per user automatically
- Message Reception: User sends a message via Telegram
- User Identification: Bot extracts user ID and retrieves/creates an OpenAI thread
- Thread Persistence: Thread ID is stored in Redis for future conversations
- OpenAI Processing: Message is sent to OpenAI Assistant API
- Response Retrieval: Bot polls for completion and retrieves the response
- Reply: Response is sent back to the user on Telegram
# Unit tests
npm run test
# E2E tests
npm run test:e2e
# Test coverage
npm run test:cov# Lint
npm run lint
# Format code
npm run formatGET /- Health check endpoint returning "Hello World!"
The application runs on port 3030 by default.
- Framework: NestJS 10.x
- Language: TypeScript
- Bot Library: Telegraf 4.x
- AI Integration: OpenAI Node.js SDK 4.x
- Data Store: Redis 4.x
- HTTP Client: Axios (via @nestjs/axios)
- WebSockets: Socket.io (via @nestjs/platform-socket.io)
- OpenAI Module: Manages OpenAI client initialization, thread creation, message sending, and response polling
- Telegram Module: Handles Telegram bot setup, message reception, and response forwarding
- Config Module: Global configuration management using @nestjs/config
- Redis: Stores user-to-thread mappings for conversation continuity
- Create a new module:
nest generate module feature-name - Add services:
nest generate service feature-name - Import the module in
app.module.ts
All configuration is managed through environment variables loaded via @nestjs/config. Add new variables to your .env file and access them using ConfigService.
- Ensure Redis server is running:
redis-cli pingshould returnPONG - Check Redis connection settings if using remote server
- Verify
TELEGRAM_BOT_TOKENis correct - Check bot is not blocked by user
- Ensure OpenAI API key is valid
- Verify
OPENAI_API_KEYhas sufficient credits - Check
ASSISTANT_IDis valid and accessible - Review OpenAI API status at status.openai.com
UNLICENSED - Private project
This is a private project. For questions or contributions, contact the repository owner.