Search code examples
phpfacebook-chatbot

I can't get my Messenger chatbot to reply successfully to a payload


So I am trying to implement "quick replies" as per the docs here: https://developers.facebook.com/docs/messenger-platform/send-messages/quick-replies#text

I have a chatbot that can successfully reply to any input from the user. My main problem arises when the answer is a quick reply payload so I have this at the very top:

$postback = isset($input['entry'][0]['messaging'][0]['postback']['payload']) ? $input['entry'][0]['messaging'][0]['postback']['payload']: '' ;
$message = isset($input['entry'][0]['messaging'][0]['message']['text']) ? $input['entry'][0]['messaging'][0]['message']['text']: '' ;

After that I check which type of message it is. If it is a "message" then some responses are displayed. If it's a "postback" then others. However, inside "message" I also check for a quick reply ($qr) and if the value isn't empty it should go in that if but it's not doing it and I have no idea why.

Towards the end I have a replyUser method that takes in the $jsonData and just replies. That's working fine. Additionally, the defaultJSON reply just sends back plaintext while jsonQuickReplies formats the json to be quick replies. I know this works because I can see the quick replies no problem is just after the user selects one that nothing happens.

The postback payload (even though it's caught in the $input['entry'][0]['messaging'][0]['message']['quick_reply']['payload'] is, for some reason, skipping the if and I have no idea why (so it always sends me the message under the "else".

if($message || $postback) { 

    if($message){

        $qr = isset($input['entry'][0]['messaging'][0]['message']['quick_reply']['payload']) ? $input['entry'][0]['messaging'][0]['message']['quick_reply']['payload']: '' ;

        if(!empty($qr)){
            if(preg_match('[postback payload from qr]', strtolower($qr))){
                $message_to_reply = "Something for the user";
                $jsonData = defaultJSON($sender, $message_to_reply);
            }
        }

        //Some Basic rules to validate ("chat") incoming messages
        if(preg_match('[hey]', strtolower($message))) { 
           $message_to_reply = "welcome message to the user";
           $jsonData = jsonQuickReplies($sender, $message_to_reply);
        }else {
           $message_to_reply = "default message not knowing what the user said";
           $jsonData = defaultJSON($sender, $message_to_reply);
    }else if($postback){
        // If Page receives Postback, process the Postback and prepare content to reply
        if(preg_match('[any postback]', strtolower($postback))){
            $message_to_reply = "process postback";
            $jsonData = defaultJSON($sender, $message_to_reply);
        }
    }
}

var breakdown:

$postback contains any postback assigned by an answer.

$message would be anything the user sends.

$qr is any quick reply value read from the postback payload.

$message_to_reply is the text message that will be sent to the user as a reply.

$jsonData is just the final json values being sent for replyUser to use in the response.

$sender is the user id from the user who just engaged with the chatbot.

The code path taken is the following: Any user sends something. That message is then read by the $postback or $message variables (literally looking into the $input. After that the if follows and if it's a $message it follows that route (the opposite is true if it's a $postback). After this, the message is created either by the defaultJSON() or jsonQuickReplies() method and finally replyUser takes the values and sends it to the user.


Solution

  • Your braces don't match up. I'l just try to intend the code to match the braces to make it more apparent.

    if($message || $postback) { 
    
        if($message){
    
            $qr = isset($input['entry'][0]['messaging'][0]['message']['quick_reply']['payload']) ? $input['entry'][0]['messaging'][0]['message']['quick_reply']['payload']: '' ;
    
            if(!empty($qr)){
                if(preg_match('[postback payload from qr]', strtolower($qr))){
                    $message_to_reply = "Something for the user";
                    $jsonData = defaultJSON($sender, $message_to_reply);
                }
            }
    
            //Some Basic rules to validate ("chat") incoming messages
            if(preg_match('[hey]', strtolower($message))) { 
               $message_to_reply = "welcome message to the user";
               $jsonData = jsonQuickReplies($sender, $message_to_reply);
            }else {
               $message_to_reply = "default message not knowing what the user said";
               $jsonData = defaultJSON($sender, $message_to_reply);
               //MISSING BRACE
            }else if($postback){
                // If Page receives Postback, process the Postback and prepare content to reply
                if(preg_match('[duda]', strtolower($postback))){
                    $message_to_reply = "process postback";
                    $jsonData = defaultJSON($sender, $message_to_reply);
                }
            }
        }