How to Use GotoFlow?

Issue

I needed to be able to detect users that were already registered in my system so I need to make a get http query to my external system to check if that number was registered or not and depending on that divert it to a convenient flow.


Possible Solution

A possible solution is to make use of the gotoFlow to be able to divert the logic depending on the response of the http request to be able to verify if the user is registered in the database or is a new user. registered in the database or is a new user.

import { createBot, createProvider, createFlow, addKeyword, MemoryDB, EVENTS } from "@builderbot/bot";
import { BaileysProvider } from "@builderbot/provider-baileys";
import registeredUsersFlow from "./flows/registeredUsersFlow";
import unregisteredUsersFlow from "./flows/unregisteredUsersFlow";

const welcomeFlow = addKeyword(EVENTS.WELCOME).addAction(
  async (_, { gotoFlow, state }) => {
    const checkDB = await fetch("http://my.app.example/checkDB", {
      method: "POST",
      body: JSON.stringify({ phoneNumber: state.from }),
      headers: {
        "Content-Type": "application/json",
      },
    });

    const status = await checkDB.json();
    if (status === undefined) {
      await state.update({ Registration: false });
      return gotoFlow(unregisteredUsersFlow);
    }
    if (status === true) {
      return gotoFlow(registeredUsersFlow);
    }
  }
);

const main = async () => {
  const provider = createProvider(BaileysProvider);
  const database = new MemoryDB();
  const flow = createFlow([welcomeFlow, registeredUsersFlow, unregisteredUsersFlow]);

  await createBot({ flow, provider, database });
};

main();

Guides

My first chatbot

Learn how build your first chatbot in few minutes

Read more

Concepts

Understand the essential concepts for building bots

Read more

Add Functions

The key to learning how to write flows is add-functions.

Read more

Plugins

Unlimitate and start implementing the community plugins.

Read more

Resources

Modularize

Learn how to modularise flows so that you can have a more maintainable bot.

Send Message

How to send a message via HTTP to start conversations, you can send multimedia as well.

Dockerizer

A good practice is to dockerise your bots to make them more maintainable and effective.

Events

Learning about events will make us more fluent when creating chatbots.