Charca / bootbot

Facebook Messenger Bot Framework for Node.js
MIT License
974 stars 253 forks source link

How to verify user reply information in Conversation. #157

Closed realnsleo closed 5 years ago

realnsleo commented 5 years ago

Hi guys, This is more of a question that an issue and I was hoping there was someone out there who can assist me. In my code, I have initiated a conversation that goes like:

const getAccountNumber = (convo) => {
  convo.ask(`Please provide your account number`, (payload, convo) => {
    const text = payload.message.text;
    convo.set('account_number', text);
    convo.say(`Awesome. Let's see ...`).then(() => fetchAccountBalance(convo));
  });
};

const fetchAccountBalance = (convo) => {
  let account = <some API call to retrieve the account details>
  convo.say(`Your Account Balance is:. ${account.balance}`, { typing: true })   
  convo.end();
};

chat.conversation((convo) => {
    getAccountNumber(convo);
});

Basically, the user responds with their account number which is sent to an external API to retrieve the account details. I want to satisfy a scenario where the user supplies a wrong account number. I want the bot to respond with something like this:

const fetchAccountBalance = (convo) => {
  let account = result.find(x => { return x.account_no === parseInt(convo.get('account_no')); });

  if(!account) {
    convo.ask(`Account number does not exist. Please try again.`, (payload, convo) => {
      const text = payload.message.text;
      convo.set('account_number', text);
      convo.say(`Awesome. Let's see ...`).then(() => fetchAccountBalance(convo));
    });
  }

  convo.say(`Your Account Balance is:.`, { typing: true })  
  convo.end();
};

I have tried this but it does not work. It hangs with no Error Message. I would like be pointed into the right direction on how to achieve this.

Thank you!

realnsleo commented 5 years ago

The code above actually works :) I had not tried out what I posted in my code!!