Skip to main content
Glama
JawaKunci

Qwen Code Telegram MCP Server

by JawaKunci
README.md
# Qwen Code Telegram MCP Server

**Version:** v2.2.0  

MCP Server for Telegram Bot integration with Qwen Code. Chat with Qwen Code directly from Telegram with persistent sessions, group chat support, and advanced features.

---

## ๐ŸŒŸ Features

### Core Features
- ๐Ÿค– **Telegram Bot Integration** - Full Telegram Bot API via long polling
- ๐Ÿ”Œ **MCP Protocol Support** - Native integration with Qwen Code
- ๐Ÿ’ฌ **Persistent Sessions** - File-based conversation history (survives restarts)
- ๐Ÿ” **Pairing System** - 6-digit code security for access control
- โŒจ๏ธ **Typing Indicators** - Better UX while processing
- ๐Ÿ“Š **Admin Commands** - Session management and monitoring
- ๐Ÿงน **Auto-Cleanup** - Automatic maintenance for old sessions
- ๐Ÿ”ง **Dynamic Configuration** - Workspace-specific settings

### v2.2.0 New Features
- ๐Ÿ‘ฅ **Group Chat Support** - Bot works in groups without crashes!
- ๐Ÿ“ข **Mention-Based Trigger** - Bot responds only when mentioned (`@bot_name`)
- ๐Ÿ’ญ **Quoted Message Context** - Bot reads reply/quote messages
- ๐Ÿ”‡ **Silent Ignore** - No spam in groups (ignores non-mentions)
- ๐ŸŽฏ **Smart Reply** - Auto-reply to original message for better UX
- ๐Ÿ“ˆ **Metrics Tracking** - Request count and token usage monitoring

---

## ๐Ÿš€ Quick Start

### For Qwen Code Users (Vibe Coding)

**๐ŸŽฏ EASIEST METHOD:** Let your LLM agent handle everything!

After cloning this repository, simply tell your Qwen Code:

```
@qwen Read and implement VIBE-INSTALL.md for installation, 
then VIBE-CONFIG.md for configuration. Ask me for confirmation 
before any sensitive data.
```

Your LLM agent will:
1. โœ… Install all dependencies automatically
2. โœ… Build the TypeScript project
3. โœ… Guide you through Telegram bot creation
4. โœ… Configure Qwen Code MCP settings
5. โœ… Test everything works

**๐Ÿ“š Vibe Coding Guides:**
- ๐Ÿ“ฆ [VIBE-INSTALL.md](VIBE-INSTALL.md) - Automated installation
- โš™๏ธ [VIBE-CONFIG.md](VIBE-CONFIG.md) - Automated configuration  
- ๐Ÿ› ๏ธ [VIBE-REPAIR.md](VIBE-REPAIR.md) - Automated troubleshooting

---

> **Special Note from the Human**
> This is the only section touched by human. Everything in this MCP is vibe-coded but safely structured. 
> If something messed up, simply run your agent. It will do the complex thing for you.
> Wanted new features? Ask your agent.
>
> My agent clears the path. Yours should follow.
> -JavaIsKey

---

### Manual Installation

#### Prerequisites
- Node.js >= 18.x
- npm >= 9.x
- Git

#### Steps

1. **Clone repository**
```bash
git clone https://github.com/YOUR_USERNAME/qwen-code-telegram-mcp.git
cd qwen-code-telegram-mcp
```

2. **Install dependencies**
```bash
npm install
```

3. **Build TypeScript**
```bash
npm run build
```

4. **Setup environment**
```bash
cp .env.example .env
# Edit .env with your configuration
```

5. **Configure Qwen Code**

Add to `~/.qwen/settings.json`:
```json
{
  "mcpServers": {
    "telegram": {
      "command": "node",
      "args": ["[YOUR_PATH]/dist/index.js"],
      "env": {
        "TELEGRAM_BOT_TOKEN": "${TELEGRAM_BOT_TOKEN}",
        "WORKSPACE_PATH": "[YOUR_WORKSPACE_PATH]"
      }
    }
  }
}
```

6. **Start Qwen Code**
```bash
npx qwen
```

7. **Chat with your bot on Telegram!**

---

## ๐Ÿ“ฑ Telegram Bot Setup

### Create New Bot

1. Open Telegram โ†’ Search `@BotFather`
2. Send `/newbot`
3. Follow instructions:
   - Choose name: `My Assistant`
   - Choose username: `my_assistant_bot`
