Botkit: convo.ask() with Attachment in Facebook Messenger

Created on 8 Nov 2016  路  3Comments  路  Source: howdyai/botkit

I saw its Available with Slack platform, couldn't find any examples for using Buttons or Generic Templates with Messenger I've tried this:

convo.ask({
        attachment: {
            'type': 'template',
            'payload': {
                'template_type': 'button',
                'text': 'Do you know the name of the artist?',
                'buttons': [
                    {
                        'type': 'postback',
                        'title': 'Yes',
                        'payload' : 'KNOWN_ARTIST'
                    },
                    {
                        'type': 'postback',
                        'title': 'No',
                        'payload' : 'UNKNOWN_ARTIST'
                    }

                ]
            }
        }
    }, function (response, convo) {
        console.log('response', response)
        convo.next();
    })

but looks like it doesn't work...

Basically what I want to do is in the middle of the conversation send templates and continue
Should I save the conversation globally so if I want to refer to it from postback block I can just use it?
Is there a better way to do this?
anyone?

Facebook-related question

Most helpful comment

Here are some examples:

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();
        });
    });
});

controller.on('facebook_postback', function(bot, message) {

    bot.reply(message, 'Great Choice!!!! (' + message.payload + ')');

});

You can also save the question in a Thread and then reference it were you need it as long as you are in the same convo.

Like so:

convo.addQuestion('A question or a facebook message object', [

    {
        pattern: 'KEYWORD',
        callback: function(response, convo) {
            convo.gotoThread('HANDLE_ANSWER_1');
        }
    },
    {
        pattern: 'quit',
        callback: function(response, convo) {
            convo.stop();
        }
    },
    {
        default: true,
        callback: function(response, convo) {
            convo.gotoThread('HANDLE_ANSWER_2');
        }
    }
],{key: 'answer'},'question_template');

// set up HANDLE_ANSWER_1 thread. Using the `completed` action will end the conversation immediately and mark it as success.

convo.addMessage({
    text: 'You got it!',
    action: 'completed'
},'HANDLE_ANSWER_1');

// set up HANDLE_ANSWER_2 thread and directs the botkit back to the `question_template` thread

convo.addMessage({
    text: 'Sorry that was not quite right',
    action: 'question_template'
}, 'HANDLE_ANSWER_2');

I hope this helps.

Alex

All 3 comments

Here are some examples:

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();
        });
    });
});

controller.on('facebook_postback', function(bot, message) {

    bot.reply(message, 'Great Choice!!!! (' + message.payload + ')');

});

You can also save the question in a Thread and then reference it were you need it as long as you are in the same convo.

Like so:

convo.addQuestion('A question or a facebook message object', [

    {
        pattern: 'KEYWORD',
        callback: function(response, convo) {
            convo.gotoThread('HANDLE_ANSWER_1');
        }
    },
    {
        pattern: 'quit',
        callback: function(response, convo) {
            convo.stop();
        }
    },
    {
        default: true,
        callback: function(response, convo) {
            convo.gotoThread('HANDLE_ANSWER_2');
        }
    }
],{key: 'answer'},'question_template');

// set up HANDLE_ANSWER_1 thread. Using the `completed` action will end the conversation immediately and mark it as success.

convo.addMessage({
    text: 'You got it!',
    action: 'completed'
},'HANDLE_ANSWER_1');

// set up HANDLE_ANSWER_2 thread and directs the botkit back to the `question_template` thread

convo.addMessage({
    text: 'Sorry that was not quite right',
    action: 'question_template'
}, 'HANDLE_ANSWER_2');

I hope this helps.

Alex

Thank you very much for this,
the second example is what I was looking for

Thanks @alexbudin !

Closing as resolved!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abinashmohanty picture abinashmohanty  路  4Comments

iworkforthem picture iworkforthem  路  3Comments

dfischer picture dfischer  路  4Comments

matueranet picture matueranet  路  4Comments

abinashmohanty picture abinashmohanty  路  4Comments