Diego T. P.
Diego T. P.

Reputation: 43

How to manage user inputs in a short time interval?

i would like to implement a way of managing a user sending many messages in a time interval (for example 3 seconds), so that the chatbot only responds to the last one.

Example of inputs (in a gap of 3 seconds):

-Hi -Hi -Hi -Help Result: The chatbot only responds to the Help message.

Thanks in advance.

Upvotes: 0

Views: 69

Answers (1)

Gary Liu
Gary Liu

Reputation: 13918

You can leverage Middleware feature to intercept every message, with which you can store every user's every message in cache, when your bot receive a new message, you can compaire with those info in cache, then dicide whether the flow needs to go forward.

Npde.js code snippet for quick test:

const moment = require('moment');
let lastMessage = null;
let lastMessageTime = null;
bot.use({
    receive: (session, next) => {
        let currentMessage = session
        if (currentMessage.text !== lastMessage) {
            lastMessage = currentMessage.text;
            lastMessageTime = currentMessage.timestamp;
            next();
        } else {
            if (moment(currentMessage.timestamp) - moment(lastMessageTime) >= 3000) {
                lastMessageTime = currentMessage.timestamp;
                next();
            } 
        }
    }
})

What needs you paying attention is that, in production env, you need to store the message with session/user id. E.G. Using session/user id as prefix of message and timesamp key in cache.

Please refer to https://learn.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-middleware for how to intercept messages in C#, and refer to https://learn.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-intercept-messages for Node.js version.

Hope it helps.

Upvotes: 1

Related Questions