Search code examples
javascriptnode.jsfacebook-messenger-bot

How to make unecesary text Messenger bot nodejs


So, I build a messenger bot using node js. How can I make:

  1. Whenever user send a random text (except the right command) the Bot will reply "Sorry I cant recognize that command"..
  2. How to make bot storing the Text from user and will confirm later ? Example : USER : Hello. BOT : What Else ? USER : World. BOT : What Else ? USER : done. BOT : I understood, BOT : 1. Hello BOT : 2. World BOT : Is that correct ?

Here is my current scripts for no. 1 :

function replyByText(sender, text, payload) {
let kata = text.toLowerCase()
let date = new Date()
let current_hour = date.getHours()
if (S(kata).contains('produk') || S(kata).contains('produknya') || S(kata).contains('product')) {
    sendOpeningProduct(sender, "Wait Sec..")
}

if (S(kata).contains('layanan')|| S(kata).contains('services')|| S(kata).contains('diagnose')){
    // sendOpeningServices(sender, 'Wait sec..' )
    sendPlainMessage(sender, 'Hi, what concern you most about your health? Please describe your symptoms..' )
}

if (S(kata).contains('health symptoms')){
    sendPlainMessage(sender, 'Tell me What You Feel..' )
}

if (S(kata).contains('im feeling sick') || S(kata).contains('im feeling unwell') || S(kata).contains('im feeling dizzy') || S(kata).contains('dizzy') || S(kata).contains('sick')){
    sendPlainMessage(sender, 'How old are you ?' )
}

if (S(kata).contains('3') || S(kata).contains('4') || S(kata).contains('5') || S(kata).contains('6') || S(kata).contains('7') || S(kata).contains('8') || S(kata).contains('9')){
    sendPlainMessage(sender, 'I see...' )
    sendOpeningProductKids(sender, "Just try our products")
}}

Any help will appreciate..


Solution

  • Solution to No 1:

    function replyByText(sender, text, payload) {
        let kata = text.toLowerCase()
        let date = new Date()
        let current_hour = date.getHours()
    
        if (S(kata).contains('produk') || S(kata).contains('produknya') || S(kata).contains('product')) {
            sendOpeningProduct(sender, "Wait Sec..")
        }
    
        else if (S(kata).contains('layanan')|| S(kata).contains('services')|| S(kata).contains('diagnose')){
            // sendOpeningServices(sender, 'Wait sec..' )
            sendPlainMessage(sender, 'Hi, what concern you most about your health? Please describe your symptoms..' )
        }
    
        else if (S(kata).contains('health symptoms')){
            sendPlainMessage(sender, 'Tell me What You Feel..' )
        }
    
        else if (S(kata).contains('im feeling sick') || S(kata).contains('im feeling unwell') || S(kata).contains('im feeling dizzy') || S(kata).contains('dizzy') || S(kata).contains('sick')){
            sendPlainMessage(sender, 'How old are you ?' )
        }
    
        else if (S(kata).contains('3') || S(kata).contains('4') || S(kata).contains('5') || S(kata).contains('6') || S(kata).contains('7') || S(kata).contains('8') || S(kata).contains('9')){
            sendPlainMessage(sender, 'I see...' )
            sendOpeningProductKids(sender, "Just try our products")
        }
        else{
            sendPlainMessage(sender, 'Sorry I cant recognize that command' )
        }
    
    }
    

    Solution to No 2:

    You will have to save state between each call to your webhook. follow these steps.

    There are several ways to solve this problem. You may solve this problem this way. Save state by updating a covesation model/object on each webhook call. Use the user's unique psid got from facebook api as a primary key or as part of a composite key for the model/object