Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Dynamic elements in convo.ask attachment template #557

Closed
nicgordon opened this issue Dec 15, 2016 · 2 comments
Closed

Dynamic elements in convo.ask attachment template #557

nicgordon opened this issue Dec 15, 2016 · 2 comments

Comments

@nicgordon
Copy link

I have a conversation that asks a series of questions, then takes those answers and performs a database query. I would like the results of that query to be presented as template elements such as in the example below. Does anyone know how I may be able to achieve this?

I am guessing it may be possible through convo.setVar but I'm not exactly sure how. Any help would be greatly appreciated!

controller.hears(['buy a shirt'], 'message_received', function(bot, message) {

    bot.startConversation(message, function(err, convo) {
        convo.ask({
            attachment: {
                'type': 'template',
                'payload': {
                    'template_type': 'generic',
                    'elements': [
                        {
                            'title': 'Classic White T-Shirt',
                            'image_url': 'http://petersapparel.parseapp.com/img/item100-thumb.png',
                            'subtitle': 'Soft white cotton t-shirt is back in style',
                            'buttons': [
                                {
                                    'type': 'web_url',
                                    'url': 'https://petersapparel.parseapp.com/view_item?item_id=100',
                                    'title': 'View Item'
                                },
                                {
                                    'type': 'web_url',
                                    'url': 'https://petersapparel.parseapp.com/buy_item?item_id=100',
                                    'title': 'Buy Item'
                                },
                                {
                                    'type': 'postback',
                                    'title': 'Bookmark Item',
                                    'payload': 'White T-Shirt'
                                }
                            ]
                        },
                        {
                            'title': 'Classic Grey T-Shirt',
                            'image_url': 'http://petersapparel.parseapp.com/img/item101-thumb.png',
                            'subtitle': 'Soft gray cotton t-shirt is back in style',
                            'buttons': [
                                {
                                    'type': 'web_url',
                                    'url': 'https://petersapparel.parseapp.com/view_item?item_id=101',
                                    'title': 'View Item'
                                },
                                {
                                    'type': 'web_url',
                                    'url': 'https://petersapparel.parseapp.com/buy_item?item_id=101',
                                    'title': 'Buy Item'
                                },
                                {
                                    'type': 'postback',
                                    'title': 'Bookmark Item',
                                    'payload': 'Grey T-Shirt'
                                }
                            ]
                        }
                    ]
                }
            }
        }, function(response, convo) {
            // whoa, I got the postback payload as a response to my convo.ask!
            convo.next();
        });
    });
});
@PierreMarieRiviere
Copy link

Have you tried assigning (global) variables to your answers? That should do the trick.
If you get Answer 1 somewhere, you could set var url1, var title1 and var payload1, then use them appropriately.

@nicgordon
Copy link
Author

Yeah I did try that however it doesn't work because convo messages are precompiled when convo.ask is first run, meaning that if you initialised your template elements collection array as an empty array, then that is what will be printed. Normally you would use convo.setVar to inject a variable set later, however that only works with strings.

The answer actually turned out to be quite easy however, and is documented here: https://github.com/howdyai/botkit/blob/bf2a10edb784d20c99cf5fdc12a6e17480795635/readme.md#multi-stage-conversations

Basically, you need a function to generate a new section of the conversation which you only call in the callback of the previous question. Thanks for taking the time to look at my problem though!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants