8
n8n 中文网amn8n.com

Telegram 机器人内联键盘与动态菜单及评分系统

中级

这是一个Support Chatbot, Multimodal AI领域的自动化工作流,包含 12 个节点。主要使用 If, Set, Function, HttpRequest, TelegramTrigger 等节点。 Telegram 机器人内联键盘:动态菜单与评分系统

前置要求
  • 可能需要目标 API 的认证凭证
  • Telegram Bot Token
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "82ee17baaf8f0713e544083c37cbe259962ad0fcd85204d3262a7dfafc166630",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "c995b5f4-c859-4ced-b5c3-2697043c714f",
      "name": "便签 1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        848,
        2752
      ],
      "parameters": {
        "color": 5,
        "width": 373,
        "height": 602,
        "content": "## 🎯 动态菜单构建器"
      },
      "typeVersion": 1
    },
    {
      "id": "e240d222-5b4f-4f43-b2fc-5a71ee6ee69c",
      "name": "便利贴",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1232,
        2752
      ],
      "parameters": {
        "color": 6,
        "width": 320,
        "height": 600,
        "content": "## 🔑 机器人令牌配置"
      },
      "typeVersion": 1
    },
    {
      "id": "7bf4da5a-d39f-4bd9-9a13-57cf9ab79188",
      "name": "便签 6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1568,
        2752
      ],
      "parameters": {
        "color": 4,
        "width": 280,
        "height": 604,
        "content": "## 🚦 消息路由器"
      },
      "typeVersion": 1
    },
    {
      "id": "d8adb59a-0825-4c06-9e0f-867d8cd8f45c",
      "name": "便签 7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1872,
        2752
      ],
      "parameters": {
        "width": 300,
        "height": 312,
        "content": "## 📡 Telegram API 发送器"
      },
      "typeVersion": 1
    },
    {
      "id": "00ee79ff-145f-45e2-8304-08ce4675f4a6",
      "name": "便签 8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1904,
        3312
      ],
      "parameters": {
        "color": 3,
        "width": 276,
        "height": 244,
        "content": "## ✅ 回调应答"
      },
      "typeVersion": 1
    },
    {
      "id": "8d17f857-fd3a-4125-8c9a-cd5ce01d4311",
      "name": "便利贴 主1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        448,
        2656
      ],
      "parameters": {
        "color": 7,
        "width": 1744,
        "height": 1064,
        "content": "# 🤖 Telegram 机器人内联键盘与动态菜单及评分系统"
      },
      "typeVersion": 1
    },
    {
      "id": "bcbc464a-ddff-4168-b42d-19a3f958aabf",
      "name": "TG 触发器节点",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        608,
        3488
      ],
      "webhookId": "telegram-webhook",
      "parameters": {
        "updates": [
          "message",
          "callback_query"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Telegram - LifeAiToolsBot"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "b172ee06-41dd-4fe4-8453-c841786e57c8",
      "name": "准备魔法响应",
      "type": "n8n-nodes-base.function",
      "position": [
        992,
        3488
      ],
      "parameters": {
        "functionCode": "// Build dynamic keyboard based on message type\nconst data = items[0].json;\n\n// Prepare base response data\nlet chatId, messageId, userName, responseText, keyboard, isCallback = false;\nlet apiMethod = 'sendMessage';\n\nif (data.message) {\n  // Handle regular message\n  const message = data.message;\n  chatId = message.chat.id;\n  \n  // Extract user's actual name from Telegram data\n  const firstName = message.from.first_name || '';\n  const lastName = message.from.last_name || '';\n  userName = firstName;\n  if (lastName) {\n    userName += ` ${lastName}`;\n  }\n  if (!userName) {\n    userName = 'User';\n  }\n  \n  const text = (message.text || '').toLowerCase();\n  \n  // Determine response based on message\n  if (text.includes('help')) {\n    responseText = '📚 <b>Help Menu</b>\\n\\nAvailable commands:\\n• /start - Main menu\\n• /help - This help\\n• /rate - Rate features\\n\\nUse the buttons below!';\n    keyboard = [\n      [{ text: '🔙 Back to Main', callback_data: 'main' }]\n    ];\n  } else if (text.includes('settings')) {\n    responseText = '⚙️ <b>Settings Menu</b>\\n\\nConfigure your preferences:';\n    keyboard = [\n      [{ text: '🔔 Notifications', callback_data: 'notif' }],\n      [{ text: '🌍 Language', callback_data: 'lang' }],\n      [{ text: '👤 Profile', callback_data: 'profile' }],\n      [{ text: '🔙 Back to Main', callback_data: 'main' }]\n    ];\n  } else {\n    // Default main menu\n    responseText = `Welcome ${userName}! 👋\\n\\nI'm your assistant bot. Please select an option:`;\n    keyboard = [\n      [\n        { text: '🎯 Feature 1', callback_data: 'feature1' },\n        { text: '🚀 Feature 2', callback_data: 'feature2' }\n      ],\n      [\n        { text: '⚙️ Settings', callback_data: 'settings' },\n        { text: '❓ Help', callback_data: 'help' }\n      ],\n      [\n        { text: '📊 Statistics', callback_data: 'stats' },\n        { text: '💬 Feedback', callback_data: 'feedback' }\n      ]\n    ];\n  }\n  \n} else if (data.callback_query) {\n  // Handle callback query\n  const callback = data.callback_query;\n  chatId = callback.message.chat.id;\n  messageId = callback.message.message_id;\n  \n  // Extract user's actual name from callback query\n  const firstName = callback.from.first_name || '';\n  const lastName = callback.from.last_name || '';\n  userName = firstName;\n  if (lastName) {\n    userName += ` ${lastName}`;\n  }\n  if (!userName) {\n    userName = 'User';\n  }\n  \n  isCallback = true;\n  apiMethod = 'editMessageText';\n  \n  const action = callback.data;\n  \n  switch(action) {\n    case 'feature1':\n      responseText = `🎯 <b>Feature 1 - Rating System Demo</b>\\n\\n${userName}, you've activated Feature 1!\\n\\nHow would you rate this feature?\\n\\nPlease select a rating from 1 to 5 stars:`;\n      keyboard = [\n        [\n          { text: '⭐', callback_data: 'rate_1' },\n          { text: '⭐⭐', callback_data: 'rate_2' },\n          { text: '⭐⭐⭐', callback_data: 'rate_3' },\n          { text: '⭐⭐⭐⭐', callback_data: 'rate_4' },\n          { text: '⭐⭐⭐⭐⭐', callback_data: 'rate_5' }\n        ],\n        [\n          { text: '💭 Skip Rating', callback_data: 'skip_rating' },\n          { text: '🔙 Back to Main', callback_data: 'main' }\n        ]\n      ];\n      break;\n      \n    case 'rate_1':\n      responseText = `😔 <b>Thank you for your feedback!</b>\\n\\n${userName}, you rated Feature 1: ⭐ (1 star)\\n\\nWe're sorry to hear you didn't have a great experience. Your feedback helps us improve!\\n\\nWould you like to provide additional feedback?`;\n      keyboard = [\n        [{ text: '💬 Write Feedback', callback_data: 'write_feedback' }],\n        [{ text: '🔙 Back to Main', callback_data: 'main' }]\n      ];\n      break;\n      \n    case 'rate_2':\n      responseText = `😐 <b>Thank you for your feedback!</b>\\n\\n${userName}, you rated Feature 1: ⭐⭐ (2 stars)\\n\\nWe appreciate your honest feedback. We'll work on making this better!\\n\\nWould you like to tell us more?`;\n      keyboard = [\n        [{ text: '💬 Write Feedback', callback_data: 'write_feedback' }],\n        [{ text: '🔙 Back to Main', callback_data: 'main' }]\n      ];\n      break;\n      \n    case 'rate_3':\n      responseText = `🙂 <b>Thank you for your feedback!</b>\\n\\n${userName}, you rated Feature 1: ⭐⭐⭐ (3 stars)\\n\\nGood to know! We're always looking to improve.\\n\\nAnything specific you'd like to share?`;\n      keyboard = [\n        [{ text: '💬 Share Thoughts', callback_data: 'write_feedback' }],\n        [{ text: '🔙 Back to Main', callback_data: 'main' }]\n      ];\n      break;\n      \n    case 'rate_4':\n      responseText = `😊 <b>Thank you for your feedback!</b>\\n\\n${userName}, you rated Feature 1: ⭐⭐⭐⭐ (4 stars)\\n\\nGreat! We're glad you enjoyed it!\\n\\nWhat could make it perfect?`;\n      keyboard = [\n        [{ text: '💡 Suggest Improvement', callback_data: 'write_feedback' }],\n        [{ text: '🔙 Back to Main', callback_data: 'main' }]\n      ];\n      break;\n      \n    case 'rate_5':\n      responseText = `🎉 <b>Thank you for your feedback!</b>\\n\\n${userName}, you rated Feature 1: ⭐⭐⭐⭐⭐ (5 stars)\\n\\nAmazing! We're thrilled you love it!\\n\\nWould you like to explore more features?`;\n      keyboard = [\n        [{ text: '🚀 Try Feature 2', callback_data: 'feature2' }],\n        [{ text: '📊 View Statistics', callback_data: 'stats' }],\n        [{ text: '🔙 Back to Main', callback_data: 'main' }]\n      ];\n      break;\n      \n    case 'skip_rating':\n      responseText = `No problem, ${userName}! You can rate features anytime.\\n\\nWhat would you like to do next?`;\n      keyboard = [\n        [\n          { text: '🚀 Try Feature 2', callback_data: 'feature2' },\n          { text: '📊 Statistics', callback_data: 'stats' }\n        ],\n        [{ text: '🔙 Back to Main', callback_data: 'main' }]\n      ];\n      break;\n      \n    case 'write_feedback':\n      responseText = `💬 <b>Feedback Form</b>\\n\\n${userName}, please type your feedback message and send it as a regular message.\\n\\nOr choose an option below:`;\n      keyboard = [\n        [{ text: '🐛 Report Bug', callback_data: 'bug_report' }],\n        [{ text: '💡 Suggest Feature', callback_data: 'feature_request' }],\n        [{ text: '🔙 Back to Main', callback_data: 'main' }]\n      ];\n      break;\n      \n    case 'feature2':\n      responseText = `🚀 <b>Feature 2 - Advanced Options</b>\\n\\n${userName}, welcome to Feature 2!\\n\\nThis feature includes:\\n• Data Analytics\\n• Custom Reports\\n• Export Options\\n\\nWhat would you like to do?`;\n      keyboard = [\n        [{ text: '📈 View Analytics', callback_data: 'analytics' }],\n        [{ text: '📄 Generate Report', callback_data: 'report' }],\n        [{ text: '⭐ Rate This Feature', callback_data: 'rate_feature2' }],\n        [{ text: '🔙 Back to Main', callback_data: 'main' }]\n      ];\n      break;\n      \n    case 'rate_feature2':\n      responseText = `🚀 <b>Rate Feature 2</b>\\n\\n${userName}, how would you rate Feature 2?\\n\\nSelect your rating:`;\n      keyboard = [\n        [\n          { text: '1️⃣', callback_data: 'f2_rate_1' },\n          { text: '2️⃣', callback_data: 'f2_rate_2' },\n          { text: '3️⃣', callback_data: 'f2_rate_3' },\n          { text: '4️⃣', callback_data: 'f2_rate_4' },\n          { text: '5️⃣', callback_data: 'f2_rate_5' }\n        ],\n        [{ text: '🔙 Back', callback_data: 'feature2' }]\n      ];\n      break;\n      \n    case 'profile':\n      responseText = `👤 <b>User Profile</b>\\n\\nName: ${userName}\\nUser ID: ${callback.from.id}\\nLanguage: ${callback.from.language_code || 'Not set'}\\n\\nProfile Settings:`;\n      keyboard = [\n        [{ text: '✏️ Edit Name', callback_data: 'edit_name' }],\n        [{ text: '🌍 Change Language', callback_data: 'lang' }],\n        [{ text: '🔙 Back to Settings', callback_data: 'settings' }]\n      ];\n      break;\n      \n    case 'stats':\n      responseText = `📊 <b>Your Statistics</b>\\n\\n${userName}, here are your usage stats:\\n\\n• Features Used: 2\\n• Ratings Given: 1\\n• Active Days: 3\\n• Total Actions: 15\\n\\nKeep exploring to unlock achievements!`;\n      keyboard = [\n        [{ text: '🏆 View Achievements', callback_data: 'achievements' }],\n        [{ text: '📈 Detailed Stats', callback_data: 'detailed_stats' }],\n        [{ text: '🔙 Back to Main', callback_data: 'main' }]\n      ];\n      break;\n      \n    case 'feedback':\n      responseText = `💬 <b>Feedback Center</b>\\n\\n${userName}, we value your input!\\n\\nHow can we help you today?`;\n      keyboard = [\n        [{ text: '⭐ Rate Features', callback_data: 'rate_all' }],\n        [{ text: '💡 Suggest Ideas', callback_data: 'feature_request' }],\n        [{ text: '🐛 Report Issues', callback_data: 'bug_report' }],\n        [{ text: '🔙 Back to Main', callback_data: 'main' }]\n      ];\n      break;\n      \n    case 'help':\n      responseText = `📚 <b>Help Menu</b>\\n\\n${userName}, here's how to use this bot:\\n\\n• Click buttons to navigate\\n• Rate features with 1-5 stars\\n• Submit feedback anytime\\n• Check your stats regularly\\n\\nNeed assistance?`;\n      keyboard = [\n        [{ text: '📖 User Guide', callback_data: 'guide' }],\n        [{ text: '❓ FAQ', callback_data: 'faq' }],\n        [{ text: '🔙 Back to Main', callback_data: 'main' }]\n      ];\n      break;\n      \n    case 'settings':\n      responseText = `⚙️ <b>Settings Menu</b>\\n\\n${userName}, configure your preferences:`;\n      keyboard = [\n        [{ text: '🔔 Notifications', callback_data: 'notif' }],\n        [{ text: '🌍 Language', callback_data: 'lang' }],\n        [{ text: '👤 Profile', callback_data: 'profile' }],\n        [{ text: '🔐 Privacy', callback_data: 'privacy' }],\n        [{ text: '🔙 Back to Main', callback_data: 'main' }]\n      ];\n      break;\n      \n    case 'notif':\n      responseText = '🔔 Notification settings updated successfully!';\n      keyboard = [\n        [{ text: '🔙 Back to Settings', callback_data: 'settings' }]\n      ];\n      break;\n      \n    case 'lang':\n      responseText = '🌍 Select your preferred language:';\n      keyboard = [\n        [{ text: '🇬🇧 English', callback_data: 'lang_en' }],\n        [{ text: '🇪🇸 Español', callback_data: 'lang_es' }],\n        [{ text: '🇫🇷 Français', callback_data: 'lang_fr' }],\n        [{ text: '🔙 Back to Settings', callback_data: 'settings' }]\n      ];\n      break;\n      \n    case 'main':\n    default:\n      responseText = `Welcome back ${userName}! 👋\\n\\nWhat would you like to do?`;\n      keyboard = [\n        [\n          { text: '🎯 Feature 1', callback_data: 'feature1' },\n          { text: '🚀 Feature 2', callback_data: 'feature2' }\n        ],\n        [\n          { text: '⚙️ Settings', callback_data: 'settings' },\n          { text: '❓ Help', callback_data: 'help' }\n        ],\n        [\n          { text: '📊 Statistics', callback_data: 'stats' },\n          { text: '💬 Feedback', callback_data: 'feedback' }\n        ]\n      ];\n      break;\n  }\n}\n\n// Build the request body based on message type\nlet requestBody;\nif (isCallback) {\n  requestBody = {\n    chat_id: chatId,\n    message_id: messageId,\n    text: responseText,\n    parse_mode: 'HTML',\n    reply_markup: {\n      inline_keyboard: keyboard\n    }\n  };\n} else {\n  requestBody = {\n    chat_id: chatId,\n    text: responseText,\n    parse_mode: 'HTML',\n    reply_markup: {\n      inline_keyboard: keyboard\n    }\n  };\n}\n\n// Return data for HTTP Request node\nreturn [{\n  json: {\n    apiMethod: apiMethod,\n    requestBody: requestBody,\n    isCallback: isCallback,\n    callbackQueryId: data.callback_query ? data.callback_query.id : null\n  }\n}];"
      },
      "typeVersion": 1
    },
    {
      "id": "fa65924c-ea1b-447a-a8f0-16f73902882e",
      "name": "设置机器人令牌 API 密钥",
      "type": "n8n-nodes-base.set",
      "position": [
        1296,
        3488
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "bot-token",
              "name": "botToken",
              "type": "string",
              "value": "PLACE YOUR TELEGRAM API KEY HERE "
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "a3eeae5e-1ae9-46c8-b881-7dc38200560b",
      "name": "是回调吗?",
      "type": "n8n-nodes-base.if",
      "position": [
        1696,
        3488
      ],
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.isCallback }}",
              "value2": true
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "0db5584d-9ad1-46af-bff7-bca3bf305f16",
      "name": "发送到 Telegram API",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1968,
        3120
      ],
      "parameters": {
        "url": "=https://api.telegram.org/bot{{ $json.botToken }}/{{ $json.apiMethod }}",
        "method": "POST",
        "options": {},
        "jsonBody": "={{ JSON.stringify($json.requestBody) }}",
        "sendBody": true,
        "specifyBody": "json"
      },
      "typeVersion": 4.2
    },
    {
      "id": "2aa00c9c-a450-42b5-bc39-96a5fda4bc21",
      "name": "应答回调",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1968,
        3568
      ],
      "parameters": {
        "url": "=https://api.telegram.org/bot{{ $('Set Bot Token API KEY').item.json.botToken }}/answerCallbackQuery",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"callback_query_id\": \"{{ $('Prepare Magic Response').item.json.callbackQueryId }}\",\n  \"text\": \"✅\"\n}",
        "sendBody": true,
        "specifyBody": "json"
      },
      "typeVersion": 4.2
    }
  ],
  "pinData": {},
  "connections": {
    "Is CALLBACK?": {
      "main": [
        [
          {
            "node": "Send to Telegram API",
            "type": "main",
            "index": 0
          },
          {
            "node": "Answer Callback",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send to Telegram API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "TG Trigger node": {
      "main": [
        [
          {
            "node": "Prepare Magic Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Bot Token API KEY": {
      "main": [
        [
          {
            "node": "Is CALLBACK?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Magic Response": {
      "main": [
        [
          {
            "node": "Set Bot Token API KEY",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级 - 客服机器人, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量12
分类2
节点类型6
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Ruslan Elishev

Ruslan Elishev

@relishev

Professional Product Director that cought vibe of Automaion and Solo Programming and Context Aware AI Vibe coding.

外部链接
在 n8n.io 查看

分享此工作流