Search code examples
phparraysjsonactions-on-google

Why are my google actions "suggestions" only showing the last entry?


I am creating a response from my own webhook.
Now i wanted to send the suggestions array, but am struggling with creating an array in an array. How does these need to be set up?

    $jsonResponse = json_encode(array(
        "session" => array(
            "params" => array(
                "antA" => "Hello Answer A",
                "antB" => "Hello Answer B",
                "extrainfo" => "This is some extra information"
            )
        ),
        "prompt" => array(
            "override" => false,
            "firstSimple" => array(
                "speech" => "<speak>".$speech."</speak>"
            ),
            "suggestions" => array(
                    "title" => "aa",
                    "title" => "bb",
                    "title" => "cc"
            ),
        )

    ));

Solution

  • The issue is that prompt.suggestions is specified to take an indexed array of Suggesion objects, that is, an array that maps from numbers to the objects (or just a list, where the numbering is assumed). But you are providing an associative array - that is, mapping a property name to something. Furthermore, your associative array is naming everything the same. Php uses similar syntax for indexed arrays and associative arrays, so it can sometimes be unclear what you actually need.

    In this case, that part of your code should probably look something more like this:

                "suggestions" => array(
                        array("title" => "aa"),
                        array("title" => "bb"),
                        array("title" => "cc")
                )