pimax/fb-messenger-php

Dynamically add elements in some easy way

Closed this issue · 0 comments

Hey
First of all thanks for a great repo and this code really makes it great to build and work on bots. I will try to learn more about it and then maybe contribute with ideas. The first I notice, maybe lack of knowledge of your code but...

When creating several elements in code how do you do this?

Look at the code below

// When bot receive "list"
                case 'list':
                    $bot->send(new StructuredMessage($message['sender']['id'],
                        StructuredMessage::TYPE_LIST,
                        [
                            'elements' => [
                                new MessageElement(
                                    'Classic T-Shirt Collection', // title
                                    'See all our colors', // subtitle
                                    'http://bit.ly/2pYCuIB', // image_url
                                    [ // buttons
                                        new MessageButton(MessageButton::TYPE_POSTBACK, // type
                                            'View', // title
                                            'POSTBACK' // postback value
                                        )
                                    ]
                                ),
                                new MessageElement(
                                    'Classic White T-Shirt', // title
                                    '100% Cotton, 200% Comfortable', // subtitle
                                    'http://bit.ly/2pb1hqh', // image_url
                                    [ // buttons
                                        new MessageButton(MessageButton::TYPE_WEB, // type
                                            'View', // title
                                            'https://google.com' // url
                                        )
                                    ]
                                )
                            ],
                            'buttons' => [
                                new MessageButton(MessageButton::TYPE_POSTBACK, 'First button', 'PAYLOAD 1')
                            ]
                        ],
                        [
                            new QuickReplyButton(QuickReplyButton::TYPE_TEXT, 'QR button','PAYLOAD')
                        ]
                    ));
                    break;

I have an external API which returns between 2-8 records and I want to loop through them and create a list elements for each of them.

// Start for each loop here and insert data from external API here
'elements' => [
                                new MessageElement(
                                    $dynamicAPIRecordTitle, // title
                                    $dynamicAPIRecordSubTitle, // subtitle
                                    $dynamicAPIRecordImageUrl, // image_url
                                    [ // buttons
                                        new MessageButton(MessageButton::TYPE_WEB, // type
                                            $dynamicAPIRecordButtonTitleText, // title
                                            $dynamicAPIRecordButtonUrlValue // url value
                                        )
                                    ]
// Stop loop here

As you can see I want to create several elements and I just don't see how I can do this. Please help me. Also I want to add the property to tell the webview how tall the window will be when opened.