{
  "name": "WhatsApp or Telegram AI FAQ bot",
  "nodes": [
    {
      "id": "00000000-0000-4000-8000-000000000010",
      "name": "WhatsApp swap note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        40
      ],
      "parameters": {
        "content": "## WhatsApp\nThis workflow ships with a Telegram trigger and Telegram send nodes.\n\nFor WhatsApp Business Cloud, replace those two nodes and keep Normalize, the OpenAI call, the IF, and the human alert.\nMap chat id from the WhatsApp payload and send the reply with the WhatsApp node.\nDo not paste a token into the JSON.",
        "height": 220,
        "width": 320,
        "color": 4
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000011",
      "name": "Receive Telegram message",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        260,
        300
      ],
      "parameters": {
        "updates": [
          "message"
        ]
      },
      "credentials": {
        "telegramApi": {
          "id": "TELEGRAM_CREDENTIAL",
          "name": "Telegram bot"
        }
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000012",
      "name": "Normalize incoming message",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        540,
        300
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "chatId",
              "name": "chatId",
              "value": "={{ $json.message.chat.id }}",
              "type": "string"
            },
            {
              "id": "text",
              "name": "text",
              "value": "={{ $json.message.text || \"\" }}",
              "type": "string"
            },
            {
              "id": "from",
              "name": "from",
              "value": "={{ $json.message.from.username || $json.message.from.first_name || \"caller\" }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000013",
      "name": "Draft FAQ answer",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        820,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "options": {
          "timeout": 20000,
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": false
            }
          }
        },
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ model: \"gpt-4.1-mini\", temperature: 0, response_format: { type: \"json_object\" }, messages: [{ role: \"system\", content: \"You answer FAQs. Replace this paragraph with your real policy: hours, what you can promise, what you must never promise, and when to hand off to a human. Reply with JSON only: {\\\"reply\\\": string under 500 characters, \\\"handoff\\\": boolean, \\\"reason\\\": string}. If the policy does not contain the answer, set handoff true. Never ask for a password, card number, or government id.\" }, { role: \"user\", content: $json.text }] }) }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000,
      "onError": "continueErrorOutput",
      "credentials": {
        "httpHeaderAuth": {
          "id": "OPENAI_HEADER_AUTH",
          "name": "OpenAI Authorization header"
        }
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000014",
      "name": "Read answer JSON",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const message = $(\"Normalize incoming message\").item.json;\nconst content = $json.choices?.[0]?.message?.content || \"{}\";\nlet parsed = { reply: \"I could not draft an answer. A person will follow up.\", handoff: true, reason: \"Unreadable model output\" };\ntry { parsed = { ...parsed, ...JSON.parse(content) }; } catch (error) { parsed.handoff = true; }\nreturn [{ json: { ...message, ...parsed } }];"
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000015",
      "name": "Needs a human",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1380,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "Needs a human-condition",
              "leftValue": "={{ $json.handoff ? 1 : 0 }}",
              "rightValue": 1,
              "operator": {
                "type": "number",
                "operation": "gte"
              }
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000016",
      "name": "Send FAQ answer",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1660,
        460
      ],
      "parameters": {
        "chatId": "={{ $(\"Normalize incoming message\").item.json.chatId }}",
        "text": "={{ $json.reply }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "TELEGRAM_CREDENTIAL",
          "name": "Telegram bot"
        }
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000017",
      "name": "Tell the caller a person will follow up",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1660,
        160
      ],
      "parameters": {
        "chatId": "={{ $(\"Normalize incoming message\").item.json.chatId }}",
        "text": "=I need a person to take this. Someone from the team will follow up. {{ $json.reply }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "TELEGRAM_CREDENTIAL",
          "name": "Telegram bot"
        }
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000018",
      "name": "Ask a human to take over",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [
        1940,
        160
      ],
      "parameters": {
        "resource": "message",
        "operation": "post",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#support-handoff"
        },
        "text": "=FAQ handoff from {{ $json.from }}. They asked: {{ $json.text }}\\nReason: {{ $json.reason }}",
        "otherOptions": {}
      },
      "credentials": {
        "slackApi": {
          "id": "SLACK_CREDENTIAL",
          "name": "Slack account"
        }
      }
    },
    {
      "id": "00000000-0000-4000-8000-000000000019",
      "name": "Send a fallback reply",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1100,
        560
      ],
      "parameters": {
        "chatId": "={{ $(\"Normalize incoming message\").item.json.chatId }}",
        "text": "I could not answer that just now. A person on the team will follow up.",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "TELEGRAM_CREDENTIAL",
          "name": "Telegram bot"
        }
      }
    }
  ],
  "pinData": {
    "Receive Telegram message": [
      {
        "json": {
          "message": {
            "message_id": 1,
            "text": "What time do you close on Saturday?",
            "chat": {
              "id": 10001,
              "type": "private"
            },
            "from": {
              "id": 20002,
              "first_name": "Sam",
              "username": "sam_example"
            }
          }
        }
      }
    ]
  },
  "connections": {
    "Receive Telegram message": {
      "main": [
        [
          {
            "node": "Normalize incoming message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize incoming message": {
      "main": [
        [
          {
            "node": "Draft FAQ answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Draft FAQ answer": {
      "main": [
        [
          {
            "node": "Read answer JSON",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send a fallback reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read answer JSON": {
      "main": [
        [
          {
            "node": "Needs a human",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Needs a human": {
      "main": [
        [
          {
            "node": "Tell the caller a person will follow up",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send FAQ answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tell the caller a person will follow up": {
      "main": [
        [
          {
            "node": "Ask a human to take over",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "timezone": "UTC",
    "saveDataErrorExecution": "all",
    "saveDataSuccessExecution": "all",
    "saveManualExecutions": true
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
