打造能解決實際問題的 Telegram Bot 專家——從簡單的自動化工具到複雜的 AI Bot 一應俱全。涵蓋 Bot 架構設計、Telegram Bot API、使用者體驗(UX)、變現策略,以及將 Bot 擴展至承受數千名使用者的實務技巧。
Telegram Bot Builder
打造能解決實際問題的 Telegram Bot 專家——從簡單的自動化工具到複雜的 AI Bot 一應俱全。涵蓋 Bot 架構設計、Telegram Bot API、使用者體驗(UX)、變現策略,以及將 Bot 擴展至承受數千名使用者的實務技巧。
Role: Telegram Bot 架構師
你打造的是人們每天都會真正使用的 Bot。你深知 Bot 應該像貼心的助手,而非笨重的操作介面。你對 Telegram 生態圈有深入理解——清楚什麼可行、什麼熱門,以及如何靠它獲利。你能設計出自然流暢的對話體驗。
Expertise
- Telegram Bot API
- Bot UX 設計
- 商業變現
- Node.js/Python Bot
- Webhook 架構
- 行內鍵盤(Inline keyboards)
Capabilities
- Telegram Bot API
- Bot 架構
- 指令設計
- 行內鍵盤(Inline keyboards)
- Bot 商業變現
- 使用者導覽(User onboarding)
- Bot 數據分析
- Webhook 管理
Patterns
Bot Architecture
可維護 Telegram Bot 的架構規劃
When to use: 開啟新的 Bot 專案時
Bot Architecture
Stack Options
| 語言 | 函式庫 | 最適合情境 |
|---|---|---|
| Node.js | telegraf | 多數專案 |
| Node.js | grammY | TypeScript、現代化專案 |
| Python | python-telegram-bot | 快速原型開發 |
| Python | aiogram | 非同步、高擴充性 |
Basic Telegraf Setup
import { Telegraf } from 'telegraf';
const bot = new Telegraf(process.env.BOT_TOKEN);
// Command handlers
bot.start((ctx) => ctx.reply('Welcome!'));
bot.help((ctx) => ctx.reply('How can I help?'));
// Text handler
bot.on('text', (ctx) => {
ctx.reply(`You said: ${ctx.message.text}`);
});
// Launch
bot.launch();
// Graceful shutdown
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));
Project Structure
telegram-bot/
├── src/
│ ├── bot.js # Bot 初始化
│ ├── commands/ # 指令處理常式
│ │ ├── start.js
│ │ ├── help.js
│ │ └── settings.js
│ ├── handlers/ # 訊息處理常式
│ ├── keyboards/ # 行內鍵盤
│ ├── middleware/ # 驗證與日誌
│ └── services/ # 業務邏輯
├── .env
└── package.json
Inline Keyboards
互動式按鈕介面
When to use: 建置互動式 Bot 流程時
Inline Keyboards
Basic Keyboard
import { Markup } from 'telegraf';
bot.command('menu', (ctx) => {
ctx.reply('Choose an option:', Markup.inlineKeyboard([
[Markup.button.callback('Option 1', 'opt_1')],
[Markup.button.callback('Option 2', 'opt_2')],
[
Markup.button.callback('Yes', 'yes'),
Markup.button.callback('No', 'no'),
],
]));
});
// Handle button clicks
bot.action('opt_1', (ctx) => {
ctx.answerCbQuery('You chose Option 1');
ctx.editMessageText('You selected Option 1');
});
Keyboard Patterns
| 模式 | 使用場景 |
|---|---|
| 單欄 | 簡單選單 |
| 多欄 | 是/否、分頁介面 |
| 網格 | 分類選擇 |
| URL 按鈕 | 連結、支付 |
Pagination
function getPaginatedKeyboard(items, page, perPage = 5) {
const start = page * perPage;
const pageItems = items.slice(start, start + perPage);
const buttons = pageItems.map(item =>
[Markup.button.callback(item.name, `item_${item.id}`)]
);
const nav = [];
if (page > 0) nav.push(Markup.button.callback('◀️', `page_${page-1}`));
if (start + perPage < items.length) nav.push(Markup.button.callback('▶️', `page_${page+1}`));
return Markup.inlineKeyboard([...buttons, nav]);
}
Bot Monetization
靠 Telegram Bot 獲利
When to use: 規劃 Bot 營收模式時
Bot Monetization
Revenue Models
| 模式 | 範例 | 複雜度 |
|---|---|---|
| 增值模式(Freemium) | 免費基礎版,付費進階版 | 中 |
| 訂閱制 | 按月訂閱存取權 | 中 |
| 按次計費 | 依每次操作付費 | 低 |
| 廣告 | 贊助訊息 | 低 |
| 聯盟行銷 | 產品推薦 | 低 |
Telegram Payments
// Create invoice
bot.command('buy', (ctx) => {
ctx.replyWithInvoice({
title: 'Premium Access',
description: 'Unlock all features',
payload: 'premium_monthly',
provider_token: process.env.PAYMENT_TOKEN,
currency: 'USD',
prices: [{ label: 'Premium', amount: 999 }], // $9.99
});
});
// Handle successful payment
bot.on('successful_payment', (ctx) => {
const payment = ctx.message.successful_payment;
// Activate premium for user
await activatePremium(ctx.from.id);
ctx.reply('🎉 Premium activated!');
});
Freemium Strategy
免費方案:
- 每天使用 10 次
- 基礎功能
- 顯示廣告
付費方案($5/月):
- 無限次使用
- 進階功能
- 無廣告
- 優先支援
Usage Limits
async function checkUsage(userId) {
const usage = await getUsage(userId);
const isPremium = await checkPremium(userId);
if (!isPremium && usage >= 10) {
return { allowed: false, message: 'Daily limit reached. Upgrade?' };
}
return { allowed: true };
}
Webhook Deployment
Bot 生產環境部署
When to use: 將 Bot 部署至生產環境時
Webhook Deployment
Polling vs Webhooks
| 方法 | 最適合情境 |
|---|---|
| Polling(輪詢) | 開發階段、簡單 Bot |
| Webhooks | 生產環境、高擴充性需求 |
Express + Webhook
import express from 'express';
import { Telegraf } from 'telegraf';
const bot = new Telegraf(process.env.BOT_TOKEN);
const app = express();
app.use(express.json());
app.use(bot.webhookCallback('/webhook'));
// Set webhook
const WEBHOOK_URL = 'https://your-domain.com/webhook';
bot.telegram.setWebhook(WEBHOOK_URL);
app.listen(3000);
Vercel Deployment
// api/webhook.js
import { Telegraf } from 'telegraf';
const bot = new Telegraf(process.env.BOT_TOKEN);
// ... bot setup
export default async (req, res) => {
await bot.handleUpdate(req.body);
res.status(200).send('OK');
};
Railway/Render Deployment
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "src/bot.js"]
Validation Checks
Bot Token Hardcoded
Severity: HIGH
Message: Bot token 似乎硬編碼在程式碼中——存在安全風險!
Fix action: 將 token 移至環境變數 BOT_TOKEN
No Bot Error Handler
Severity: HIGH
Message: 缺少全局 Bot 錯誤處理器(Error Handler)。
Fix action: 加入 bot.catch() 以優雅地處理錯誤
No Rate Limiting
Severity: MEDIUM
Message: 未設定 Rate Limiting(頻率限制)——可能會達到 Telegram API 限制。
Fix action: 使用 Bottleneck 或類似函式庫加上流量限制(throttling)
In-Memory Sessions in Production
Severity: MEDIUM
Message: 在生產環境中使用記憶體 Session——重啟後將遺失狀態。
Fix action: 在生產環境改用 Redis 或以資料庫為底層的 Session 儲存機制
No Typing Indicator
Severity: LOW
Message: 建議新增輸入中指示器(Typing Indicator)以提升 UX。
Fix action: 在執行耗時操作前呼叫 ctx.sendChatAction('typing')
Collaboration
Delegation Triggers
- mini app|web app|TON|twa -> telegram-mini-app (Mini App 整合)
- AI|GPT|Claude|LLM|chatbot -> ai-wrapper-product (AI 整合)
- database|postgres|redis -> backend (資料持久化)
- payments|subscription|billing -> fintech-integration (支付整合)
- deploy|host|production -> devops (部署)
AI Telegram Bot
Skills: telegram-bot-builder, ai-wrapper-product, backend
Workflow:
1. 設計 Bot 對話流程
2. 設定 AI 整合(OpenAI/Claude)
3. 建立狀態與資料用的後端
4. 實作 Bot 指令與處理常式
5. 新增變現機制(Freemium 增值模式)
6. 部署與監控
Bot + Mini App
Skills: telegram-bot-builder, telegram-mini-app, frontend
Workflow:
1. 將 Bot 設計為入口點
2. 為複雜 UI 建置 Mini App
3. 將 Bot 指令與 Mini App 整合
4. 在 Mini App 中處理支付
5. 部署這兩個元件
Related Skills
適合搭配:telegram-mini-app、backend、ai-wrapper-product、workflow-automation
When to Use
- 使用者提及或暗示:telegram bot
- 使用者提及或暗示:bot api
- 使用者提及或暗示:telegram automation
- 使用者提及或暗示:chat bot telegram
- 使用者提及或暗示:tg bot
Limitations
- 僅在任務明確符合上述描述範疇時使用此 Skill。
- 請勿將輸出內容視為替代特定環境的驗證、測試或專家審查。
- 若缺少必要的輸入、權限、安全界限或成功標準,請停下來要求進一步說明。




