2025-12-16 23:15:43 +08:00
---
name : telegram-dev
2025-12-17 20:26:50 +08:00
description : A full-stack guide to Telegram ecosystem development - covering Bot API, Mini Apps (Web Apps), and MTProto client development. Includes complete development resources for message handling, payments, inline mode, webhooks, authentication, storage, sensor APIs, and more.
2025-12-16 23:15:43 +08:00
---
2025-12-17 20:26:50 +08:00
# Telegram Ecosystem Development Skill
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
A comprehensive guide to Telegram development, covering the full technology stack for Bot development, Mini Apps (Web Apps), and client development.
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
## When to Use This Skill
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
Use this skill when you need help with the following:
- Developing a Telegram Bot (message bot)
- Creating Telegram Mini Apps
- Building a custom Telegram client
- Integrating Telegram payments and business features
- Implementing webhooks and long polling
- Using Telegram authentication and storage
- Handling messages, media, and files
- Implementing inline mode and keyboards
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
## Overview of the Telegram Development Ecosystem
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
### Three Core APIs
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
1. **Bot API** - For creating bot programs
- Simple to use HTTP interface
- Automatically handles encryption and communication
- Suitable for: chatbots, automation tools
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
2. **Mini Apps API** (Web Apps) - For creating web applications
- JavaScript interface
- Runs inside Telegram
- Suitable for: mini-apps, games, e-commerce
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
3. **Telegram API & TDLib** - For creating clients
- Full implementation of the Telegram protocol
- Supports all platforms
- Suitable for: custom clients, enterprise applications
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
## Bot API Development
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
### Quick Start
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**API Endpoint:**
2025-12-16 23:15:43 +08:00
```
https://api.telegram.org/bot<TOKEN>/METHOD_NAME
```
2025-12-17 20:26:50 +08:00
**Get a Bot Token:**
1. Talk to @BotFather
2. Send `/newbot`
3. Follow the prompts to set a name
4. Get the token
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**First Bot (Python):**
2025-12-16 23:15:43 +08:00
```python
import requests
BOT_TOKEN = "your_bot_token_here"
API_URL = f "https://api.telegram.org/bot { BOT_TOKEN } "
2025-12-17 20:26:50 +08:00
# Send a message
2025-12-16 23:15:43 +08:00
def send_message ( chat_id , text ):
url = f " { API_URL } /sendMessage"
data = { "chat_id" : chat_id , "text" : text }
return requests . post ( url , json = data )
2025-12-17 20:26:50 +08:00
# Get updates (long polling)
2025-12-16 23:15:43 +08:00
def get_updates ( offset = None ):
url = f " { API_URL } /getUpdates"
params = { "offset" : offset , "timeout" : 30 }
return requests . get ( url , params = params ) . json ()
2025-12-17 20:26:50 +08:00
# Main loop
2025-12-16 23:15:43 +08:00
offset = None
while True :
updates = get_updates ( offset )
for update in updates . get ( "result" , []):
chat_id = update [ "message" ][ "chat" ][ "id" ]
text = update [ "message" ][ "text" ]
2025-12-17 20:26:50 +08:00
# Reply to the message
send_message ( chat_id , f "You said: { text } " )
2025-12-16 23:15:43 +08:00
offset = update [ "update_id" ] + 1
```
2025-12-17 20:26:50 +08:00
### Core API Methods
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Update Management:**
- `getUpdates` - Get updates via long polling
- `setWebhook` - Set a webhook
- `deleteWebhook` - Delete a webhook
- `getWebhookInfo` - Query webhook status
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Message Operations:**
- `sendMessage` - Send a text message
- `sendPhoto` / `sendVideo` / `sendDocument` - Send media
- `sendAudio` / `sendVoice` - Send audio
- `sendLocation` / `sendVenue` - Send a location
- `editMessageText` - Edit a message
- `deleteMessage` - Delete a message
- `forwardMessage` / `copyMessage` - Forward/copy a message
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Interactive Elements:**
- `sendPoll` - Send a poll (up to 12 options)
- Inline Keyboard (InlineKeyboardMarkup)
- Reply Keyboard (ReplyKeyboardMarkup)
- `answerCallbackQuery` - Respond to a callback query
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**File Operations:**
- `getFile` - Get file information
- `downloadFile` - Download a file
- Supports files up to 2GB (in local Bot API mode)
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Payment Features:**
- `sendInvoice` - Send an invoice
- `answerPreCheckoutQuery` - Process a payment
- Telegram Stars payment (up to 10,000 Stars)
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
### Webhook Configuration
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Set a Webhook:**
2025-12-16 23:15:43 +08:00
```python
import requests
BOT_TOKEN = "your_token"
WEBHOOK_URL = "https://yourdomain.com/webhook"
requests . post (
f "https://api.telegram.org/bot { BOT_TOKEN } /setWebhook" ,
json = { "url" : WEBHOOK_URL }
)
```
2025-12-17 20:26:50 +08:00
**Flask Webhook Example:**
2025-12-16 23:15:43 +08:00
```python
from flask import Flask , request
import requests
app = Flask ( __name__ )
BOT_TOKEN = "your_token"
@app.route ( '/webhook' , methods = [ 'POST' ])
def webhook ():
update = request . get_json ()
chat_id = update [ "message" ][ "chat" ][ "id" ]
text = update [ "message" ][ "text" ]
2025-12-17 20:26:50 +08:00
# Send a reply
2025-12-16 23:15:43 +08:00
requests . post (
f "https://api.telegram.org/bot { BOT_TOKEN } /sendMessage" ,
2025-12-17 20:26:50 +08:00
json = { "chat_id" : chat_id , "text" : f "Received: { text } " }
2025-12-16 23:15:43 +08:00
)
return "OK"
if __name__ == '__main__' :
app . run ( port = 5000 )
```
2025-12-17 20:26:50 +08:00
**Webhook Requirements:**
- Must use HTTPS
- Supports TLS 1.2+
- Ports: 443, 80, 88, 8443
- Publicly accessible URL
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
### Inline Keyboard
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Create an Inline Keyboard:**
2025-12-16 23:15:43 +08:00
```python
def send_inline_keyboard ( chat_id ):
keyboard = {
"inline_keyboard" : [
[
2025-12-17 20:26:50 +08:00
{ "text" : "Button 1" , "callback_data" : "btn1" },
{ "text" : "Button 2" , "callback_data" : "btn2" }
2025-12-16 23:15:43 +08:00
],
[
2025-12-17 20:26:50 +08:00
{ "text" : "Open Link" , "url" : "https://example.com" }
2025-12-16 23:15:43 +08:00
]
]
}
requests . post (
f " { API_URL } /sendMessage" ,
json = {
"chat_id" : chat_id ,
2025-12-17 20:26:50 +08:00
"text" : "Choose an option:" ,
2025-12-16 23:15:43 +08:00
"reply_markup" : keyboard
}
)
```
2025-12-17 20:26:50 +08:00
**Handle Callbacks:**
2025-12-16 23:15:43 +08:00
```python
def handle_callback_query ( callback_query ):
query_id = callback_query [ "id" ]
data = callback_query [ "data" ]
chat_id = callback_query [ "message" ][ "chat" ][ "id" ]
2025-12-17 20:26:50 +08:00
# Respond to the callback
2025-12-16 23:15:43 +08:00
requests . post (
f " { API_URL } /answerCallbackQuery" ,
2025-12-17 20:26:50 +08:00
json = { "callback_query_id" : query_id , "text" : f "You clicked { data } " }
2025-12-16 23:15:43 +08:00
)
2025-12-17 20:26:50 +08:00
# Update the message
2025-12-16 23:15:43 +08:00
requests . post (
f " { API_URL } /editMessageText" ,
json = {
"chat_id" : chat_id ,
"message_id" : callback_query [ "message" ][ "message_id" ],
2025-12-17 20:26:50 +08:00
"text" : f "You chose: { data } "
2025-12-16 23:15:43 +08:00
}
)
```
2025-12-17 20:26:50 +08:00
### Inline Mode
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Configure Inline Mode:**
Talk to @BotFather and send `/setinline`
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Handle Inline Queries:**
2025-12-16 23:15:43 +08:00
```python
def handle_inline_query ( inline_query ):
query_id = inline_query [ "id" ]
query_text = inline_query [ "query" ]
2025-12-17 20:26:50 +08:00
# Create results
2025-12-16 23:15:43 +08:00
results = [
{
"type" : "article" ,
"id" : "1" ,
2025-12-17 20:26:50 +08:00
"title" : "Result 1" ,
2025-12-16 23:15:43 +08:00
"input_message_content" : {
2025-12-17 20:26:50 +08:00
"message_text" : f "You searched for: { query_text } "
2025-12-16 23:15:43 +08:00
}
}
]
requests . post (
f " { API_URL } /answerInlineQuery" ,
json = { "inline_query_id" : query_id , "results" : results }
)
```
2025-12-17 20:26:50 +08:00
## Mini Apps (Web Apps) Development
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
### Initialize a Mini App
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**HTML Template:**
2025-12-16 23:15:43 +08:00
```html
<!DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< script src = "https://telegram.org/js/telegram-web-app.js" ></ script >
< title > My Mini App</ title >
</ head >
< body >
< h1 > Telegram Mini App</ h1 >
2025-12-17 20:26:50 +08:00
< button id = "mainBtn" > Main Button</ button >
2025-12-16 23:15:43 +08:00
< script >
2025-12-17 20:26:50 +08:00
// Get the Telegram WebApp object
2025-12-16 23:15:43 +08:00
const tg = window . Telegram . WebApp ;
2025-12-17 20:26:50 +08:00
// Notify Telegram that the app is ready
2025-12-16 23:15:43 +08:00
tg . ready ();
2025-12-17 20:26:50 +08:00
// Expand to full screen
2025-12-16 23:15:43 +08:00
tg . expand ();
2025-12-17 20:26:50 +08:00
// Display user information
2025-12-16 23:15:43 +08:00
const user = tg . initDataUnsafe ? . user ;
if ( user ) {
2025-12-17 20:26:50 +08:00
console . log ( "Username:" , user . first_name );
console . log ( "User ID:" , user . id );
2025-12-16 23:15:43 +08:00
}
2025-12-17 20:26:50 +08:00
// Configure the main button
tg . MainButton . text = "Submit" ;
2025-12-16 23:15:43 +08:00
tg . MainButton . show ();
tg . MainButton . onClick (() => {
2025-12-17 20:26:50 +08:00
// Send data to the Bot
2025-12-16 23:15:43 +08:00
tg . sendData ( JSON . stringify ({ action : "submit" }));
});
2025-12-17 20:26:50 +08:00
// Add a back button
2025-12-16 23:15:43 +08:00
tg . BackButton . show ();
tg . BackButton . onClick (() => {
tg . close ();
});
</ script >
</ body >
</ html >
```
2025-12-17 20:26:50 +08:00
### Mini App Core API
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**WebApp Object Main Properties:**
2025-12-16 23:15:43 +08:00
```javascript
2025-12-17 20:26:50 +08:00
// Initialization data
tg . initData // Raw initialization string
tg . initDataUnsafe // Parsed object
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
// User and theme
tg . initDataUnsafe . user // User information
tg . themeParams // Theme colors
tg . colorScheme // 'light' or 'dark'
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
// Status
tg . isExpanded // Whether it's full screen
tg . isFullscreen // Whether it's full screen
tg . viewportHeight // Viewport height
tg . platform // Platform type
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
// Version
tg . version // WebApp version
2025-12-16 23:15:43 +08:00
```
2025-12-17 20:26:50 +08:00
**Main Methods:**
2025-12-16 23:15:43 +08:00
```javascript
2025-12-17 20:26:50 +08:00
// Window control
tg . ready () // Mark the app as ready
tg . expand () // Expand to full height
tg . close () // Close the Mini App
tg . requestFullscreen () // Request full screen
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
// Data sending
tg . sendData ( data ) // Send data to the Bot
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
// Navigation
tg . openLink ( url ) // Open an external link
tg . openTelegramLink ( url ) // Open a Telegram link
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
// Dialogs
tg . showPopup ( params , callback ) // Show a popup
tg . showAlert ( message ) // Show an alert
tg . showConfirm ( message ) // Show a confirmation
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
// Sharing
tg . shareMessage ( message ) // Share a message
tg . shareUrl ( url ) // Share a link
2025-12-16 23:15:43 +08:00
```
2025-12-17 20:26:50 +08:00
### UI Controls
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Main Button (MainButton):**
2025-12-16 23:15:43 +08:00
```javascript
2025-12-17 20:26:50 +08:00
tg . MainButton . setText ( "Click Me" );
2025-12-16 23:15:43 +08:00
tg . MainButton . show ();
tg . MainButton . enable ();
2025-12-17 20:26:50 +08:00
tg . MainButton . showProgress (); // Show loading
2025-12-16 23:15:43 +08:00
tg . MainButton . hideProgress ();
tg . MainButton . onClick (() => {
2025-12-17 20:26:50 +08:00
console . log ( "Main button clicked" );
2025-12-16 23:15:43 +08:00
});
```
2025-12-17 20:26:50 +08:00
**Secondary Button (SecondaryButton):**
2025-12-16 23:15:43 +08:00
```javascript
2025-12-17 20:26:50 +08:00
tg . SecondaryButton . setText ( "Cancel" );
2025-12-16 23:15:43 +08:00
tg . SecondaryButton . show ();
tg . SecondaryButton . onClick (() => {
tg . close ();
});
```
2025-12-17 20:26:50 +08:00
**Back Button (BackButton):**
2025-12-16 23:15:43 +08:00
```javascript
tg . BackButton . show ();
tg . BackButton . onClick (() => {
2025-12-17 20:26:50 +08:00
// Back logic
2025-12-16 23:15:43 +08:00
});
```
2025-12-17 20:26:50 +08:00
**Haptic Feedback:**
2025-12-16 23:15:43 +08:00
```javascript
tg . HapticFeedback . impactOccurred ( 'light' ); // light, medium, heavy
tg . HapticFeedback . notificationOccurred ( 'success' ); // success, warning, error
tg . HapticFeedback . selectionChanged ();
```
2025-12-17 20:26:50 +08:00
### Storage API
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Cloud Storage:**
2025-12-16 23:15:43 +08:00
```javascript
2025-12-17 20:26:50 +08:00
// Save data
2025-12-16 23:15:43 +08:00
tg . CloudStorage . setItem ( 'key' , 'value' , ( error , success ) => {
2025-12-17 20:26:50 +08:00
if ( success ) console . log ( 'Saved successfully' );
2025-12-16 23:15:43 +08:00
});
2025-12-17 20:26:50 +08:00
// Get data
2025-12-16 23:15:43 +08:00
tg . CloudStorage . getItem ( 'key' , ( error , value ) => {
2025-12-17 20:26:50 +08:00
console . log ( 'Value:' , value );
2025-12-16 23:15:43 +08:00
});
2025-12-17 20:26:50 +08:00
// Delete data
2025-12-16 23:15:43 +08:00
tg . CloudStorage . removeItem ( 'key' );
2025-12-17 20:26:50 +08:00
// Get all keys
2025-12-16 23:15:43 +08:00
tg . CloudStorage . getKeys (( error , keys ) => {
2025-12-17 20:26:50 +08:00
console . log ( 'All keys:' , keys );
2025-12-16 23:15:43 +08:00
});
```
2025-12-17 20:26:50 +08:00
**Local Storage:**
2025-12-16 23:15:43 +08:00
```javascript
2025-12-17 20:26:50 +08:00
// Normal local storage
2025-12-16 23:15:43 +08:00
localStorage . setItem ( 'key' , 'value' );
const value = localStorage . getItem ( 'key' );
2025-12-17 20:26:50 +08:00
// Secure storage (requires biometrics)
2025-12-16 23:15:43 +08:00
tg . SecureStorage . setItem ( 'secret' , 'value' , callback );
tg . SecureStorage . getItem ( 'secret' , callback );
```
2025-12-17 20:26:50 +08:00
### Biometric Authentication
2025-12-16 23:15:43 +08:00
```javascript
const bioManager = tg . BiometricManager ;
2025-12-17 20:26:50 +08:00
// Initialize
2025-12-16 23:15:43 +08:00
bioManager . init (() => {
if ( bioManager . isInited ) {
2025-12-17 20:26:50 +08:00
console . log ( 'Supported type:' , bioManager . biometricType );
2025-12-16 23:15:43 +08:00
// 'finger', 'face', 'unknown'
if ( bioManager . isAccessGranted ) {
2025-12-17 20:26:50 +08:00
// Already authorized, can be used
2025-12-16 23:15:43 +08:00
} else {
2025-12-17 20:26:50 +08:00
// Request authorization
bioManager . requestAccess ({ reason : 'Need to verify identity' }, ( success ) => {
2025-12-16 23:15:43 +08:00
if ( success ) {
2025-12-17 20:26:50 +08:00
console . log ( 'Authorization successful' );
2025-12-16 23:15:43 +08:00
}
});
}
}
});
2025-12-17 20:26:50 +08:00
// Perform authentication
bioManager . authenticate ({ reason : 'Confirm action' }, ( success , token ) => {
2025-12-16 23:15:43 +08:00
if ( success ) {
2025-12-17 20:26:50 +08:00
console . log ( 'Authentication successful, token:' , token );
2025-12-16 23:15:43 +08:00
}
});
```
2025-12-17 20:26:50 +08:00
### Location and Sensors
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Get Location:**
2025-12-16 23:15:43 +08:00
```javascript
tg . LocationManager . init (() => {
if ( tg . LocationManager . isInited ) {
tg . LocationManager . getLocation (( location ) => {
2025-12-17 20:26:50 +08:00
console . log ( 'Latitude:' , location . latitude );
console . log ( 'Longitude:' , location . longitude );
2025-12-16 23:15:43 +08:00
});
}
});
```
2025-12-17 20:26:50 +08:00
**Accelerometer:**
2025-12-16 23:15:43 +08:00
```javascript
tg . Accelerometer . start ({ refresh_rate : 100 }, ( started ) => {
if ( started ) {
tg . Accelerometer . onEvent (( event ) => {
2025-12-17 20:26:50 +08:00
console . log ( 'Acceleration:' , event . x , event . y , event . z );
2025-12-16 23:15:43 +08:00
});
}
});
2025-12-17 20:26:50 +08:00
// Stop
2025-12-16 23:15:43 +08:00
tg . Accelerometer . stop ();
```
2025-12-17 20:26:50 +08:00
**Gyroscope:**
2025-12-16 23:15:43 +08:00
```javascript
tg . Gyroscope . start ({ refresh_rate : 100 }, callback );
tg . Gyroscope . onEvent (( event ) => {
2025-12-17 20:26:50 +08:00
console . log ( 'Rotation speed:' , event . x , event . y , event . z );
2025-12-16 23:15:43 +08:00
});
```
2025-12-17 20:26:50 +08:00
**Device Orientation:**
2025-12-16 23:15:43 +08:00
```javascript
tg . DeviceOrientation . start ({ refresh_rate : 100 }, callback );
tg . DeviceOrientation . onEvent (( event ) => {
2025-12-17 20:26:50 +08:00
console . log ( 'Orientation:' , event . absolute , event . alpha , event . beta , event . gamma );
2025-12-16 23:15:43 +08:00
});
```
2025-12-17 20:26:50 +08:00
### Payment Integration
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Initiate a Payment (Telegram Stars):**
2025-12-16 23:15:43 +08:00
```javascript
tg . openInvoice ( 'https://t.me/$invoice_link' , ( status ) => {
if ( status === 'paid' ) {
2025-12-17 20:26:50 +08:00
console . log ( 'Payment successful' );
2025-12-16 23:15:43 +08:00
} else if ( status === 'cancelled' ) {
2025-12-17 20:26:50 +08:00
console . log ( 'Payment cancelled' );
2025-12-16 23:15:43 +08:00
} else if ( status === 'failed' ) {
2025-12-17 20:26:50 +08:00
console . log ( 'Payment failed' );
2025-12-16 23:15:43 +08:00
}
});
```
2025-12-17 20:26:50 +08:00
### Data Validation
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Server-side Validation of initData (Python):**
2025-12-16 23:15:43 +08:00
```python
import hmac
import hashlib
from urllib.parse import parse_qs
def validate_init_data ( init_data , bot_token ):
2025-12-17 20:26:50 +08:00
# Parse the data
2025-12-16 23:15:43 +08:00
parsed = parse_qs ( init_data )
received_hash = parsed . get ( 'hash' , [ '' ])[ 0 ]
2025-12-17 20:26:50 +08:00
# Remove the hash
2025-12-16 23:15:43 +08:00
data_check_arr = []
for key , value in parsed . items ():
if key != 'hash' :
data_check_arr . append ( f " { key } = { value [ 0 ] } " )
2025-12-17 20:26:50 +08:00
# Sort
2025-12-16 23:15:43 +08:00
data_check_arr . sort ()
data_check_string = ' \n ' . join ( data_check_arr )
2025-12-17 20:26:50 +08:00
# Calculate the secret key
2025-12-16 23:15:43 +08:00
secret_key = hmac . new (
b "WebAppData" ,
bot_token . encode (),
hashlib . sha256
) . digest ()
2025-12-17 20:26:50 +08:00
# Calculate the hash
2025-12-16 23:15:43 +08:00
calculated_hash = hmac . new (
secret_key ,
data_check_string . encode (),
hashlib . sha256
) . hexdigest ()
return calculated_hash == received_hash
```
2025-12-17 20:26:50 +08:00
### Launching a Mini App
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**From a Keyboard Button:**
2025-12-16 23:15:43 +08:00
```python
keyboard = {
2025-12-17 20:26:50 +08:00
"keyboard" : [[
2025-12-16 23:15:43 +08:00
{
2025-12-17 20:26:50 +08:00
"text" : "Open App" ,
2025-12-16 23:15:43 +08:00
"web_app" : { "url" : "https://yourdomain.com/app" }
}
]],
"resize_keyboard" : True
}
requests . post (
f " { API_URL } /sendMessage" ,
json = {
"chat_id" : chat_id ,
2025-12-17 20:26:50 +08:00
"text" : "Click the button to open the app" ,
2025-12-16 23:15:43 +08:00
"reply_markup" : keyboard
}
)
```
2025-12-17 20:26:50 +08:00
**From an Inline Button:**
2025-12-16 23:15:43 +08:00
```python
keyboard = {
"inline_keyboard" : [[
{
2025-12-17 20:26:50 +08:00
"text" : "Launch App" ,
2025-12-16 23:15:43 +08:00
"web_app" : { "url" : "https://yourdomain.com/app" }
}
]]
}
```
2025-12-17 20:26:50 +08:00
**From the Menu Button:**
Talk to @BotFather:
2025-12-16 23:15:43 +08:00
```
/setmenubutton
2025-12-17 20:26:50 +08:00
→ Choose your Bot
→ Provide URL: https://yourdomain.com/app
2025-12-16 23:15:43 +08:00
```
2025-12-17 20:26:50 +08:00
## Client Development (TDLib)
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
### Using TDLib
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Python Example (python-telegram):**
2025-12-16 23:15:43 +08:00
```python
from telegram.client import Telegram
tg = Telegram (
api_id = 'your_api_id' ,
api_hash = 'your_api_hash' ,
phone = '+1234567890' ,
database_encryption_key = 'changeme1234' ,
)
tg . login ()
2025-12-17 20:26:50 +08:00
# Send a message
2025-12-16 23:15:43 +08:00
result = tg . send_message (
chat_id = 123456789 ,
text = 'Hello from TDLib!'
)
2025-12-17 20:26:50 +08:00
# Get chat list
2025-12-16 23:15:43 +08:00
result = tg . get_chats ()
result . wait ()
chats = result . update
print ( chats )
tg . stop ()
```
2025-12-17 20:26:50 +08:00
### MTProto Protocol
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
**Features:**
- End-to-end encryption
- High performance
- Supports all Telegram features
- Requires API ID/Hash (from https://my.telegram.org)
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
## Best Practices
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
### Bot Development
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
1. **Error Handling**
```python
try:
response = requests.post(url, json=data, timeout=10)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
` ``
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
2. **Rate Limiting**
- Group messages: max 20/minute
- Private messages: max 30/second
- Global limits: avoid being too frequent
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
3. **Use Webhooks instead of Long Polling**
- More efficient
- Lower latency
- Better scalability
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
4. **Data Validation**
- Always validate initData
- Don't trust client-side data
- Server-side validation for all operations
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
### Mini Apps Development
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
1. **Responsive Design**
` ``javascript
// Listen for theme changes
tg.onEvent('themeChanged', () => {
document.body.style.backgroundColor = tg.themeParams.bg_color;
});
// Listen for viewport changes
tg.onEvent('viewportChanged', () => {
console.log('New height:', tg.viewportHeight);
});
` ``
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
2. **Performance Optimization**
- Minimize JavaScript bundle size
- Use lazy loading
- Optimize images and resources
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
3. **User Experience**
- Adapt to dark/light themes
- Use native UI controls (MainButton, etc.)
- Provide haptic feedback
- Respond quickly to user actions
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
4. **Security Considerations**
- HTTPS is mandatory
- Validate initData
- Don't store sensitive information on the client
- Use SecureStorage for secrets
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
## Common Libraries and Tools
2025-12-16 23:15:43 +08:00
### Python
2025-12-17 20:26:50 +08:00
- ` python-telegram-bot` - A powerful Bot framework
- ` aiogram` - An asynchronous Bot framework
- ` telethon` / ` pyrogram` - MTProto clients
2025-12-16 23:15:43 +08:00
### Node.js
2025-12-17 20:26:50 +08:00
- ` node-telegram-bot-api` - Bot API wrapper
- ` telegraf` - Modern Bot framework
- ` grammy` - Lightweight framework
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
### Other Languages
2025-12-16 23:15:43 +08:00
- PHP: ` telegram-bot-sdk`
- Go: ` telegram-bot-api`
- Java: ` TelegramBots`
- C#: ` Telegram.Bot`
2025-12-17 20:26:50 +08:00
## Reference Resources
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
### Official Documentation
2025-12-16 23:15:43 +08:00
- Bot API: https://core.telegram.org/bots/api
- Mini Apps: https://core.telegram.org/bots/webapps
- Mini Apps Platform: https://docs.telegram-mini-apps.com
- Telegram API: https://core.telegram.org
2025-12-17 20:26:50 +08:00
### GitHub Repositories
- Bot API Server: https://github.com/tdlib/telegram-bot-api
- Android Client: https://github.com/DrKLO/Telegram
- Desktop Client: https://github.com/telegramdesktop/tdesktop
- Official Organization: https://github.com/orgs/TelegramOfficial/repositories
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
### Tools
- @BotFather - Create and manage Bots
- https://my.telegram.org - Get API ID/Hash
- Telegram Web App test environment
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
## Reference Files
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
This skill includes a detailed index of Telegram development resources and complete implementation templates:
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
- **index.md** - A complete index of resources and quick navigation
- **Telegram_Bot_按钮和键盘实现模板.md** - An implementation guide for interactive buttons and keyboards (404 lines, 12 KB)
- Detailed explanation of three button types (Inline/Reply/Command Menu)
- Comparison of implementations with python-telegram-bot and Telethon
- Complete ready-to-use code examples and project structure
- Handler system, error handling, and deployment方案
- **动态视图对齐实现文档.md** - A guide to data display in Telegram (407 lines, 12 KB)
- Intelligent dynamic alignment algorithm (three-step method, O(n× m) complexity)
- Perfect alignment solution for monospaced font environments
- Intelligent numerical formatting system (automatic B/M/K abbreviation)
- Professional display for leaderboards and data tables
2025-12-16 23:15:43 +08:00
2025-12-17 20:26:50 +08:00
These concise guides provide core solutions for Telegram Bot development:
- All implementation methods for button and keyboard interaction
- Professional formatting and display of messages and data
- Practical best practices and quick references
2025-12-16 23:15:43 +08:00
---
2025-12-17 20:26:50 +08:00
**Master full-stack development of the Telegram ecosystem with this skill!**