Files
vibe-coding-cn/i18n/en/skills/04-dev-tools/telegram-dev/references/index.md
T

470 lines
11 KiB
Markdown
Raw Normal View History

2025-12-17 20:39:02 +08:00
# Telegram Ecosystem Development Resource Index
2025-12-17 20:39:02 +08:00
## Official Documentation
### Bot API
2025-12-17 20:39:02 +08:00
**Main Documentation:** https://core.telegram.org/bots/api
**Description:** Complete reference documentation for the Telegram Bot API
2025-12-17 20:39:02 +08:00
**Core Features:**
- Sending and receiving messages
- Handling media files
- Inline mode
- Payment integration
- Webhook configuration
- Games and polls
### Mini Apps (Web Apps)
2025-12-17 20:39:02 +08:00
**Main Documentation:** https://core.telegram.org/bots/webapps
**Full Platform:** https://docs.telegram-mini-apps.com
**Description:** Development documentation for Telegram Mini Apps
2025-12-17 20:39:02 +08:00
**Core Features:**
- WebApp API
2025-12-17 20:39:02 +08:00
- Themes and UI controls
- Storage (Cloud/Device/Secure)
- Biometric authentication
- Location and sensors
- Payment integration
### Telegram API & MTProto
2025-12-17 20:39:02 +08:00
**Main Documentation:** https://core.telegram.org
**Description:** Complete Telegram protocol and client development
2025-12-17 20:39:02 +08:00
**Core Features:**
- MTProto protocol
- TDLib client library
- Authentication and encryption
- File operations
- Secret Chats
2025-12-17 20:39:02 +08:00
## Official GitHub Repositories
2025-12-17 20:39:02 +08:00
### Bot API Server
**Repository:** https://github.com/tdlib/telegram-bot-api
**Description:** Implementation of the Telegram Bot API server
**Features:**
- Local mode deployment
- Support for large files (up to 2000 MB)
- C++ implementation
- Based on TDLib
2025-12-17 20:39:02 +08:00
### Android Client
**Repository:** https://github.com/DrKLO/Telegram
**Description:** Source code for the official Android client
**Features:**
- Complete Android implementation
- Material Design
2025-12-17 20:39:02 +08:00
- Customizable compilation
2025-12-17 20:39:02 +08:00
### Desktop Client
**Repository:** https://github.com/telegramdesktop/tdesktop
**Description:** Official desktop client (Windows, macOS, Linux)
**Features:**
- Qt/C++ implementation
- Cross-platform support
- Full functionality
2025-12-17 20:39:02 +08:00
### Official Organization
**Organization Page:** https://github.com/orgs/TelegramOfficial/repositories
**Includes:**
- Beta versions
- Support tools
- Example code
2025-12-17 20:39:02 +08:00
## API Method Categories
2025-12-17 20:39:02 +08:00
### Update Management
- `getUpdates` - Long polling
- `setWebhook` - Set a webhook
- `deleteWebhook` - Delete a webhook
- `getWebhookInfo` - Webhook information
2025-12-17 20:39:02 +08:00
### Message Operations
**Sending Messages:**
- `sendMessage` - Text message
- `sendPhoto` - Photo
- `sendVideo` - Video
- `sendDocument` - Document
- `sendAudio` - Audio
- `sendVoice` - Voice
- `sendLocation` - Location
- `sendVenue` - Venue
- `sendContact` - Contact
- `sendPoll` - Poll
- `sendDice` - Dice/Darts
2025-12-17 20:39:02 +08:00
**Editing Messages:**
- `editMessageText` - Edit text
- `editMessageCaption` - Edit caption
- `editMessageMedia` - Edit media
- `editMessageReplyMarkup` - Edit keyboard
- `deleteMessage` - Delete a message
2025-12-17 20:39:02 +08:00
**Other Operations:**
- `forwardMessage` - Forward a message
- `copyMessage` - Copy a message
- `sendChatAction` - Send an action (typing...)
2025-12-17 20:39:02 +08:00
### File Operations
- `getFile` - Get file information
- File download URL: `https://api.telegram.org/file/bot<token>/<file_path>`
- File upload: Supports multipart/form-data
- Max file size: 50 MB (standard), 2000 MB (local Bot API)
2025-12-17 20:39:02 +08:00
### Inline Mode
- `answerInlineQuery` - Respond to an inline query
- Result types: article, photo, gif, video, audio, voice, document, location, venue, contact, game, sticker
2025-12-17 20:39:02 +08:00
### Callback Queries
- `answerCallbackQuery` - Respond to a button click
- Can display a notification or an alert
2025-12-17 20:39:02 +08:00
### Payments
- `sendInvoice` - Send an invoice
- `answerPreCheckoutQuery` - Pre-checkout
- `answerShippingQuery` - Shipping query
- Supported providers: Stripe, Yandex.Money, Telegram Stars
2025-12-17 20:39:02 +08:00
### Games
- `sendGame` - Send a game
- `setGameScore` - Set a score
- `getGameHighScores` - Get high scores
2025-12-17 20:39:02 +08:00
### Group Management
- `kickChatMember` / `unbanChatMember` - Ban/unban
- `restrictChatMember` - Restrict permissions
- `promoteChatMember` - Promote to admin
- `setChatTitle` / `setChatDescription` - Set chat info
- `setChatPhoto` - Set chat photo
- `pinChatMessage` / `unpinChatMessage` - Pin/unpin a message
2025-12-17 20:39:02 +08:00
## Mini Apps API Details
2025-12-17 20:39:02 +08:00
### Initialization
```javascript
const tg = window.Telegram.WebApp;
tg.ready();
tg.expand();
```
2025-12-17 20:39:02 +08:00
### Main Objects
- **WebApp** - Main interface
- **MainButton** - Main button
- **SecondaryButton** - Secondary button
- **BackButton** - Back button
- **SettingsButton** - Settings button
- **HapticFeedback** - Haptic feedback
- **CloudStorage** - Cloud storage
- **BiometricManager** - Biometrics
- **LocationManager** - Location services
- **Accelerometer** - Accelerometer
- **Gyroscope** - Gyroscope
- **DeviceOrientation** - Device orientation
2025-12-17 20:39:02 +08:00
### Event System
40+ events including:
- `themeChanged` - Theme changed
- `viewportChanged` - Viewport changed
- `mainButtonClicked` - Main button clicked
- `backButtonClicked` - Back button clicked
- `settingsButtonClicked` - Settings button clicked
- `invoiceClosed` - Payment completed
- `popupClosed` - Popup closed
- `qrTextReceived` - QR code scan result
- `clipboardTextReceived` - Clipboard text
- `writeAccessRequested` - Write access requested
- `contactRequested` - Contact requested
2025-12-17 20:39:02 +08:00
### Theme Parameters
```javascript
tg.themeParams = {
2025-12-17 20:39:02 +08:00
bg_color, // Background color
text_color, // Text color
hint_color, // Hint color
link_color, // Link color
button_color, // Button color
button_text_color, // Button text color
secondary_bg_color, // Secondary background color
header_bg_color, // Header background color
accent_text_color, // Accent text color
section_bg_color, // Section background color
section_header_text_color, // Section header text color
subtitle_text_color, // Subtitle color
destructive_text_color // Destructive action color
}
```
2025-12-17 20:39:02 +08:00
## Development Tools
2025-12-17 20:39:02 +08:00
### @BotFather Commands
The core tool for creating and managing Bots:
2025-12-17 20:39:02 +08:00
**Bot Management:**
- `/newbot` - Create a new Bot
- `/mybots` - Manage my Bots
- `/deletebot` - Delete a Bot
- `/token` - Regenerate a token
2025-12-17 20:39:02 +08:00
**Settings Commands:**
- `/setname` - Set name
- `/setdescription` - Set description
- `/setabouttext` - Set about text
- `/setuserpic` - Set user picture
2025-12-17 20:39:02 +08:00
**Feature Configuration:**
- `/setcommands` - Set command list
- `/setinline` - Enable inline mode
- `/setinlinefeedback` - Inline feedback
- `/setjoingroups` - Allow joining groups
- `/setprivacy` - Privacy mode
2025-12-17 20:39:02 +08:00
**Payments and Games:**
- `/setgamescores` - Game scores
- `/setpayments` - Configure payments
2025-12-17 20:39:02 +08:00
**Mini Apps:**
- `/newapp` - Create a Mini App
- `/myapps` - Manage Mini Apps
- `/setmenubutton` - Set menu button
2025-12-17 20:39:02 +08:00
### Getting an API ID
Visit https://my.telegram.org
1. Log in to your account
2. Go to API development tools
3. Create an application
4. Get your API ID and API Hash
2025-12-17 20:39:02 +08:00
## Common Python Libraries
### python-telegram-bot
```bash
pip install python-telegram-bot
```
2025-12-17 20:39:02 +08:00
**Features:**
- Complete Bot API wrapper
- Asynchronous and synchronous support
- Rich extensions
- Actively maintained
2025-12-17 20:39:02 +08:00
**Basic Example:**
```python
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
2025-12-17 20:39:02 +08:00
await update.message.reply_text('Hello!')
app = Application.builder().token("TOKEN").build()
app.add_handler(CommandHandler("start", start))
app.run_polling()
```
### aiogram
```bash
pip install aiogram
```
2025-12-17 20:39:02 +08:00
**Features:**
- Purely asynchronous
- High performance
- FSM state machine
- Middleware system
### Telethon / Pyrogram
2025-12-17 20:39:02 +08:00
MTProto client libraries:
```bash
pip install telethon
pip install pyrogram
```
2025-12-17 20:39:02 +08:00
**Uses:**
- Custom clients
- User account automation
- Full Telegram functionality
2025-12-17 20:39:02 +08:00
## Common Node.js Libraries
### node-telegram-bot-api
```bash
npm install node-telegram-bot-api
```
### Telegraf
```bash
npm install telegraf
```
2025-12-17 20:39:02 +08:00
**Features:**
- Modern
- Middleware architecture
- TypeScript support
### grammY
```bash
npm install grammy
```
2025-12-17 20:39:02 +08:00
**Features:**
- Lightweight
- Type-safe
- Plugin ecosystem
2025-12-17 20:39:02 +08:00
## Deployment Options
2025-12-17 20:39:02 +08:00
### Webhook Hosting
**Recommended Platforms:**
- Heroku
- AWS Lambda
- Google Cloud Functions
- Azure Functions
- Vercel
- Railway
- Render
2025-12-17 20:39:02 +08:00
**Requirements:**
- HTTPS support
- Publicly accessible
- Supported ports: 443, 80, 88, 8443
2025-12-17 20:39:02 +08:00
### Long Polling Hosting
**Recommended Platforms:**
- VPS (Vultr, DigitalOcean, Linode)
- Raspberry Pi
2025-12-17 20:39:02 +08:00
- Local server
2025-12-17 20:39:02 +08:00
**Advantages:**
- No HTTPS required
- Simple configuration
- Suitable for development and testing
2025-12-17 20:39:02 +08:00
## Security Best Practices
2025-12-17 20:39:02 +08:00
1. **Token Security**
- Do not commit to Git
- Use environment variables
- Rotate tokens regularly
2025-12-17 20:39:02 +08:00
2. **Data Validation**
- Validate initData
- Server-side validation
- Do not trust the client
2025-12-17 20:39:02 +08:00
3. **Permission Control**
- Check user permissions
- Admin verification
- Group permissions
2025-12-17 20:39:02 +08:00
4. **Rate Limiting**
- Implement request limits
- Prevent abuse
- Monitor for anomalies
2025-12-17 20:39:02 +08:00
## Debugging Tips
2025-12-17 20:39:02 +08:00
### Bot Debugging
```python
import logging
logging.basicConfig(level=logging.DEBUG)
```
2025-12-17 20:39:02 +08:00
### Mini App Debugging
```javascript
2025-12-17 20:39:02 +08:00
// Enable debug mode
tg.showAlert(JSON.stringify(tg.initDataUnsafe, null, 2));
2025-12-17 20:39:02 +08:00
// Console logs
console.log('WebApp version:', tg.version);
console.log('Platform:', tg.platform);
console.log('Theme:', tg.colorScheme);
```
2025-12-17 20:39:02 +08:00
### Webhook Testing
Use ngrok for local testing:
```bash
ngrok http 5000
2025-12-17 20:39:02 +08:00
# Set the generated https URL as the webhook
```
2025-12-17 20:39:02 +08:00
## Community Resources
2025-12-17 20:39:02 +08:00
- **Telegram Developer Group**: @BotDevelopers
- **Telegram API Discussion**: @TelegramBots
- **Mini Apps Discussion**: @WebAppChat
2025-12-17 20:39:02 +08:00
## Changelog
2025-12-17 20:39:02 +08:00
**Latest Features:**
- Paid Media
- Checklist Tasks
- Gift Conversion
- Business Features
- Poll options increased to 12
- Story publishing and editing
---
2025-12-17 20:39:02 +08:00
## Complete Implementation Templates (New)
2025-12-17 20:39:02 +08:00
### Telegram Bot Button and Keyboard Implementation Guide
**File:** `Telegram_Bot_button_and_keyboard_implementation_template.md`
**Lines:** 404
**Size:** 12 KB
**Language:** Chinese
2025-12-17 20:39:02 +08:00
A concise and practical guide to implementing interactive features for Telegram Bots:
2025-12-17 20:39:02 +08:00
**Core Content:**
- Detailed explanation of three button types (Inline/Reply/Command Menu)
- Comparison of implementations with python-telegram-bot and Telethon
- Complete code examples (ready to use)
- Project structure and modular design
- Handler priority and event handling
- Production deployment solutions
- Security and error handling best practices
2025-12-17 20:39:02 +08:00
**Features:**
- Concise core code, removing redundant examples
- Focus on common scenarios and practical tips
- A complete quick reference table
---
2025-12-17 20:39:02 +08:00
### Dynamic View Alignment - Data Display Guide
**File:** `dynamic-view-alignment-implementation-document.md`
**Lines:** 407
**Size:** 12 KB
- **Language:** Chinese
2025-12-17 20:39:02 +08:00
A professional solution for monospaced font data alignment and formatting:
2025-12-17 20:39:02 +08:00
**Core Features:**
- Intelligent dynamic view alignment algorithm (three-step method)
- Automatic column width calculation, no hardcoding required
- Smart alignment rules (text left, numbers right)
- Complete formatting system:
- Smart abbreviation for trading volume (B/M/K)
- Smart precision for price (adaptive decimal places)
- Formatting for price change percentage (+/- signs)
- Smart display for fund flow
2025-12-17 20:39:02 +08:00
**Use Cases:**
- Leaderboards, data tables, real-time tickers
- Any Telegram Bot that needs professional data display
2025-12-17 20:39:02 +08:00
**Technical Features:**
- O(n×m) linear complexity, highly efficient
- Processes 1000 rows of data in just 5-10ms
- Supports Chinese character width expansion
2025-12-17 20:39:02 +08:00
**Visual Effect Example:**
```
1. BTC $1.23B $45,000 +5.23%
2. ETH $890.5M $2,500 +3.12%
3. SOL $567.8M $101 +8.45%
```
---
2025-12-17 20:39:02 +08:00
**These templates provide a complete solution for Telegram Bot development, from basic to production level!**