4. **Copy the bot token** (format: `123456789:ABCdef...`)

### โš ๏ธ IMPORTANT: Disable Privacy Mode

For group chat support:

1. @BotFather โ†’ `/setprivacy`
2. Select your bot
3. Choose: **"Disable privacy"**

Without this, bot won't read group messages!

---

## ๐ŸŽฎ Usage

### Private Chat

Just send message to bot:
```
User: Hello
Bot: [AI response from Qwen Code]
```

### Group Chat

**Must mention bot:**
```
User: @my_bot_name What's the capital of France?
Bot: [AI response]

User: hello everyone
Bot: [Ignored - no mention]
```

**With quoted message:**
```
User 1: [sends message] "This is important"
User 2: [reply] @my_bot_name summarize this
Bot: [Reads quoted message + instruction]
```

---

## ๐Ÿ“‹ Available Commands

### User Commands
- `/start` - Welcome message + pairing instructions
- `/help` - Help information
- `/status` - Server status
- `/session` - Your session info
- `/clear` - Clear your session
- `/whoami` - Your Telegram info
- `/ask <question>` - Ask Qwen Code directly
- `/pair <CODE>` - Pair bot with code
- `/approvalmode [mode]` - Set approval mode (plan/auto-accept/yolo)

### Admin Commands
- `/sessions` - List all active sessions

---

## ๐Ÿ” Pairing System

### How It Works

1. **User messages bot** - Bot checks if user is paired
2. **Admin generates code** - Via MCP tool:
   ```
   generate_telegram_pairing_code
   ```
3. **User pairs** - User runs:
   ```
   /pair ABC123
   ```
4. **Access granted** - User now has access

### Pairing Code Properties
- 6 characters (uppercase alphanumeric)
- Valid for 15 minutes
- One-time use
- Auto-cleanup when expired

---

## ๐Ÿ“ Project Structure

```
qwen-code-telegram-mcp/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ index.ts              # Main code (2300+ lines)
โ”œโ”€โ”€ dist/
โ”‚   โ””โ”€โ”€ index.js              # Compiled code
โ”œโ”€โ”€ .env                      # Environment config
โ”œโ”€โ”€ .env.example              # Example config
โ”œโ”€โ”€ package.json              # Dependencies
โ”œโ”€โ”€ tsconfig.json             # TypeScript config
โ”œโ”€โ”€ README.md                 # This file
โ”œโ”€โ”€ DOCUMENTATION.md          # Detailed documentation
โ”œโ”€โ”€ VIBE-INSTALL.md           # LLM agent installation guide
โ”œโ”€โ”€ VIBE-CONFIG.md            # LLM agent configuration guide
โ””โ”€โ”€ VIBE-REPAIR.md            # LLM agent troubleshooting guide

Workspace/
โ”œโ”€โ”€ temp/
โ”‚   โ”œโ”€โ”€ telegram-bot.log              # Runtime log
โ”‚   โ””โ”€โ”€ telegram-sessions/
โ”‚       โ”œโ”€โ”€ {chatId}.json             # User sessions
โ”‚       โ”œโ”€โ”€ allowlist.json            # Paired users
โ”‚       โ””โ”€โ”€ approval-modes.json       # Approval modes
โ””โ”€โ”€ .qwen/
    โ””โ”€โ”€ commands/
        โ””โ”€โ”€ *.md                      # Custom commands
```

---

## โš™๏ธ Configuration

### Environment Variables

Copy `.env.example` to `.env` and configure:

```env
# Telegram Bot Configuration
TELEGRAM_BOT_TOKEN=your_bot_token_here

# Workspace Configuration
WORKSPACE_PATH=/path/to/your/workspace

# MCP Server Configuration
MCP_SERVER_PORT=3000
MCP_TRANSPORT=stdio
```

### Session Settings

| Setting | Default | Description |
|---------|---------|-------------|
| `MAX_CONVERSATION_HISTORY` | 50 | Max messages per session |
| `INACTIVE_CHAT_TIMEOUT_MS` | 86400000 | Session expiry (24h) |

---

## ๐Ÿ› ๏ธ Development

```bash
# Build TypeScript
npm run build

# Run development mode
npm run dev

# Run production
npm start
```

---

## ๐Ÿ› Troubleshooting

