Tuesday, 30 May 2017

Messenger postback won't trigger new query

I'm working on a Messenger bot that asks a question with several options, and when a user clicks one of the answer it triggers the next question - at least it should. However, unless one of the answers is typed manually, it doesn't trigger.

app.post('/webhook', function (req, res) {
var events = req.body.entry[0].messaging;
for (i = 0; i < events.length; i++) {
    var event = events[i];
    if (event.message && event.message.text) {
        if (!askQuestions(event.sender.id, event.message.text)) {
            sendMessage(event.sender.id, {
                text: "Echo: " + event.message.text
            });
        }
    }
    else if (event.postback) {
        var pb = JSON.stringify(event.postback)
        console.log("Postback received: " + pb);
    }
}
res.sendStatus(200);

The general sendMessage function:

function sendMessage(recipientId, message) {
request({
    url: 'https://graph.facebook.com/v2.6/me/messages'
    , qs: {
        access_token: process.env.PAGE_ACCESS_TOKEN
    }
    , method: 'POST'
    , json: {
        recipient: {
            id: recipientId
        }
        , message: message
    , }
}, function (error, response, body) {
    if (error) {
        console.log('Error sending message: ', error);
    }
    else if (response.body.error) {
        console.log('Error: ', response.body.error);
    }
});

And the main function that comes up with the questions and answers. The initial "hello" triggers question number one (which type of property), and if "Studio" is picked it should trigger the Rent/Buy question. Except, unless if the user manually types "Studio", it doesn't. Nothing happens.

function askQuestions(recipientId, text) {
text = text || "";
var values = text.split(' ');
if (text === 'hello') {
    message = {
        "attachment": {
            "type": "template"
            , "payload": {
                "template_type": "generic"
                , "elements": [{
                    "title": "Hey there! What type of property are you looking for?"
                    , "buttons": [{
                        "type": "postback"
                        , "title": "Studio"
                        , "payload": "Studio"
                    , }, {
                        "type": "postback"
                        , "title": "Apartment"
                        , "payload": "Apartment"
                    , }, {
                        "type": "postback"
                        , "title": "House"
                        , "payload": "House"
                    , }]
                    }]
            }
        }
    };
    sendMessage(recipientId, message);
    return true;
}
if (text === 'Rent') {
    message = {
        "attachment": {
            "type": "template"
            , "payload": {
                "template_type": "generic"
                , "elements": [{
                    "title": "Okay. And what is your budget?"
                    , "buttons": [{
                        "type": "postback"
                        , "title": "500 EUR or less"
                        , "payload": "User " + recipientId + " likes picture "
                    , }, {
                        "type": "postback"
                        , "title": "Between 500 and 750 EUR"
                        , "payload": "User " + recipientId + " likes picture "
                    , }, {
                        "type": "postback"
                        , "title": "Between 750 and 1000 EUR"
                        , "payload": "User " + recipientId + " likes picture "
                    , }]
                    }]
            }
        }
    };
    sendMessage(recipientId, message);
    return true;
}
if (text === 'Studio') {
    message = {
        "attachment": {
            "type": "template"
            , "payload": {
                "template_type": "generic"
                , "elements": [{
                    "title": "Great! Are you looking to rent or buy?"
                    , "buttons": [{
                        "type": "postback"
                        , "title": "Buy"
                        , "payload": "Buy"
                    , }, {
                        "type": "postback"
                        , "title": "Rent"
                        , "payload": "Rent"
                    , }]
                    }]
            }
        }
    };
    sendMessage(recipientId, message);
    return true;
}
return false;

The code is far from clean, I'm just trying to get this to work before I advance it any further. :-) Anyone has an idea?



via Rudolf

No comments:

Post a Comment