{
  "description": "Flow Twilio Studio — Chatbot WhatsApp Welely",
  "initial_state": "Trigger",
  "states": [
    {
      "name": "Trigger",
      "type": "trigger",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "CheckBusinessHours"
        }
      ],
      "properties": {}
    },
    {
      "name": "CheckBusinessHours",
      "type": "split-based-on",
      "transitions": [
        {
          "event": "match",
          "conditions": [
            {
              "friendly_name": "WithinHours",
              "arguments": [
                {
                  "type": "string",
                  "value": "{{now().hour}}"
                }
              ],
              "expression": " {{ now().hour | int }} >= 8 and {{ now().hour | int }} < 18 "
            }
          ],
          "next": "Greeting"
        },
        {
          "event": "noMatch",
          "next": "AfterHoursMessage"
        }
      ],
      "properties": {
        "input": "{{trigger.message.SmsMessageSid}}"
      }
    },
    {
      "name": "AfterHoursMessage",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "CollectAfterHoursLead"
        }
      ],
      "properties": {
        "service": "studio",
        "to": "{{trigger.message.From}}",
        "body": "Bonjour 👋\\nMerci d’avoir contacté Welely. Nos équipes sont disponibles du lundi au vendredi de 8h à 18h.\\nLaissez votre message (nom, besoin, téléphone et email) et nous vous répondrons dès notre retour.\\nVous pouvez aussi écrire : contact@welely.com"
      }
    },
    {
      "name": "CollectAfterHoursLead",
      "type": "split-based-on",
      "transitions": [
        {
          "event": "match",
          "next": "SaveLeadWebhook"
        }
      ],
      "properties": {
        "input": "{{widgets.AfterHoursMessage.inbound.Body}}"
      }
    },
    {
      "name": "Greeting",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "ServiceMenu"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "👋 Bonjour et bienvenue chez *Welely* — le digital sur mesure, pensé pour vous.\\nJe suis *Weli*, votre assistant. Comment puis-je vous aider aujourd’hui ?\\n\\n1️⃣ Création de site web\\n2️⃣ Application mobile\\n3️⃣ Design & identité visuelle\\n4️⃣ Photographie & vidéographie\\n5️⃣ Accompagnement digital & consulting\\n6️⃣ Maintenance / Formation / SEO\\n7️⃣ Autre demande\\n\\nRépondez par le numéro correspondant."
      }
    },
    {
      "name": "ServiceMenu",
      "type": "split-based-on",
      "transitions": [
        {
          "event": "match",
          "conditions": [
            {
              "expression": "{{widgets.Greeting.inbound.Body}} == '1'",
              "friendly_name": "SiteWeb",
              "next": "SiteWeb_Submenu"
            },
            {
              "expression": "{{widgets.Greeting.inbound.Body}} == '2'",
              "friendly_name": "AppMobile",
              "next": "AppMobile_Submenu"
            },
            {
              "expression": "{{widgets.Greeting.inbound.Body}} == '3'",
              "friendly_name": "Design",
              "next": "Design_Submenu"
            },
            {
              "expression": "{{widgets.Greeting.inbound.Body}} == '4'",
              "friendly_name": "PhotoVideo",
              "next": "PhotoVideo_Submenu"
            },
            {
              "expression": "{{widgets.Greeting.inbound.Body}} == '5'",
              "friendly_name": "Accompagnement",
              "next": "Accompagnement_Submenu"
            },
            {
              "expression": "{{widgets.Greeting.inbound.Body}} == '6'",
              "friendly_name": "Maintenance",
              "next": "Maintenance_Submenu"
            },
            {
              "expression": "{{widgets.Greeting.inbound.Body}} == '7'",
              "friendly_name": "Autre",
              "next": "Other_Request"
            }
          ]
        },
        {
          "event": "noMatch",
          "next": "InvalidChoice"
        }
      ],
      "properties": {
        "input": "{{widgets.Greeting.inbound.Body}}"
      }
    },
    {
      "name": "InvalidChoice",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "Greeting"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "Désolé, je n’ai pas compris votre choix. Répondez par le numéro correspondant (1 à 7)."
      }
    },
    {
      "name": "SiteWeb_Submenu",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "Collect_Name"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "💻 *Création de site web*\\nQuel type de site souhaitez-vous ?\\n1️⃣ Site vitrine\\n2️⃣ E-commerce\\n3️⃣ Refonte de site existant\\n4️⃣ Autre"
      }
    },
    {
      "name": "AppMobile_Submenu",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "Collect_Name"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "🚀 *Application mobile*\\nQuel type d’application ?\\n1️⃣ Entreprise\\n2️⃣ E-commerce\\n3️⃣ Sur mesure\\n4️⃣ Autre"
      }
    },
    {
      "name": "Design_Submenu",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "Collect_Name"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "🎨 *Design & identité visuelle*\\nQue souhaitez-vous ?\\n1️⃣ Logo\\n2️⃣ Charte graphique\\n3️⃣ Visuels réseaux sociaux\\n4️⃣ Autre"
      }
    },
    {
      "name": "PhotoVideo_Submenu",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "Collect_Name"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "📸 *Photographie & vidéographie*\\nChoisissez :\\n1️⃣ Séance photo professionnelle\\n2️⃣ Tournage vidéo corporate\\n3️⃣ Captation d’événement\\n4️⃣ Autre"
      }
    },
    {
      "name": "Accompagnement_Submenu",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "Collect_Name"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "💡 *Accompagnement digital & consulting*\\nQue voulez-vous ?\\n1️⃣ Stratégie communication\\n2️⃣ Automatisation\\n3️⃣ Audit digital\\n4️⃣ Autre"
      }
    },
    {
      "name": "Maintenance_Submenu",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "Collect_Name"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "🔧 *Maintenance / Formation / SEO*\\nQue souhaitez-vous explorer ?\\n1️⃣ Maintenance\\n2️⃣ Formation\\n3️⃣ SEO / Référencement\\n4️⃣ Autre"
      }
    },
    {
      "name": "Other_Request",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "Collect_Name"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "Merci. Décrivez en quelques mots votre besoin (ex : site + logo, devis, question technique...)."
      }
    },
    {
      "name": "Collect_Name",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "SaveName"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "Super ✅\\nPour commencer, quel est votre *nom* (ou le nom de l’entreprise) ?"
      }
    },
    {
      "name": "SaveName",
      "type": "set-variables",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "Collect_Telephone"
        }
      ],
      "properties": {
        "variables": [
          {
            "key": "lead_name",
            "value": "{{widgets.Collect_Name.inbound.Body}}"
          },
          {
            "key": "lead_service_choice",
            "value": "{{widgets.ServiceMenu.input}}"
          }
        ]
      }
    },
    {
      "name": "Collect_Telephone",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "SaveTelephone"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "Merci. Quel est votre *téléphone* (format international si possible) ?"
      }
    },
    {
      "name": "SaveTelephone",
      "type": "set-variables",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "Collect_Email"
        }
      ],
      "properties": {
        "variables": [
          {
            "key": "lead_telephone",
            "value": "{{widgets.Collect_Telephone.inbound.Body}}"
          }
        ]
      }
    },
    {
      "name": "Collect_Email",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "SaveEmail"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "Parfait. Quel est votre *email* ? (ou écrivez “pas d’email” si vous n’en avez pas)"
      }
    },
    {
      "name": "SaveEmail",
      "type": "set-variables",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "Collect_Budget"
        }
      ],
      "properties": {
        "variables": [
          {
            "key": "lead_email",
            "value": "{{widgets.Collect_Email.inbound.Body}}"
          }
        ]
      }
    },
    {
      "name": "Collect_Budget",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "SaveBudget"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "Quel est votre budget approximatif pour ce projet ? (ex : < 500k FCFA / 500k–2M FCFA / > 2M FCFA / Je préfère en discuter)"
      }
    },
    {
      "name": "SaveBudget",
      "type": "set-variables",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "Collect_Message"
        }
      ],
      "properties": {
        "variables": [
          {
            "key": "lead_budget",
            "value": "{{widgets.Collect_Budget.inbound.Body}}"
          }
        ]
      }
    },
    {
      "name": "Collect_Message",
      "type": "send-and-wait-for-reply",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "SaveMessage"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "Merci. Décris en quelques lignes le projet ou la demande (objectifs, contraintes, délais...)."
      }
    },
    {
      "name": "SaveMessage",
      "type": "set-variables",
      "transitions": [
        {
          "event": "incomingMessage",
          "next": "SaveLeadWebhook"
        }
      ],
      "properties": {
        "variables": [
          {
            "key": "lead_message",
            "value": "{{widgets.Collect_Message.inbound.Body}}"
          },
          {
            "key": "lead_source",
            "value": "WhatsApp Bot Welely"
          },
          {
            "key": "lead_channel",
            "value": "{{trigger.message.From}}"
          }
        ]
      }
    },
    {
      "name": "SaveLeadWebhook",
      "type": "http-request",
      "transitions": [
        {
          "event": "success",
          "next": "ConfirmToUser"
        },
        {
          "event": "failed",
          "next": "ConfirmToUser"
        }
      ],
      "properties": {
        "method": "POST",
        "url": "https://welely.com/api/chatbot-leads",
        "content_type": "application/json",
        "body": "{ \"nom\": \"{{flow.data.lead_name}}\", \"telephone\": \"{{flow.data.lead_telephone}}\", \"email\": \"{{flow.data.lead_email}}\", \"service\": \"{{flow.data.lead_service_choice}}\", \"budget\": \"{{flow.data.lead_budget}}\", \"message\": \"{{flow.data.lead_message}}\", \"source\": \"{{flow.data.lead_source}}\", \"channel\": \"{{flow.data.lead_channel}}\" }",
        "headers": [
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "timeout": 15000
      }
    },
    {
      "name": "ConfirmToUser",
      "type": "send-message",
      "transitions": [
        {
          "event": "sent",
          "next": "NotifyHuman"
        }
      ],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "✅ Merci {{flow.data.lead_name}} — votre demande a bien été reçue.\\nUn membre de notre équipe commerciale vous contacte très bientôt.\\nVous pouvez aussi appeler : +2250708453194 ou écrire : contact@welely.com"
      }
    },
    {
      "name": "NotifyHuman",
      "type": "http-request",
      "transitions": [
        {
          "event": "success",
          "next": "End"
        },
        {
          "event": "failed",
          "next": "End"
        }
      ],
      "properties": {
        "method": "POST",
        "url": "https://welely.com/api/notify-internal",
        "content_type": "application/json",
        "body": "{ \"nom\": \"{{flow.data.lead_name}}\", \"telephone\": \"{{flow.data.lead_telephone}}\", \"email\": \"{{flow.data.lead_email}}\", \"service\": \"{{flow.data.lead_service_choice}}\", \"budget\": \"{{flow.data.lead_budget}}\", \"message\": \"{{flow.data.lead_message}}\" }",
        "headers": [
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "timeout": 10000
      }
    },
    {
      "name": "End",
      "type": "send-message",
      "transitions": [],
      "properties": {
        "to": "{{trigger.message.From}}",
        "body": "Merci ! Si vous avez d'autres questions, répondez simplement à ce message. — Welely"
      }
    }
  ],
  "friendly_name": "Chatbot Welely - Flow importable",
  "version": "1.0"
}
