{
  "name": "Northwind Studio bot (with a free-text question)",
  "nodes": [
    {
      "id": "06ca781f-98ac-4fc6-a67e-bf3c4e6a3bd5",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.5,
      "position": [
        -240,
        300
      ],
      "parameters": {
        "updates": [
          "message",
          "callback_query"
        ],
        "additionalFields": {}
      }
    },
    {
      "id": "ec37f13f-0ee9-4cca-a24d-e6191640c596",
      "name": "Load conversation state",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        -20,
        300
      ],
      "parameters": {
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "filters": {
          "conditions": [
            {
              "keyName": "chat_id",
              "condition": "eq",
              "keyValue": "={{ $json.message?.chat.id ?? $json.callback_query.message.chat.id }}"
            }
          ]
        },
        "limit": 1
      },
      "alwaysOutputData": true
    },
    {
      "id": "37aec29b-1941-45d7-980d-2d1f66bd54a3",
      "name": "What came in?",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.4,
      "position": [
        220,
        300
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "3e51c89f-3c6c-47af-8e9f-2495a507152e",
                    "leftValue": "={{ $('Telegram Trigger').item.json.callback_query }}",
                    "rightValue": "",
                    "operator": {
                      "type": "object",
                      "operation": "exists",
                      "singleValue": true
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Button tap"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "7deb5483-2b52-4c72-b5b2-cd1ec289e67c",
                    "leftValue": "={{ $('Telegram Trigger').item.json.message.text }}",
                    "rightValue": "/start",
                    "operator": {
                      "type": "string",
                      "operation": "startsWith"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "/start"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "ddb1acad-c87f-4b58-95ac-79784e9471da",
                    "leftValue": "={{ $('Telegram Trigger').item.json.message.text }}",
                    "rightValue": "",
                    "operator": {
                      "type": "string",
                      "operation": "exists",
                      "singleValue": true
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Text"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "29b5597d-d42e-4f7a-9568-98cf6477be87",
      "name": "Answer the tap",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        480,
        60
      ],
      "parameters": {
        "resource": "callback",
        "queryId": "={{ $('Telegram Trigger').item.json.callback_query.id }}",
        "additionalFields": {}
      }
    },
    {
      "id": "01ddb812-7370-4333-af42-1d077227f16f",
      "name": "Which button?",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.4,
      "position": [
        700,
        60
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "93f2d6f7-1088-4094-a5f5-d819a8019145",
                    "leftValue": "={{ $('Telegram Trigger').item.json.callback_query.data }}",
                    "rightValue": "ask",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Ask"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "d08f92ea-7d21-4e4c-a615-94d6f255682f",
                    "leftValue": "={{ $('Telegram Trigger').item.json.callback_query.data }}",
                    "rightValue": "quote",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Quote"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "e8a86332-0974-40ba-a1d3-45e765e5ed1d",
                    "leftValue": "={{ $('Telegram Trigger').item.json.callback_query.data }}",
                    "rightValue": "back",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Back"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "63b6e806-40f0-4566-8e2b-c3f7a6ddad46",
                    "leftValue": "={{ $('Telegram Trigger').item.json.callback_query.data }}",
                    "rightValue": "svc_",
                    "operator": {
                      "type": "string",
                      "operation": "startsWith"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Service"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "bc1b6c60-e630-4da2-9ba1-1209a0e865bc",
      "name": "Invite a question",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        980,
        -260
      ],
      "parameters": {
        "operation": "editMessageText",
        "chatId": "={{ $('Telegram Trigger').item.json.callback_query.message.chat.id }}",
        "messageId": "={{ $('Telegram Trigger').item.json.callback_query.message.message_id }}",
        "text": "Sure. Type your question and I'll answer.",
        "additionalFields": {}
      }
    },
    {
      "id": "63b7ac16-947a-4661-a0b8-b23b486f2c62",
      "name": "Show services",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        980,
        -100
      ],
      "parameters": {
        "operation": "editMessageText",
        "chatId": "={{ $('Telegram Trigger').item.json.callback_query.message.chat.id }}",
        "messageId": "={{ $('Telegram Trigger').item.json.callback_query.message.message_id }}",
        "text": "What do you need a quote for?",
        "additionalFields": {},
        "replyMarkup": "inlineKeyboard",
        "inlineKeyboard": {
          "rows": [
            {
              "row": {
                "buttons": [
                  {
                    "text": "Website",
                    "additionalFields": {
                      "callback_data": "svc_website"
                    }
                  },
                  {
                    "text": "Online store",
                    "additionalFields": {
                      "callback_data": "svc_store"
                    }
                  }
                ]
              }
            },
            {
              "row": {
                "buttons": [
                  {
                    "text": "Something else",
                    "additionalFields": {
                      "callback_data": "svc_other"
                    }
                  }
                ]
              }
            },
            {
              "row": {
                "buttons": [
                  {
                    "text": "← Back",
                    "additionalFields": {
                      "callback_data": "back"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    },
    {
      "id": "e556cb63-fc7b-4800-bbbf-ed1b17aee2ad",
      "name": "Back to main menu",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        980,
        60
      ],
      "parameters": {
        "operation": "editMessageText",
        "chatId": "={{ $('Telegram Trigger').item.json.callback_query.message.chat.id }}",
        "messageId": "={{ $('Telegram Trigger').item.json.callback_query.message.message_id }}",
        "text": "=Hi {{ $('Telegram Trigger').item.json.callback_query.from.first_name }}! I'm the Northwind Studio bot. What can I do for you?",
        "additionalFields": {},
        "replyMarkup": "inlineKeyboard",
        "inlineKeyboard": {
          "rows": [
            {
              "row": {
                "buttons": [
                  {
                    "text": "💬 Ask a question",
                    "additionalFields": {
                      "callback_data": "ask"
                    }
                  }
                ]
              }
            },
            {
              "row": {
                "buttons": [
                  {
                    "text": "📋 Get a quote",
                    "additionalFields": {
                      "callback_data": "quote"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    },
    {
      "id": "96b611f7-b51b-47a1-ae8d-8ff3e6bc8c7e",
      "name": "Remember: waiting for brief",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        980,
        220
      ],
      "parameters": {
        "operation": "upsert",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "filters": {
          "conditions": [
            {
              "keyName": "chat_id",
              "condition": "eq",
              "keyValue": "={{ $('Telegram Trigger').item.json.message?.chat.id ?? $('Telegram Trigger').item.json.callback_query.message.chat.id }}"
            }
          ]
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "chat_id": "={{ $('Telegram Trigger').item.json.message?.chat.id ?? $('Telegram Trigger').item.json.callback_query.message.chat.id }}",
            "step": "awaiting_brief",
            "service": "={{ $('Telegram Trigger').item.json.callback_query.data }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "chat_id",
              "displayName": "chat_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            },
            {
              "id": "step",
              "displayName": "step",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            },
            {
              "id": "service",
              "displayName": "service",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      }
    },
    {
      "id": "74f1c86b-a102-4052-ab58-c0fc7252044f",
      "name": "Ask for the brief",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1200,
        220
      ],
      "parameters": {
        "operation": "editMessageText",
        "chatId": "={{ $('Telegram Trigger').item.json.callback_query.message.chat.id }}",
        "messageId": "={{ $('Telegram Trigger').item.json.callback_query.message.message_id }}",
        "text": "Got it. Describe the project in a sentence or two.",
        "additionalFields": {}
      }
    },
    {
      "id": "8b5fe44a-3e06-4a09-b2da-cece24ff1b0a",
      "name": "Reset state",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        480,
        400
      ],
      "parameters": {
        "operation": "upsert",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "filters": {
          "conditions": [
            {
              "keyName": "chat_id",
              "condition": "eq",
              "keyValue": "={{ $('Telegram Trigger').item.json.message?.chat.id ?? $('Telegram Trigger').item.json.callback_query.message.chat.id }}"
            }
          ]
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "chat_id": "={{ $('Telegram Trigger').item.json.message?.chat.id ?? $('Telegram Trigger').item.json.callback_query.message.chat.id }}",
            "step": "",
            "service": ""
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "chat_id",
              "displayName": "chat_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            },
            {
              "id": "step",
              "displayName": "step",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            },
            {
              "id": "service",
              "displayName": "service",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      }
    },
    {
      "id": "310570b0-8fa8-4fcd-930b-a775a4306f2d",
      "name": "Send main menu",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        700,
        400
      ],
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "=Hi {{ $('Telegram Trigger').item.json.message.from.first_name }}! I'm the Northwind Studio bot. What can I do for you?",
        "additionalFields": {
          "appendAttribution": false
        },
        "replyMarkup": "inlineKeyboard",
        "inlineKeyboard": {
          "rows": [
            {
              "row": {
                "buttons": [
                  {
                    "text": "💬 Ask a question",
                    "additionalFields": {
                      "callback_data": "ask"
                    }
                  }
                ]
              }
            },
            {
              "row": {
                "buttons": [
                  {
                    "text": "📋 Get a quote",
                    "additionalFields": {
                      "callback_data": "quote"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    },
    {
      "id": "d69dd0d1-e46b-4af7-8aaf-67774075e805",
      "name": "Waiting for a brief?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        480,
        640
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "87053476-d349-4e62-ac7e-3ddaf2052c9b",
              "leftValue": "={{ $('Load conversation state').item.json.step }}",
              "rightValue": "awaiting_brief",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      }
    },
    {
      "id": "e454f405-9e53-4f03-a1b8-149e0a99bea2",
      "name": "Notify the team",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        760,
        560
      ],
      "parameters": {
        "chatId": "-1001234567890",
        "text": "=New quote request: {{ $('Load conversation state').item.json.service }}\nFrom: {{ $('Telegram Trigger').item.json.message.from.first_name }} (@{{ $('Telegram Trigger').item.json.message.from.username }})\nBrief: {{ $('Telegram Trigger').item.json.message.text }}",
        "additionalFields": {
          "appendAttribution": false
        }
      }
    },
    {
      "id": "c0768b94-8d0d-49b8-a84a-b03180d7c4a0",
      "name": "Clear state",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        980,
        560
      ],
      "parameters": {
        "operation": "upsert",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "filters": {
          "conditions": [
            {
              "keyName": "chat_id",
              "condition": "eq",
              "keyValue": "={{ $('Telegram Trigger').item.json.message?.chat.id ?? $('Telegram Trigger').item.json.callback_query.message.chat.id }}"
            }
          ]
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "chat_id": "={{ $('Telegram Trigger').item.json.message?.chat.id ?? $('Telegram Trigger').item.json.callback_query.message.chat.id }}",
            "step": "",
            "service": ""
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "chat_id",
              "displayName": "chat_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            },
            {
              "id": "step",
              "displayName": "step",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            },
            {
              "id": "service",
              "displayName": "service",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "readOnly": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      }
    },
    {
      "id": "2f34a47b-da9c-40e0-84be-7934c9f7af8e",
      "name": "Confirm to the customer",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1200,
        560
      ],
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "Thanks! We'll message you here within one working day.",
        "additionalFields": {
          "appendAttribution": false
        }
      }
    },
    {
      "id": "eb07464f-e784-46e5-8905-fda1cead6d0c",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        760,
        800
      ],
      "parameters": {
        "promptType": "define",
        "text": "={{ $('Telegram Trigger').item.json.message.text }}",
        "options": {
          "systemMessage": "You are the assistant of Northwind Studio, a small web design studio. Answer in two or three short sentences. If you do not know, say that a person from the team will follow up."
        }
      }
    },
    {
      "id": "f8e63329-8cbe-4b39-b0f6-0d6fe54e2c30",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        720,
        1020
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-5-mini"
        },
        "options": {}
      }
    },
    {
      "id": "f89805e1-15c7-4247-9ce4-c087a3bdc742",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.4,
      "position": [
        880,
        1020
      ],
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "contextWindowLength": 10
      }
    },
    {
      "id": "54a20d76-d68b-4466-9211-075613c7d685",
      "name": "Send the answer",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1140,
        800
      ],
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}",
        "additionalFields": {
          "appendAttribution": false
        }
      }
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Load conversation state",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Load conversation state": {
      "main": [
        [
          {
            "node": "What came in?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "What came in?": {
      "main": [
        [
          {
            "node": "Answer the tap",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Reset state",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Waiting for a brief?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reset state": {
      "main": [
        [
          {
            "node": "Send main menu",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Answer the tap": {
      "main": [
        [
          {
            "node": "Which button?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Which button?": {
      "main": [
        [
          {
            "node": "Invite a question",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Show services",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Back to main menu",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Remember: waiting for brief",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Remember: waiting for brief": {
      "main": [
        [
          {
            "node": "Ask for the brief",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Waiting for a brief?": {
      "main": [
        [
          {
            "node": "Notify the team",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Notify the team": {
      "main": [
        [
          {
            "node": "Clear state",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Clear state": {
      "main": [
        [
          {
            "node": "Confirm to the customer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Send the answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}