Play with Spotify from Telegram

工作流概述

这是一个包含14个节点的复杂工作流,主要用于自动化处理各种任务。

工作流源代码

下载
{
  "id": "F7CfIF10XjXhqbGb",
  "meta": {
    "instanceId": "ba8f1362d8ed4c2ce84171d2f481098de4ee775241bdc1660d1dce80434ec7d4",
    "templateCredsSetupCompleted": true
  },
  "name": "Play with Spotify from Telegram",
  "tags": [],
  "nodes": [
    {
      "id": "0395b3e4-94ef-49ea-9b4c-8f908e62f8c6",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -60,
        20
      ],
      "webhookId": "e7aa284b-5eef-4ac1-94bf-8e4d307a3b14",
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "gblW5oACGEPuccja",
          "name": "Telegram account"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "263edf45-58a0-45e8-91f8-601bc62c7d6f",
      "name": "OpenAI - Ask about a track",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        120,
        -120
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini",
          "cachedResultName": "GPT-4O-MINI"
        },
        "options": {},
        "messages": {
          "values": [
            {
              "content": "=get artist and song name from '{{ $json.message.text }}'. Reply only eg. 'track:song name artist:artist name'"
            }
          ]
        }
      },
      "credentials": {
        "openAiApi": {
          "id": "vDcge3EgslxfX3EC",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.6
    },
    {
      "id": "086aef8b-533a-4c33-9952-29d5adb152c8",
      "name": "Search track",
      "type": "n8n-nodes-base.spotify",
      "onError": "continueErrorOutput",
      "position": [
        540,
        -200
      ],
      "parameters": {
        "limit": 1,
        "query": "={{ $json.message.content }}",
        "filters": {},
        "resource": "track",
        "operation": "search"
      },
      "credentials": {
        "spotifyOAuth2Api": {
          "id": "wylKghFNQa8IKy1U",
          "name": "Spotify account"
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "08af6055-ba52-4cb2-a561-ea04ac55279f",
      "name": "Add song",
      "type": "n8n-nodes-base.spotify",
      "onError": "continueErrorOutput",
      "position": [
        780,
        -240
      ],
      "parameters": {
        "id": "=spotify:track:{{ $json.id }}"
      },
      "credentials": {
        "spotifyOAuth2Api": {
          "id": "wylKghFNQa8IKy1U",
          "name": "Spotify account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "2dbdafa4-3b6f-4a14-813c-4e10da10abad",
      "name": "Next Song",
      "type": "n8n-nodes-base.spotify",
      "onError": "continueErrorOutput",
      "position": [
        980,
        -280
      ],
      "parameters": {
        "operation": "nextSong"
      },
      "credentials": {
        "spotifyOAuth2Api": {
          "id": "wylKghFNQa8IKy1U",
          "name": "Spotify account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "cb8d42aa-0c7e-45a5-90b5-b91e483dd13a",
      "name": "Resume play",
      "type": "n8n-nodes-base.spotify",
      "notes": "We don't have to stop here on error. An error is thrown from Spotify if the player is already playing.",
      "onError": "continueRegularOutput",
      "position": [
        1240,
        -380
      ],
      "parameters": {
        "operation": "resume"
      },
      "credentials": {
        "spotifyOAuth2Api": {
          "id": "wylKghFNQa8IKy1U",
          "name": "Spotify account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "089e1070-b013-454c-9f6c-55b909e06c1d",
      "name": "Currently Playing",
      "type": "n8n-nodes-base.spotify",
      "onError": "continueErrorOutput",
      "position": [
        1420,
        -300
      ],
      "parameters": {
        "operation": "currentlyPlaying"
      },
      "credentials": {
        "spotifyOAuth2Api": {
          "id": "wylKghFNQa8IKy1U",
          "name": "Spotify account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "e9df0dcf-b166-45a3-910b-787b3718bbcf",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        120,
        -300
      ],
      "parameters": {
        "color": 5,
        "width": 254.05813953488382,
        "content": "## Telegram to Spotify 
Ask AI about a track with artist and song name or if you can't remember describe it and AI does it's thing.
"
      },
      "typeVersion": 1
    },
    {
      "id": "77bae9be-2d92-4028-ae78-7887b6a2d394",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        440,
        220
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineAll"
      },
      "typeVersion": 3
    },
    {
      "id": "0d95000d-7efd-402a-9a34-47ababb2f53e",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        620,
        -440
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "02af5387-07d2-4a16-bd83-e1359d091165",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json?.id }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "363f89ad-34d0-4445-8ff3-693d991dad09",
      "name": "Message parser",
      "type": "n8n-nodes-base.set",
      "position": [
        1280,
        -40
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "93cd2545-c6e9-4717-96b7-d49eb056ac70",
              "name": "message",
              "type": "string",
              "value": "={{ $json.error }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "8b80f80d-8c8e-44de-9838-6d05199bb734",
      "name": "Not found error message",
      "type": "n8n-nodes-base.set",
      "position": [
        880,
        -460
      ],
      "parameters": {
        "mode": "raw",
        "options": {},
        "jsonOutput": "{
 \"error\": \"Song not found\"
}
"
      },
      "typeVersion": 3.4
    },
    {
      "id": "f1785140-8e97-43e1-9d84-aedc8b8d5e06",
      "name": "Return message to Telegram",
      "type": "n8n-nodes-base.telegram",
      "position": [
        760,
        220
      ],
      "parameters": {
        "text": "={{ $('Message parser').item.json.message }}",
        "chatId": "={{ $json.message.chat.id }}",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "gblW5oACGEPuccja",
          "name": "Telegram account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "e3e16535-094b-41bf-88c6-166bb6805d53",
      "name": "Define Now Playing",
      "type": "n8n-nodes-base.set",
      "notes": "We use the object \"error\" as a returned bject so we can re-use the Message Parser node.",
      "position": [
        1660,
        -240
      ],
      "parameters": {
        "mode": "raw",
        "options": {},
        "jsonOutput": "={
 \"error\": \"Now playing {{ $json.item.name }} - {{ $json.item.artists[0].name }} - {{ $json.item.album.name }}\"
}
"
      },
      "typeVersion": 3.4
    }
  ],
  "active": true,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "6f219c9e-f17a-45b1-ab8d-09d991fd8e34",
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "Add song",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Not found error message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Return message to Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add song": {
      "main": [
        [
          {
            "node": "Next Song",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Message parser",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Next Song": {
      "main": [
        [
          {
            "node": "Resume play",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Message parser",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Resume play": {
      "main": [
        [
          {
            "node": "Currently Playing",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Search track": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Message parser",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Message parser": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "OpenAI - Ask about a track",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Currently Playing": {
      "main": [
        [
          {
            "node": "Define Now Playing",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Message parser",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Define Now Playing": {
      "main": [
        [
          {
            "node": "Message parser",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Not found error message": {
      "main": [
        [
          {
            "node": "Message parser",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI - Ask about a track": {
      "main": [
        [
          {
            "node": "Search track",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

功能特点

  • 自动检测新邮件
  • AI智能内容分析
  • 自定义分类规则
  • 批量处理能力
  • 详细的处理日志

技术分析

节点类型及作用

  • Telegramtrigger
  • @N8N/N8N Nodes Langchain.Openai
  • Spotify
  • Stickynote
  • Merge

复杂度评估

配置难度:
★★★★☆
维护难度:
★★☆☆☆
扩展性:
★★★★☆

实施指南

前置条件

  • 有效的Gmail账户
  • n8n平台访问权限
  • Google API凭证
  • AI分类服务订阅

配置步骤

  1. 在n8n中导入工作流JSON文件
  2. 配置Gmail节点的认证信息
  3. 设置AI分类器的API密钥
  4. 自定义分类规则和标签映射
  5. 测试工作流执行
  6. 配置定时触发器(可选)

关键参数

参数名称 默认值 说明
maxEmails 50 单次处理的最大邮件数量
confidenceThreshold 0.8 分类置信度阈值
autoLabel true 是否自动添加标签

最佳实践

优化建议

  • 定期更新AI分类模型以提高准确性
  • 根据邮件量调整处理批次大小
  • 设置合理的分类置信度阈值
  • 定期清理过期的分类规则

安全注意事项

  • 妥善保管API密钥和认证信息
  • 限制工作流的访问权限
  • 定期审查处理日志
  • 启用双因素认证保护Gmail账户

性能优化

  • 使用增量处理减少重复工作
  • 缓存频繁访问的数据
  • 并行处理多个邮件分类任务
  • 监控系统资源使用情况

故障排除

常见问题

邮件未被正确分类

检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。

Gmail认证失败

确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。

调试技巧

  • 启用详细日志记录查看每个步骤的执行情况
  • 使用测试邮件验证分类逻辑
  • 检查网络连接和API服务状态
  • 逐步执行工作流定位问题节点

错误处理

工作流包含以下错误处理机制:

  • 网络超时自动重试(最多3次)
  • API错误记录和告警
  • 处理失败邮件的隔离机制
  • 异常情况下的回滚操作