tawn33y / whatsapp-cloud-api

A Node.js library for creating bots and sending/receiving messages using the Whatsapp Cloud API.
https://www.npmjs.com/package/whatsapp-cloud-api
GNU General Public License v3.0
187 stars 51 forks source link

help with list_reply #39

Closed kev095 closed 1 year ago

kev095 commented 1 year ago

Hello, I have a question when I send a list of options (list_reply) and the user enters an "account status" option and then enters the document number. How do I get that value going through the previous validations again?

image

tawn33y commented 1 year ago

You need a cache or database to persist this. Let's say you have a series of inputs, eg first name, last name, document number, etc, you need to save each of these during the step when the user replies.

This can also prove useful especially if the server restarts in the middle of a conversation as the user won't have to start answering the questions from the top.

Here's an example (not clean code, but demonstrates the idea):

let cache = {}; // can be redis, sql, etc 

if (msg.type === 'list_reply') {
  if (msg.data.id === 'first_name') {
    cache = { ...cache, firstName: msg.data.text };
    // send reply, proceed to next step, etc
  } else if (msg.data.id === 'document_number') {
    cache = { ...cache, documentNumber: msg.data.text };
    // send reply, proceed to next step, etc
  } else if (msg.data.id === 'email') {
    cache = { ...cache, email: msg.data.text };
    // send reply, proceed to next step, etc
  }
} 
kev095 commented 1 year ago

Thank you very much for your answer, your successful project is great!

tawn33y commented 1 year ago

You're most welcome. Thank you very much! 😊

tawn33y commented 1 year ago

Marking this as closed for now. @kev095, feel free to reopen if need be 😊