### Bot not responding

**Check:**
1. Bot token valid? โ†’ @BotFather
2. Bot running in Qwen Code?
3. Privacy mode disabled? โ†’ `/setprivacy`

### Session not saving

**Check:**
1. `WORKSPACE_PATH` correct?
2. Folder has write permission?
3. `temp/telegram-sessions/` exists?

### Pairing code not working

**Check:**
1. Code not expired (15 min only)
2. Code entered correctly (uppercase)
3. User in allowlist?

### Need Help?

๐Ÿ‘‰ **See [VIBE-REPAIR.md](VIBE-REPAIR.md)** for comprehensive troubleshooting guide!

---

## ๐Ÿ“Š Metrics Tracking

Bot automatically tracks:
- `totalRequests` - Total message count
- `totalErrors` - Error count
- `tokens.total` - Estimated token usage
- `tokens.prompt` - Input tokens
- `tokens.output` - Output tokens

**View with:** `/status` command

---

## ๐Ÿงช Testing Checklist

### Basic Functionality
- [ ] DM: `hello` โ†’ AI response
- [ ] Group: `@bot_name test` โ†’ AI response
- [ ] Group: `test` (no mention) โ†’ Ignored
- [ ] Custom command: `/health` โ†’ Executed

### Sessions
- [ ] Session file created
- [ ] Session persists after restart
- [ ] `/clear` deletes session
- [ ] `/session` shows correct info

### Pairing
- [ ] Unpaired โ†’ Access denied
- [ ] Generate code โ†’ Works
- [ ] `/pair ABC123` โ†’ Success
- [ ] Invalid code โ†’ Rejected

### Files
- [ ] Send document โ†’ Processed
- [ ] Send photo โ†’ Downloaded
- [ ] `send_telegram_file` โ†’ Works

### Logging
- [ ] Log file created
- [ ] Messages logged
- [ ] Errors logged
- [ ] No infinite loops

---

## ๐Ÿ“ Version History

### v2.2.0 (Current)
- โœ… Full group chat support
- โœ… Mention-based triggering
- โœ… Quoted message context
- โœ… Telegram auto-correct handling
- โœ… Metrics tracking

### v2.1
- Multi-bot support via workspace-specific MCP config

### v2.0
- Persistent sessions
- Pairing system
- Typing indicators
- Admin commands

### v1.0
- Initial release with basic MCP integration

---

## ๐Ÿ“š Documentation

- **[VIBE-INSTALL.md](VIBE-INSTALL.md)** - LLM agent installation guide
- **[VIBE-CONFIG.md](VIBE-CONFIG.md)** - LLM agent configuration guide
- **[VIBE-REPAIR.md](VIBE-REPAIR.md)** - LLM agent troubleshooting guide
- **[DOCUMENTATION.md](DOCUMENTATION.md)** - Detailed technical documentation
- **[CONTRIBUTING.md](CONTRIBUTING.md)** - Contribution guidelines
- **[CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md)** - Code of conduct

---

## ๐Ÿค Contributing

Contributions welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) first.

### Quick Contributing
1. Fork repository
2. Create branch: `git checkout -b feature/your-feature`
3. Make changes
4. Test thoroughly
5. Submit PR

---

## ๐Ÿ“„ License

ISC License - see [LICENSE](LICENSE) file.

---

## ๐Ÿ†˜ Support

**For Issues:**
1. Check [VIBE-REPAIR.md](VIBE-REPAIR.md) first
2. Review logs: `temp/telegram-bot.log`
3. Create GitHub issue with:
   - Error message
   - What you were doing
   - Your environment (Node.js, OS)

**For Questions:**
- Create GitHub Discussion
- Check [DOCUMENTATION.md](DOCUMENTATION.md)

---

## ๐ŸŽฏ Why Vibe Coding?

This project is designed for **Qwen Code vibe coding**:

- โœ… **LLM-Friendly Setup** - VIBE-* guides for automated installation
- โœ… **Zero Configuration** - LLM agent handles everything
- โœ… **Smart Troubleshooting** - VIBE-REPAIR for automated fixes
- โœ… **User Confirmation** - Sensitive data requires user approval
- โœ… **Well Documented** - Every step clearly explained

**Just clone and let your LLM agent do the work!** ๐Ÿš€

---

*Built with โค๏ธ for Qwen Code*  
*Last updated: 2026-03-26*