Search code examples
facebook-messengerfacebook-messenger-botmessenger

How to keep track of what question user is responding to?


Some questions my messenger chatbot is asking require textual input. In the button template I can set the payload so I know what exactly user chosen and handle it based on that. But with textual responses I need to filter if user answer is somehow related to "Change address, Billing, Returns, etc." For example:

bot-question: What is your new phone number?
user-answer: +123 123 12345

And now in the back-end I'm doing this:

if(user-answer in_array('billing keywords') {
    // code
} elseif (user-answer in_array('delivery keywords')) {
    // code
} elseif (user-answer in_array('payments keywords')) {
    // code
} elseif (user-answer in_array('change-phone-number keywords')) {
    // Finally got where I wanted 🎉😒
}

Isn't it possible to somehow add some tag to bot-question, so I would already know that the answer will be related to change-phone-number keywords? For example like this:

  "template_type" => "text",
  "text" => "What is your new phone number?",
  "payload" => "changing_phone_number"

Solution

  • In the end it was impossible to get some postback from a normal text message, so what I did, I created additional columns:

    • expecting_billing_info
    • expecting_phone_number_update
    • etc.

    And I set it to 0 by default, but when bot asks a question like:

    Would you like to change your phone number?

    I set it to 1 and when user types it in I do:

    if (expecting_phone_number_update == 1) {
        // 1. get the message
        // 2. Check if it's correct phone number format
        // 3. Reply based on 2. and update phone number
    } elseif (expecting_billing_info == 1) {
        // ...
    }
    

    This is not perfectly clean solution, but it does the work. Let's hope messenger will release this postback feature one day for normal messages as well! :)