Search code examples
phphttpresponsewebhooksactions-on-google

Escape statements on basicCard response formatted text not parsed


I have a php script on my website and this action is linked as a webhook to it. So once the data is submitted to the server, this is the response I send in json.

`{"payload":
 {"google":
  {"expectUserResponse":true,"
     richResponse":
      {"items":[{ 
          "simpleResponse":{
            "textToSpeech":"This feature is coming soon! Please wait until you receive a notification about its launch! You can verify if the details collected for your entry were right and inform the creator if they weren't. Here are the details."
          }
         },{"basicCard":{"title":"This is the entry I made","subtitle":"But I couldn't submit it","formattedText":"Item: Books  \\nRemarks: McDonalds  \\nDate: 2019-02-17T12:00:00+05:30  \\nAmount: 2098  \\nCategory: Expense  \\nIf the details above aren't right, please inform the creator."}}]}}}}'

But none of the \n get parsed. What I actually entered in php was a double space followed by single backslash and then n but for some reason php adds an extra backslash preventing it from escaping even when I used json encode json_unescaped_slashes. Here is the php code I used to create the json.

            $response=new \stdClass();
            $response->payload->google->expectUserResponse= true;
            $items=new \stdClass();
            $res->simpleResponse->textToSpeech="This feature is coming soon! Please wait until you receive a notification about its launch! You can verify if the details collected for your entry were right and inform the creator if they weren't. Here are the details.";
            $items->basicCard->title="This is the entry I made";
            $items->basicCard->subtitle="But I couldn't submit it";
            $items->basicCard->formattedText="Item: ".$type."  \nRemarks: ".$item."  \nDate: ".$date."  \nAmount: ".$amount."  \nCategory: ".$category."  \nIf the details above aren't right, please inform the creator.";
            $response->payload->google->richResponse->items[]=json_encode($res, JSON_UNESCAPED_SLASHES).",".json_encode($items, JSON_UNESCAPED_SLASHES);
            $response2=str_replace('\"','"',json_encode($response, JSON_UNESCAPED_SLASHES));
            $response2=str_replace('"{','{',$response2);
            $response2=str_replace('}"','}',$response2);
            echo $response2;

And this is the link to the screenshot of how it appears in the basicCard response https://photos.app.goo.gl/RzG5H3VgTJfrgfB59


Solution

  • The problem is that you're encoding the object as JSON as an intermediary step, rather than building the object first in PHP and then encoding it. As such, you're getting some strange double-encoding issues.

    Doing something like this

      $msg=new \stdClass();
      $msg->simpleResponse->textToSpeech="Coming soon";
    
      $card=new \stdClass();
      $card->basicCard->title="This is the entry I made";
      $card->basicCard->subtitle="But I couldn't submit it";
      $card->basicCard->formattedText="Item: ".$type."  \nRemarks: ".$item."  \nDate: ".$date."  \nAmount: ".$amount."  \nCategory: ".$category."  \nIf the details above aren't right, please inform the creator.";
    
      $response=new \stdClass();
      $response->payload->google->expectUserResponse= true;
      $response->payload->google->richResponse->items = array(
        $msg,
        $card
      );
    
      $json = json_encode( $response );
    

    is more like what you're trying to do.