{
  "name": "Telegram DM (Direct Messages to Users)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "send-telegram-dm",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "node-webhook",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "cfg-bot-token",
              "name": "telegram_bot_token",
              "value": "YOUR_TELEGRAM_BOT_TOKEN",
              "type": "string"
            },
            {
              "id": "cfg-secret",
              "name": "webhook_secret",
              "value": "YOUR_WEBHOOK_SECRET",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "node-config",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        224,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const secret = $('Webhook').item.json.headers['x-n8n-webhook-secret'];\nconst expected = $('Config').item.json.webhook_secret;\n\nif (secret !== expected) {\n  throw new Error('Invalid webhook secret');\n}\n\nconst body = $('Webhook').item.json.body;\nconst { chatId, content } = body;\n\nif (!chatId) throw new Error('Missing required field: chatId');\nif (!content) throw new Error('Missing required field: content');\n\nreturn { json: { chatId, content } };"
      },
      "id": "node-validate",
      "name": "Validate",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        448,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://api.telegram.org/bot{{ $('Config').item.json.telegram_bot_token }}/sendMessage",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ JSON.stringify({ chat_id: $json.chatId, text: $json.content, parse_mode: 'HTML' }) }}",
        "options": {
          "response": {
            "response": {
              "neverError": true
            }
          }
        }
      },
      "id": "node-send-dm",
      "name": "Send DM",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        672,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const result = $input.item.json;\nconst ok = result.ok === true;\n\nreturn {\n  json: {\n    success: ok,\n    messageId: ok ? result.result?.message_id : null,\n    error: ok ? null : (result.description || 'Telegram API error')\n  }\n};"
      },
      "id": "node-response",
      "name": "Format Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        0
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config": {
      "main": [
        [
          {
            "node": "Validate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate": {
      "main": [
        [
          {
            "node": "Send DM",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send DM": {
      "main": [
        [
          {
            "node": "Format Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [
    {
      "name": "Telegram"
    },
    {
      "name": "DM"
    }
  ]
}