Moonwalk Projects logoMoonwalk Projects
Back to templates

n8n workflow template

Telegram Bot Pause/Resume Control

Listens for /pause and /resume commands in Telegram, looks up (or creates) the matching customer record in Supabase by phone number, and flips an agent_paused flag the main WhatsApp agent checks before responding.

Intermediate20-30 minutesn8nTelegramSupabase
01

What this workflow handles

  • One-tap human takeover of an AI conversation
  • Auto-resume safety net if a human forgets to unpause
02

Setup steps

  1. 1Create a Telegram bot via @BotFather and add its token as an n8n credential
  2. 2Run the companion SQL to add agent_paused / paused_at columns
  3. 3Message /pause <phone> or /resume <phone> to test
  4. 4Keep the bot private — restrict access to trusted operators only
03

Values to replace

Review every placeholder below before activating this workflow. Public downloads should never include live credentials or client data.

  • Supabase project URL
  • Telegram bot token/credential
  • Example phone number format

Workflow JSON preview

Review before downloading or importing.

{
  "name": "Bot Pause Control - Telegram",
  "nodes": [
    {
      "parameters": {
        "content": "## Setup notes\n\n1. Create a Telegram bot via @BotFather in Telegram (takes ~2 minutes), then add its token as a Telegram credential in n8n and assign it on the **Telegram Trigger** and both **Send** nodes below.\n2. Message your bot `/pause 18761234567` or `/resume 18761234567` (with whatever country code format matches your `customers.session_key` values exactly).\n3. Run the companion SQL file in Supabase first -- this workflow depends on the `agent_paused` / `paused_at` columns it adds.\n4. Double-check the **Update Customer Pause State** node's field mappings after import -- Supabase node parameters occasionally need a dropdown re-selected even when the logic is correct.\n5. This is a private control channel -- don't share the bot with anyone besides whoever should be allowed to pause/resume the AI.",
        "height": 432,
        "width": 460
      },
      "id": "e606c168-b7f6-41ec-b045-3dcd69e454ee",
      "name": "Setup Notes",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "id": "048a014e-3b2e-4f63-a6af-b0471251f5de",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.1,
      "position": [
        272,
        240
      ],
      "webhookId": "d75ce98e-9667-4a99-a67d-b1bc5768c585",
      "credentials": {
        "telegramApi": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\n\nreturn items.map(item => {\n  const text = (item.json.message && item.json.message.text) || '';\n  const match = text.trim().match(/^\\/(pause|resume)\\s+(\\+?\\d{7,15})$/i);\n  const chatId = item.json.message ? item.json.message.chat.id : null;\n\n  if (!match) {\n    return {\n      json: {\n        valid: false,\n        chat_id: chatId,\n        raw_text: text\n      }\n    };\n  }\n\n  const command = match[1].toLowerCase();\n  const phone = match[2].replace(/^\\+/, '');\n\n  return {\n    json: {\n      valid: true,\n      command,\n      phone,\n      chat_id: chatId,\n      target_paused: command === 'pause',\n      target_paused_at: command === 'pause' ? new Date().toISOString() : null\n    }\n  };\n});"
      },
      "id": "7358c2c3-57d9-4663-ae9d-c536c9f7b2d0",
      "name": "Parse Command",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        512,
        240
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 1
          },
          "conditions": [
            {
              "id": "cond-valid",
              "leftValue": "={{ $json.valid }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "683aebcb-a4be-4994-a8c9-1990ee1f117d",
      "name": "Valid Command?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        752,
        240
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $json.chat_id }}",
        "text": "Sorry, I didn't understand that. Use:\n/pause <phone number>\n/resume <phone number>\n\nExample: /pause 18761234567",
        "additionalFields": {}
      },
      "id": "35f94cb1-ab15-475a-b437-a8667d52ff23",
      "name": "Send Usage Help",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1024,
        448
      ],
      "webhookId": "ea8884ba-e8c0-4efc-84dc-f13316cb3742",
      "credentials": {
        "telegramApi": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "tableId": "customers",
        "filters": {
          "conditions": [
            {
              "keyName": "session_key",
              "condition": "eq",
              "keyValue": "={{ $json.session_key }}"
            }
          ]
        },
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "agent_paused",
              "fieldValue": "={{ $('Valid Command?').item.json.target_paused }}"
            },
            {
              "fieldId": "paused_at",
              "fieldValue": "={{ $('Valid Command?').item.json.target_paused_at }}"
            }
          ]
        }
      },
      "id": "d54dad7b-003e-41ee-9b15-5fcd6364a350",
      "name": "Update Customer Pause State",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        1632,
        192
      ],
      "alwaysOutputData": true,
      "retryOnFail": true,
      "maxTries": 2,
      "waitBetweenTries": 1000,
      "credentials": {
        "supabaseApi": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Supabase account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 1
          },
          "conditions": [
            {
              "id": "cond-found",
              "leftValue": "={{ $json.session_key }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "85fdf1d1-c3d2-4d2d-a595-2c4317269218",
      "name": "Customer Found?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1872,
        192
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $('Parse Command').item.json.chat_id }}",
        "text": "={{ $('Parse Command').item.json.command === 'pause' ? '⏸️ Paused the AI for ' : '▶️ Resumed the AI for ' }}{{ $('Parse Command').item.json.phone }}\n\nThe AI Agent has been pause. Use \"/resume number\" to resume agent or \"/pause number\" to pause the Agent for another number.",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "id": "4bae2a7a-9793-4004-b401-ee5f3eb3bc84",
      "name": "Send Success Reply",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        2112,
        112
      ],
      "webhookId": "19abfdf3-f058-4e44-a14a-0211d73eed73",
      "credentials": {
        "telegramApi": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Parse Command').item.json.chat_id }}",
        "text": "={{ '❌ No customer found with number ' + $('Parse Command').item.json.phone + '. Double check the number matches what\\'s stored in Supabase.' }} make sure to use number format e.g. 18761234567",
        "additionalFields": {}
      },
      "id": "323ee872-64d8-450b-9200-bbe078dc18ff",
      "name": "Send Not Found Reply",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        2112,
        304
      ],
      "webhookId": "2ed6b10e-c486-40d0-9958-c6c93762e0f4",
      "credentials": {
        "telegramApi": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "LINK TO YOUR SUPABASE TABLE HERE",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "on_conflict",
              "value": "session_key"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Prefer",
              "value": "resolution=merge-duplicates,return=representation"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"session_key\": \"{{ $('Valid Command?').item.json.phone }}\",\n  \"channel\": \"whatsapp\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.5,
      "position": [
        1248,
        288
      ],
      "id": "e7fd8a6a-a210-4db2-8057-c26e74edde26",
      "name": "create customer in supabase",
      "credentials": {
        "supabaseApi": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Supabase account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "0af0356f-fff9-49b2-b42b-132330e71ed0",
              "leftValue": "={{ $json.session_key }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        1248,
        80
      ],
      "id": "790a2ed0-7080-42ec-b282-f1abba0f5e9d",
      "name": "If"
    },
    {
      "parameters": {
        "operation": "getAll",
        "tableId": "customers",
        "limit": 1,
        "filters": {
          "conditions": [
            {
              "keyName": "session_key",
              "condition": "eq",
              "keyValue": "={{ $json.phone }}"
            }
          ]
        }
      },
      "id": "bb6579b9-930c-4589-a832-bd7d9b998840",
      "name": "check if customer exist",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        1024,
        80
      ],
      "alwaysOutputData": true,
      "retryOnFail": true,
      "maxTries": 2,
      "waitBetweenTries": 1000,
      "credentials": {
        "supabaseApi": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Supabase account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Parse Command",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Command": {
      "main": [
        [
          {
            "node": "Valid Command?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Valid Command?": {
      "main": [
        [
          {
            "node": "check if customer exist",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Usage Help",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Customer Pause State": {
      "main": [
        [
          {
            "node": "Customer Found?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Customer Found?": {
      "main": [
        [
          {
            "node": "Send Success Reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Not Found Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Update Customer Pause State",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "create customer in supabase",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "check if customer exist": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "create customer in supabase": {
      "main": [
        [
          {
            "node": "Update Customer Pause State",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "timeSavedMode": "fixed",
    "saveDataErrorExecution": "all",
    "callerPolicy": "workflowsFromSameOwner",
    "availableInMCP": false
  },
  "versionId": "78f1027c-5292-41d2-98b1-959423321856",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodeGroups": [],
  "id": "IQMAM8paxJoZykS5",
  "tags": []
}