🚀 ¡Nuevo! builderbot cloud para No-code ¡Pruébalo gratis!

Fast Entires

Issue

Sometimes it happens that people are impatient and write independent messages in a very short time gap preventing the bot to answer, which makes that each message is answered but not in the desired order.


Possible Solution

For this other type of environments you can implement a functionality which you can create to have a margin of 3000ms for the user to write a message, and each time he writes a message in a time less than 3000ms it will accumulate all the messages and then after the margin time the bot will interpret everything as a single conversation.

Applying this implementation, what is achieved is that before passing to the processing stage, all independent messages (3) become one (1) and are processed as an independent message.

In this example we say 3000ms which is equal to 3 seconds but you can modify this to your liking in MESSAGE_GAP_SECONDS

interface Message {
    text: string;
    timestamp: number;
}

const messageQueue: Message[] = [];

const MESSAGE_GAP_SECONDS = 3000;

let messageTimer: NodeJS.Timeout | null = null;

/**
* Adds a message to the queue for later processing.
* @param messageText The text of the message to add to the queue.
* @returns A promise that resolves when the message queue is processed.
*/
async function enqueueMessage(messageText: string): Promise<string> {
    messageQueue.push({ text: messageText, timestamp: Date.now() });

    return new Promise((resolve) => {
        if (messageTimer) {
            clearTimeout(messageTimer);
        }

        messageTimer = setTimeout(() => {
            resolve(processMessageQueue());
        }, MESSAGE_GAP_SECONDS);
    });
}

/**
* Processes the message queue by combining all messages into a single string and clearing the queue.
* @returns The combined string of all messages in the queue.
*/
function processMessageQueue(): string {
    if (messageQueue.length === 0) {
        return '';
    }

    const combinedMessage = messageQueue.map(message => message.text).join(" ");
    messageQueue.length = 0;
    return combinedMessage;
}

export { enqueueMessage, processMessageQueue };

Remember that this is an alternative solution, and it is possible that its implementation could be improved.


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.