eristemena / dialogflow-fulfillment-webhook-php

Dialogflow agent fulfillment PHP library supporting v1 & v2
MIT License
103 stars 47 forks source link

custom payload outside of fulfillmentMessages array #12

Open isbkch opened 5 years ago

isbkch commented 5 years ago

I've been trying to send back a normal text reply plus a Facebook's receipt template, but the template always ends up outside of the fulfillmentMessagespart of the JSON response.

I was not able to see the template so I checked in DialogFlow and it clearly sits outside of the fulfillmentMessages under unspecified. Any idea how that might happen?

My BE is in Lumen. The method that builds the response:

public function history()
    {
        $this->dialogAgent->reply('The following, ' . $this->user->first_name . ' is you order history');

        $history              = $this->user->getOrders();
        $historyReceipt = FacebookTemplates::receipt($history, $this->user);

        $this->dialogAgent->reply($historyReceipt);

        return response()->json($this->dialogAgent->render());
    }

And the method that build the receipt:

Public static function receipt($orders, $user)
    {
        $elements = [];
        foreach ($orders as $order) {
            $elements [] = [
                'title'     => 'Classic White T-Shirt',
                'subtitle'  => '100% Soft and Luxurious Cotton',
                'quantity'  => $order->quantity,
                'price'     => 50,
                'currency'  => 'USD',
                'image_url' => 'http://petersapparel.parseapp.com/img/whiteshirt.png',
            ];
        }

        return \Dialogflow\RichMessage\Payload::create([
            'attachment' => [
                'type'    => 'template',
                'payload' => [
                    'template_type'  => 'receipt',
                    'recipient_name' => 'My Name',
                    'summary'        => [
                        'subtotal'      => 400,
                        'shipping_cost' => 50,
                        'total_tax'      => 20,
                        'total_cost'    => 3334
                    ],
                    'elements' => $elements,
                ],
            ],
        ]);   
    }

Just putting dummy data in the template so far.

The template I'm trying to mimic is this one https://developers.facebook.com/docs/messenger-platform/send-messages/template/receipt