telegram-bot

telegram-bot

熱門

Telegram 機器人開發 - 聊天機器人、通知、AI 助手與群組自動化

310星標
68分支
更新於 2026/1/31
SKILL.md
唯讀
名稱
telegram-bot
描述

Telegram 機器人開發 - 聊天機器人、通知、AI 助手與群組自動化

版本
1.0.0

Telegram Bot

建立 Telegram 機器人,用於聊天機器人、通知、AI 助手與群組自動化。基於 n8n 的 Telegram 工作流程範本。

概述

此技能涵蓋:

  • 機器人設定與配置
  • 訊息處理模式
  • AI 驅動的助手
  • 通知工作流程
  • 群組自動化

機器人設定

建立機器人

setup_steps:
  1. create_bot:
      - 開啟: Telegram 上的 @BotFather
      - 指令: /newbot
      - 提供: 機器人名稱
      - 提供: 機器人使用者名稱(必須以 'bot' 結尾)
      - 接收: API_token
      
  2. configure_bot:
      - 指令: /setdescription
      - 指令: /setabouttext
      - 指令: /setuserpic
      - 指令: /setcommands
      
  3. get_chat_id:
      - 開始: 與機器人對話
      - 呼叫: https://api.telegram.org/bot{TOKEN}/getUpdates
      - 提取: 從回應中取得 chat.id

機器人指令

commands:
  - command: /start
    description: "啟動機器人"
    
  - command: /help
    description: "顯示可用指令"
    
  - command: /status
    description: "檢查系統狀態"
    
  - command: /subscribe
    description: "訂閱通知"
    
  - command: /unsubscribe
    description: "取消訂閱通知"

訊息處理器

基本訊息處理器

workflow: "Telegram 訊息處理器"
trigger: telegram_message

handlers:
  text_message:
    action: |
      1. 解析訊息文字
      2. 判斷意圖
      3. 處理請求
      4. 發送回應
      
  command:
    pattern: "^/"
    action: 路由到指令處理器
    
  photo:
    action: |
      1. 下載照片
      2. 使用視覺 AI 處理
      3. 回覆分析結果
      
  document:
    action: |
      1. 下載文件
      2. 提取內容
      3. 處理並回覆
      
  voice:
    action: |
      1. 下載音訊
      2. 使用 Whisper 轉錄
      3. 處理文字
      4. 回覆(文字或語音)
      
  location:
    action: |
      1. 提取座標
      2. 查詢當地資訊
      3. 回覆相關資料

n8n 工作流程

workflow: "Telegram Bot n8n"

nodes:
  - name: "Telegram 觸發器"
    type: "n8n-nodes-base.telegramTrigger"
    parameters:
      updates: ["message", "callback_query"]
      
  - name: "路由訊息類型"
    type: "n8n-nodes-base.switch"
    parameters:
      rules:
        - output: 0
          condition: "{{ $json.message.text.startsWith('/') }}"
        - output: 1
          condition: "{{ $json.message.photo }}"
        - output: 2
          condition: "{{ $json.message.voice }}"
        - output: 3
          fallback: true
          
  - name: "使用 AI 處理"
    type: "n8n-nodes-base.openAi"
    parameters:
      model: "gpt-4"
      messages:
        - role: "system"
          content: "你是一個有用的 Telegram 助手。"
        - role: "user"
          content: "{{ $json.message.text }}"
          
  - name: "發送回應"
    type: "n8n-nodes-base.telegram"
    parameters:
      chatId: "{{ $json.message.chat.id }}"
      text: "{{ $json.response }}"

AI 驅動機器人

GPT-4 整合

ai_bot:
  name: "AI 助手機器人"
  
  system_prompt: |
    你是一個在 Telegram 上的有用 AI 助手。
    
    指導原則:
    - 簡潔(Telegram 有訊息長度限制)
    - 適當使用表情符號
    - 必要時使用 Markdown 格式
    - 如有需要,提出澄清問題
    
  features:
    - conversational_memory: true
    - context_window: 最近 10 則訊息
    - tools: [web_search, calculator, weather]
    
  message_formatting:
    max_length: 4096
    split_long_messages: true
    use_markdown: true

多模態機器人

multimodal_bot:
  handlers:
    text:
      model: gpt-4
      action: chat_completion
      
    image:
      model: gpt-4-vision
      action: analyze_and_respond
      
    voice:
      transcribe: whisper
      process: gpt-4
      respond: text_or_voice
      
    document:
      extract: based_on_type
      summarize: gpt-4
      respond: text

通知系統

警報機器人

workflow: "系統警報機器人"

triggers:
  - source: monitoring_system
    event: alert
  - source: ci_cd
    event: build_status
  - source: ecommerce
    event: new_order
    
