Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to add request limitations #23

Closed
Deplyapp opened this issue Feb 8, 2024 · 3 comments
Closed

Is it possible to add request limitations #23

Deplyapp opened this issue Feb 8, 2024 · 3 comments
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers not planned Something not planned

Comments

@Deplyapp
Copy link

Deplyapp commented Feb 8, 2024

The gemini free api Provides can it is possible to add a message limit of 3 mer minute per person.

@Zain-ul-din
Copy link
Owner

Zain-ul-din commented Feb 8, 2024

yes, you can if you know how to write code.

this is place from where you can start https://github.com/Zain-ul-din/whatsapp-ai-bot/blob/master/src/models/GeminiModel.ts

Basic Overview:

  • use msg.from to get sender id and then construct map using sender id
  • increment map value each time someone send request
  • allow request only if elapsed time is greater than 1 min or map value is under 3
const useLimitedRequests = (timeInMs, requestAllowed) => {
  const map = {};

  return (sender) => {
    if (map[sender] == undefined) {
      map[sender] = {
        count: 0,
        lastRequestTime: new Date(),
      };
    }
    
    const { count, lastRequestTime } = map[sender];
    const elapsedTime = new Date() - lastRequestTime;

    if (elapsedTime >= timeInMs) {
      map[sender] = { count: 1, lastRequestTime: new Date() };
      return true;
    }

    if (count >= requestAllowed) {
      return false;
    }

    map[sender] = { count: count + 1, lastRequestTime };
    return true;
  };
};


// usage
const isUserAllowed = useLimitedRequests(60000, 3) // 60000ms = 1min

if(!isUserAllowed(msg.from)) return; // quota exceed

from string- ID for the Chat that this message was sent to, except if the message was sent by the current user.

Appendix

Whatspp Js Docs link: https://docs.wwebjs.dev/Message.html

@Zain-ul-din Zain-ul-din self-assigned this Feb 8, 2024
@Zain-ul-din Zain-ul-din added documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers labels Feb 8, 2024
@vpsbykaif
Copy link

I can paste this code in gemini.ts to get the function

@zain-ul-din-zafar
Copy link

I added a rate-limiting feature to this branch but it has not been tested yet. check this out

https://github.com/zainuldeen/whatsapp-ai-bot/tree/issue_23

@Zain-ul-din Zain-ul-din changed the title Can it is possible to add limitations Feb 9, 2024
@Zain-ul-din Zain-ul-din changed the title Is it possible to add limitations Feb 9, 2024
@Zain-ul-din Zain-ul-din added the not planned Something not planned label Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers not planned Something not planned
4 participants