Search code examples
node.jsfacebookbotsfacebook-messenger

Conversational facebook messenger bot


Hi I am making a new bot for a product where I need to ask customer login id and password.

So for my case, user would initiate a conversation by typing hi then bot will reply Hey! How can I help you, here if customer enter I want to transfer money then bot will respond Please enter a login ID, first here customer will enter login id and bot will respond Enter your password then it goes like this.

I have done like below in node js which seems not a correct way to me.

app.post('/webhook/', function (req, res) {

    let messaging_events = req.body.entry[0].messaging
    for (let i = 0; i < messaging_events.length; i++) {
        let event = req.body.entry[0].messaging[i]
        let sender = event.sender.id
        if (event.message && event.message.text) {
            let text = event.message.text.toLowerCase();
            var greets = ['hi', 'hey', 'hello', 'good morning', 'good evening']
            if(greets.indexOf(text) > -1) {
                sendTextMessage(sender, 'Hey! How can I help you ?')
            }
            else if((text.indexOf('money transfer') > -1) || (text.indexOf('transfer money') > -1)) {
                sendTextMessage(sender, 'OK. Please enter your login details.')
                lastMessage = 'Please enter your login ID.'
            } else if(text.indexOf('restart') > -1) {
                lastMessage = ''
                sendTextMessage(sender, 'Thanks, Give a seconds to me :)')
            }
            else {
                sendTextMessage(sender, 'Huh! Uhhh broke me :(')
            }
        } else if (event.postback) {
            var payload = event.postback.payload;
            if(payload) {
                // When a postback is called, we'll send a message back to the sender to 
                // let them know it was successful.
                // do nothing
                switch (payload) {
                    case 'USER_DEFINED_PAYLOAD':
                        sendTextMessage(sender, 'Welcome to XXX, What are you looking for today ?')
                        break;
                }
            }
            console.log("Postback received: " + JSON.stringify(event.postback));
        }
    }
    res.sendStatus(200)
})

What is the best way to achieve my case, how flow will be maintained if user enter a login id then bot would ask for password.

Thanks in advance.


Solution

  • I think the best way is send a external link to customer, or send a structured message with a url button that customer could click and link to outside. So your external link should bring user's sender_id. Once your customer has logged in at external link, your callback function could then tell bot to send a login success! message, then you can continue the conversation.