You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Facebook Messenger中实现嵌套数组式快速回复功能

Fixing Nested Quick Reply Flow for Facebook Messenger

Hey there! Let’s work through this quick reply flow issue you’re facing. The core problem with your current $questions array is that it’s just a flat, alternating list of questions and options—there’s no explicit mapping telling your code which option leads to which follow-up question. That’s why clicking "Order" isn’t jumping to "Order Status" right now.

Step 1: Restructure Your Data for Clear Flow Mapping

First, let’s rebuild your array to explicitly link each quick reply option to its corresponding next question. We’ll use an associative array where each entry represents a conversation "node" with a unique ID, the question text, and quick replies that point to the next node:

$questionFlow = [
    'main_menu' => [
        'text' => "What can I help you?",
        'replies' => [
            ['title' => 'Order', 'next_node' => 'order_status'],
            ['title' => 'Shipping', 'next_node' => 'shipping_topic'], // Define this node if needed
            ['title' => 'How to Buy', 'next_node' => 'how_to_buy'] // Or leave empty for end of flow
        ]
    ],
    'order_status' => [
        'text' => "Order Status",
        'replies' => [
            ['title' => 'Status', 'next_node' => 'status_details'],
            ['title' => 'Tracking', 'next_node' => 'tracking_info'],
            ['title' => 'ETA', 'next_node' => 'eta_details']
        ]
    ],
    'check_out' => [
        'text' => "Check this out",
        'replies' => [
            ['title' => 'Shipping Status', 'next_node' => 'shipping_status'],
            ['title' => 'Bla bla bla', 'next_node' => 'bla_details'],
            ['title' => 'bb bb bb', 'next_node' => 'bb_details']
        ]
    ],
    'showing' => [
        'text' => "Showing",
        'replies' => [
            ['title' => 'View steps', 'next_node' => 'view_steps'],
            ['title' => 'View Video', 'next_node' => 'view_video'],
            ['title' => 'View Steps', 'next_node' => 'view_steps']
        ]
    ]
];

Each node includes:

  • A unique key (like main_menu) to identify the conversation state
  • text: The question displayed to the user
  • replies: An array of quick reply options, each with a title (button text) and next_node (the key of the question to jump to when clicked)

Step 2: Implement the Flow Logic

Now, we’ll add functions to send quick replies and handle user interactions. The key here is using the next_node value as the payload for each quick reply—this lets us directly know which question to show next when the user clicks an option.

// Send a quick reply message based on a conversation node
function sendQuickReply($userId, $nodeKey) {
    global $questionFlow;

    // Handle invalid nodes gracefully
    if (!isset($questionFlow[$nodeKey])) {
        sendTextMessage($userId, "Sorry, I didn't catch that. Let's start over!");
        sendQuickReply($userId, 'main_menu');
        return;
    }

    $currentNode = $questionFlow[$nodeKey];
    $quickReplies = [];

    // Build Messenger-compatible quick reply objects
    foreach ($currentNode['replies'] as $reply) {
        $quickReplies[] = [
            'content_type' => 'text',
            'title' => $reply['title'],
            'payload' => $reply['next_node'] // Pass the next node key as payload
        ];
    }

    // Construct the API request
    $messageData = [
        'recipient' => ['id' => $userId],
        'message' => [
            'text' => $currentNode['text'],
            'quick_replies' => $quickReplies
        ]
    ];

    // Send the message via Facebook Messenger API (implement this function based on your setup)
    sendMessengerApiRequest($messageData);

    // Optional: Track the user's current node in a database/cache if handling free-text inputs
    updateUserSession($userId, $nodeKey);
}

// Handle incoming user replies
function handleUserResponse($userId, $receivedPayload) {
    // Use the payload from the quick reply to jump directly to the next node
    sendQuickReply($userId, $receivedPayload);
}

// Initialize the conversation with the main menu
sendQuickReply($userFacebookId, 'main_menu');

Key Notes to Make This Work:

  • Messenger API Format: Ensure your sendMessengerApiRequest function correctly posts data to Facebook’s Graph API endpoint with your page access token.
  • Session Tracking: If you want to handle free-text responses (not just quick replies), store the user’s current conversation node in a database or cache. For example, if a user types "Order" instead of clicking the button, you can map that text to the correct next_node based on their current state.
  • Back Navigation: Add a "Back" option to nodes if you want users to return to previous menus—just set next_node to the key of the parent menu (e.g., add ['title' => 'Back', 'next_node' => 'main_menu'] to the order_status replies).

内容的提问来源于stack exchange,提问作者Kelvin Toh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 07:04:12