notification_templates:
  alert:
    format: |
      🚨 *警報:{severity}*
      
      *服務:* {service}
      *訊息:* {message}
      *時間:* {timestamp}
      
      [查看儀表板]({dashboard_link})
      
  build:
    format: |
      {status_emoji} *建置 {status}*
      
      *專案:* {project}
      *分支:* {branch}
      *提交:* `{commit_short}`
      
      {details}
      
  order:
    format: |
      🛒 *新訂單!*
      
      *訂單編號:* #{order_id}
      *客戶:* {customer}
      *總金額:* ${total}
      *商品數量:* {item_count}
      
routing:
  by_severity:
    critical: [admin_group, on_call_user]
    warning: [team_group]
    info: [logging_channel]

排程通知

scheduled_notifications:
  daily_digest:
    schedule: "每天上午 9 點"
    template: |
      📊 *每日摘要 - {date}*
      
      📈 銷售額:${sales}({change})
      👥 新用戶:{new_users}
      🎫 未處理工單:{tickets}
      
      ㊗️ 祝你有美好的一天!☀️
      
  weekly_report:
    schedule: "週一上午 9 點"
    template: weekly_metrics_report
    
  reminder:
    trigger: custom_event
    template: |
      ⏰ *提醒*
      
      {reminder_text}
      
      排程者:{creator}

群組自動化

歡迎機器人

group_bot:
  on_member_join:
    action: |
      1. 檢查是否為新成員
      2. 發送歡迎訊息
      3. 分享規則
      4. 建議自我介紹
      
    template: |
      👋 歡迎加入 {group_name},{user_name}!
      
      請:
      1. 閱讀 /rules
      2. 自我介紹
      3. 隨時提問!
      
      祝你在這裡愉快!🎉
      
  on_member_leave:
    action: optional_goodbye
    
  moderation:
    - spam_detection: auto_delete + warn
    - link_filtering: whitelist_only
    - flood_control: rate_limit

投票與問卷機器人

poll_bot:
  commands:
    /poll:
      format: "/poll 問題 | 選項1 | 選項2 | ..."
      action: create_poll
      
    /survey:
      action: start_multi_question_survey
      
    /results:
      action: show_aggregated_results
      
  features:
    - anonymous_voting: optional
    - multiple_choice: optional
    - scheduled_close: optional
    - export_results: to_sheets

內聯功能

內聯鍵盤

inline_keyboard:
  example:
    text: "選擇一個選項:"
    keyboard:
      - row:
          - text: "選項 1"
            callback_data: "opt_1"
          - text: "選項 2"
            callback_data: "opt_2"
      - row:
          - text: "取消"
            callback_data: "cancel"
            
  callback_handler:
    opt_1:
      action: process_option_1
      response: "你選擇了選項 1"
      
    opt_2:
      action: process_option_2
      response: "你選擇了選項 2"
      
    cancel:
      action: cancel_operation
      response: "操作已取消"

內聯查詢

inline_mode:
  trigger: "@botname 查詢"
  
  results:
    - type: article
      title: "{query} 結果"
      description: "點擊發送"
      message_text: "查詢結果:{query}"
      
    - type: photo
      photo_url: "{search_result_image}"
      
    - type: gif
      gif_url: "{gif_url}"

輸出範例

請求:"建立一個用於伺服器警報的 Telegram 通知機器人"

輸出

# 伺服器警報 Telegram 機器人

## 設定

1. 使用 @BotFather 建立機器人
2. 取得 Token:`123456:ABC-DEF...`
3. 取得警報頻道的聊天 ID

## n8n 工作流程

```yaml
nodes:
  1. Webhook(接收警報)
  2. 格式化訊息
  3. Telegram 發送

警報範本

重大警報:

🚨 重大警報

伺服器:production-web-01
狀態:DOWN
時間:2026-01-30 14:32:05 UTC

詳細資訊:
- CPU:98%
- 記憶體:95%
- 磁碟:89%

[查看 Grafana] [確認]

恢復:

✅ 已恢復

伺服器:production-web-01
停機時間:5 分鐘
狀態:所有系統正常

事件已自動解決。

實作

// 發送警報函式
async function sendAlert(severity, message, details) {
  const emoji = {
    critical: '🚨',
    warning: '⚠️',
    info: 'ℹ️',
    success: '✅'
  };
  
  const text = `${emoji[severity]} *${severity.toUpperCase()}*\n\n${message}\n\n${details}`;
  
  await telegram.sendMessage({
    chat_id: ALERT_CHANNEL_ID,
    text: text,
    parse_mode: 'Markdown'
  });
}

功能

  • 依嚴重程度路由
  • 內聯動作按鈕
  • 確認追蹤
  • 升級規則

---

*Telegram 機器人技能 - Claude Office Skills 的一部分*