{
  "name": "Account Linking",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "id": "telegram-trigger",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1,
      "position": [
        240,
        300
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "start-command",
              "leftValue": "={{ $json.message.text }}",
              "rightValue": "/start",
              "operator": {
                "type": "string",
                "operation": "startsWith"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "check-start-command",
      "name": "Check Start Command",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "connection-code",
              "name": "connectionCode",
              "value": "={{ $json.message.text.split(' ')[1] || '' }}",
              "type": "string"
            },
            {
              "id": "chat-id",
              "name": "chatId",
              "value": "={{ $json.message.chat.id }}",
              "type": "string"
            },
            {
              "id": "username",
              "name": "username",
              "value": "={{ $json.message.from.username || '' }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "extract-connection-data",
      "name": "Extract Connection Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.3,
      "position": [
        680,
        200
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "has-connection-code",
              "leftValue": "={{ $json.connectionCode }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEqual"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "check-connection-code",
      "name": "Check Connection Code",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        900,
        200
      ]
    },
    {
      "parameters": {
        "url": "={{ $env.APP_URL }}/api/telegram/link",
        "options": {
          "headers": {
            "Content-Type": "application/json"
          }
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "userId",
              "value": "={{ $json.connectionCode }}"
            },
            {
              "name": "chatId",
              "value": "={{ $json.chatId }}"
            },
            {
              "name": "username",
              "value": "={{ $json.username }}"
            }
          ]
        }
      },
      "id": "link-account",
      "name": "Link Account",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        100
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $json.chatId }}",
        "text": "\u2705 Account linked successfully!\n\nYou will now receive:\n\u2022 Booking notifications\n\u2022 Contact updates\n\u2022 Meeting reminders\n\nSend /help to see available commands.",
        "additionalFields": {}
      },
      "id": "send-success-message",
      "name": "Send Success Message",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        1340,
        100
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $json.chatId }}",
        "text": "\u274c Failed to link account.\n\nPlease check your connection code and try again.\n\nTo get your connection code:\n1. Go to your SimpleSchedule account settings\n2. Look for the Telegram integration section\n3. Use the connection code shown there",
        "additionalFields": {}
      },
      "id": "send-error-message",
      "name": "Send Error Message",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        1340,
        200
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $json.chatId }}",
        "text": "\ud83d\udd17 Welcome to SimpleSchedule Bot!\n\nTo link your account:\n1. Go to your SimpleSchedule account settings\n2. Find the Telegram integration section\n3. Copy your connection code\n4. Send: /start YOUR_CONNECTION_CODE\n\nExample: /start 12345",
        "additionalFields": {}
      },
      "id": "send-welcome-message",
      "name": "Send Welcome Message",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        1120,
        300
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $json.message.chat.id }}",
        "text": "\u2753 This bot is for SimpleSchedule users only.\n\nTo use this bot, you need a SimpleSchedule account.\n\nVisit https://your-app-url.com to sign up!",
        "additionalFields": {}
      },
      "id": "send-not-start-message",
      "name": "Send Not Start Message",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        680,
        400
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Check Start Command",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Start Command": {
      "main": [
        [
          {
            "node": "Extract Connection Data",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Not Start Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Connection Data": {
      "main": [
        [
          {
            "node": "Check Connection Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Connection Code": {
      "main": [
        [
          {
            "node": "Link Account",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Welcome Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Link Account": {
      "main": [
        [
          {
            "node": "Send Success Message",
            "type": "main",
            "index": 0
          },
          {
            "node": "Send Error Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 1,
  "updatedAt": "2024-01-15T10:00:00.000Z",
  "versionId": "1"
}