{
  "name": "SoftGlaze: Telegram /start command (Beta)",
  "nodes": [
    {
      "parameters": {
        "content": "## Start a SoftGlaze profile from Telegram (Beta)\n\nSend `/start US-ADS-01` to your bot and it starts that profile and replies.\n\n1. Create a Telegram bot with @BotFather and add its token as a **Telegram API** credential.\n2. Put your own chat id in **Restrict to Chat IDs** on the trigger and in `ALLOWED_CHAT_ID` in **Check the command** (replace `CHANGE_ME`). Anyone else is ignored.\n3. In SoftGlaze, create an **automation token** (Owner): Operate scope, limited to the groups you want to start from Telegram, with **start_profile** pre-approved. Use it in both SoftGlaze nodes. Change `8080` if your port is different.\n\nWithout the pre-approval, each start waits for your Allow in SoftGlaze. The Telegram trigger needs n8n to be reachable by Telegram over HTTPS (a webhook).\n\nBeta: not yet run against a live SoftGlaze. Guide: browser.softglaze.com/docs/ai-agents#n8n",
        "height": 360,
        "width": 540
      },
      "id": "9ae8fdba-46dd-44ad-b313-57c4bd284aaa",
      "name": "Read me",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -60,
        -380
      ]
    },
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {
          "chatIds": "CHANGE_ME"
        }
      },
      "id": "bd86b57b-8b59-4c36-9130-e5178f903e8c",
      "name": "Telegram message",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.1,
      "position": [
        0,
        0
      ],
      "webhookId": "8f63956d-b704-437f-8143-500822c8f225"
    },
    {
      "parameters": {
        "jsCode": "// Only your own chat may start profiles. Set ALLOWED_CHAT_ID to your Telegram chat id\n// (also set it under Restrict to Chat IDs on the trigger). Expects \"/start PROFILE-NAME\".\nconst ALLOWED_CHAT_ID = 'CHANGE_ME';\nconst out = [];\nfor (const item of $input.all()) {\n  const msg = item.json.message || {};\n  const chatId = msg.chat ? String(msg.chat.id) : '';\n  if (!chatId || chatId !== ALLOWED_CHAT_ID) continue;\n  const m = /^\\/start(?:@\\w+)?\\s+(.+)$/i.exec(String(msg.text || '').trim());\n  if (!m) continue;\n  out.push({ json: { chatId, profileName: m[1].trim() } });\n}\nreturn out;"
      },
      "id": "072859a2-991c-4d50-adcc-f48c057f5b0f",
      "name": "Check the command",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://127.0.0.1:8080/api/v1/tools/list_profiles",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ search: $json.profileName, pageSize: 25 }) }}",
        "options": {
          "timeout": 130000
        }
      },
      "id": "2821c6b9-c54e-4489-8f5b-bf8223e9cfca",
      "name": "Find the profile",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// The profile whose name matches exactly (ignoring case), from the search above.\nconst asked = $('Check the command').first().json;\nconst profiles = ($input.first().json.data || {}).profiles || [];\nconst hit = profiles.find((p) => String(p.name).toLowerCase() === asked.profileName.toLowerCase());\nreturn [{ json: { chatId: asked.chatId, profileName: asked.profileName, found: Boolean(hit), profileId: hit ? hit.id : null, running: hit ? hit.running : false } }];"
      },
      "id": "5060b434-1a93-4b61-8d74-02f9d039b237",
      "name": "Pick the exact match",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "67455bb0-6fc0-46ac-ac9d-a43ca9283937",
              "leftValue": "={{ $json.found }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "2fcbbc31-28f0-40f7-ae29-966cdd6d9926",
      "name": "Found it?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        880,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://127.0.0.1:8080/api/v1/tools/start_profile",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ id: $json.profileId }) }}",
        "options": {
          "timeout": 130000,
          "response": {
            "response": {
              "neverError": true
            }
          }
        }
      },
      "id": "759e8c4a-0cce-4365-9b81-b834c097c4f7",
      "name": "Start the profile",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        -100
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $('Pick the exact match').first().json.chatId }}",
        "text": "={{ $json.ok ? ($json.data && $json.data.alreadyRunning ? $('Pick the exact match').first().json.profileName + ' is already running.' : 'Started ' + $('Pick the exact match').first().json.profileName + '.') : 'SoftGlaze did not start it: ' + ($json.message || $json.error || 'unknown error') }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "id": "c1e2b83c-1cba-4ff1-9158-9c00c1657b33",
      "name": "Reply with the result",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1320,
        -100
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $json.chatId }}",
        "text": "={{ 'No profile is named ' + $json.profileName + '. Check the name in SoftGlaze.' }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "id": "27b5bb64-5008-43c6-93cf-8d78ab4baa80",
      "name": "Reply: not found",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1100,
        120
      ]
    }
  ],
  "connections": {
    "Telegram message": {
      "main": [
        [
          {
            "node": "Check the command",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check the command": {
      "main": [
        [
          {
            "node": "Find the profile",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find the profile": {
      "main": [
        [
          {
            "node": "Pick the exact match",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pick the exact match": {
      "main": [
        [
          {
            "node": "Found it?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Found it?": {
      "main": [
        [
          {
            "node": "Start the profile",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Reply: not found",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start the profile": {
      "main": [
        [
          {
            "node": "Reply with the result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
