Throttle and debounce add-on for Pyrogram

Overview

pyrothrottle

Throttle and debounce add-on for Pyrogram

Quickstart

implementation on decorators

from pyrogram import Client, filters
from pyrogram.types import Message
from pyrothrottle.decorators import personal_throttle

client = Client('client')

@client.on_message(filters.incoming & filters.text)
@personal_throttle(3)
def handler(c: Client, m: Message):
    m.reply_text(f'Message processed. You can send next in {m.request_info.interval} seconds')

@handler.on_fallback
def fallback_handler(c: Client, m: Message):
    m.reply_text(f'Too fast. Write me after {m.request_info.cooldown} seconds')

client.run()

implementation on filters

from pyrogram import Client, filters
from pyrogram.types import Message
from pyrothrottle.filters import personal_throttle

client = Client('client')
throttle = personal_throttle(3)

@client.on_message(filters.incoming & filters.text & throttle.filter)
def handler(c: Client, m: Message):
    m.reply_text(f'Message processed. You can send next in {m.request_info.interval} seconds')

@throttle.on_fallback
def fallback_handler(c: Client, m: Message):
    m.reply_text(f'Too fast. Write me after {m.request_info.cooldown} seconds')

Docs

First of all, I have to mention that package has two implementations (each was shown in Quickstart section), so, each type of antispam system would have two equal named classes, one in .filters subpackage, and one in .decorators subpackage.
Also, for convinient usage, every class (when package is initialised) named in snake case (But in declaration they're named in camel case as it should be). So, in documentation they will be named as usual classes (for example, PersonalDebounce), but in code you have to use snake case names (for example, personal_debounce).

Meaningful part

In order to choice right system, you just need to undestand 5 terms.

  • Global
    Global in class name means that chosen system would have common for all users counter.
  • Personal
    Personal in class name means that chosen system would have separate counters for each user.
  • Throttle
    Throttle system counts interval between now and last processed (not last received) event. If this interval equals to or greater than given, event would be processed. Only interval is mandatory parameter.
  • Debounce
    Debounce system counts interval between now and last received event. If this interval equals to or greater than given, event would be processed. Only interval is mandatory parameter.
  • ReqrateController
    ReqrateController system counts, how many events were processed for last interval of time with length of provided interval (from some time point till now). If amount of processed events less than given allowed amount, event would be processed. Have 2 mandatory parameters: interval and amount.

In every class name first goes scope (Global or Personal), and then technique name (for example, PersonalDebounce).

Full API explanation

Classes

class pyrothrottle.decorators.GlobalThrottle

class pyrothrottle.filters.GlobalThrottle

Parameters:

  • interval(int|float) — Interval between successfully processed events. Since it's Throttle, system would pass any event, if interval between now and last processed (not last received) event would equals to or be greater than given interval. Because it's Global, system wound have common for all users counter.
  • falback (callable, optional) — Function that will be called if passed not enough time between events. Must accept two positional arguments (client, event).

class pyrothrottle.decorators.PersonalThrottle

class pyrothrottle.filters.PersonalThrottle

Parameters:

  • interval(int|float|callable) — Interval between successfully processed events. If callable passed, it must accept one positional argument (user_id) and return int or float. Since it's Throttle, system would pass an event, if interval between now and last processed (not last received) event would equals to or be greater than given interval. Because it's Personal, system wound have separate counters for each user.
  • falback (callable, optional) — Function that will be called if passed not enough time between events. Must accept two positional arguments (client, event).

class pyrothrottle.decorators.GlobalDebounce

class pyrothrottle.filters.GlobalDebounce

Parameters:

  • interval(int|float) — Interval between successfully processed events. Since it's Debounce, system would pass an event, if interval between now and last received event would equals to or be greater than given interval. Because it's Global, system wound have common for all users counter.
  • falback (callable, optional) — Function that will be called if passed not enough time between events. Must accept two positional arguments (client, event).

class pyrothrottle.decorators.PersonalDebounce

class pyrothrottle.filters.PersonalDebounce

Parameters:

  • interval(int|float|callable) — Interval between successfully processed events. If callable passed, it must accept one positional argument (user_id) and return int or float. Since it's Debounce, system would pass an event, if interval between now and last received event would equals to or be greater than given interval. Because it's Personal, system wound have separate counters for each user.
  • falback (callable, optional) — Function that will be called if passed not enough time between events. Must accept two positional arguments (client, event).

class pyrothrottle.decorators.GlobalReqrateController

class pyrothrottle.filters.GlobalReqrateController

Parameters:

  • interval(int|float) — Interval between successfully processed events. Since it's ReqrateController, system would pass an event, if amount of processed for last interval of time with length of provided interval events less that given allowed amount. Because it's Global, system wound have common for all users counter.
  • amount(int) — Allowed amount of processed requests during given interval.
  • falback (callable, optional) — Function that will be called if passed not enough time between events. Must accept two positional arguments (client, event).

class pyrothrottle.decorators.PersonalReqrateController

class pyrothrottle.filters.PersonalReqrateController

Parameters:

  • interval(int|float|callable) — Interval between successfully processed events. If callable passed, it must accept one positional argument (user_id) and return int or float. Since it's ReqrateController, system would pass an event, if amount of processed for last interval of time with length of provided interval events less that given allowed amount. Because it's Personal, system wound have separate counters for each user.
  • amount(int|callable) — Allowed amount of processed requests during given interval. If callable passed, it must accept one positional argument (user_id) and return int.
  • falback (callable, optional) — Function that will be called if passed not enough time between events. Must accept two positional arguments (client, event).

Decorators

Decorators intended to use in next way:

@client.on_event(...) # i.e. on_message, on_callback_query, etc.
@personal_throttle(3) # I'll use personal_throttle for examples
def handler(c: Client, e: Event):
    ...

If you want to add fallback handler to your system, you have to use .on_fallback (this method would contain in variable named as function that you registered as handler) as decorator. Fallback function must accept two positional arguments (same arguments as provided to main handler)

@handler.on_fallback
def fallback_handler(c: Client, e: Event):
    ...

Please note: Event objects (i.e. Message, CallbackQuery or InlineQuery) are patched, so they have have attribute request_info with usefull info (more on RequestInfo class later).

Filters

First of all, I have to mention that filter itself contained in filter attribute. Filters have 2 major ways to use: normal and anonymous.

Normal use

throttle = personal_throttle(3)

@client.on_event(different_filters & throttle.filter) # i.e. on_message, on_callback_query, etc.
def handler(c: Client, e: Event):
    ...

@throttle.on_fallback
def fallback_handler(c: Client, e: Event):
    ...

So, instead of decorators, when using filters (in normal way), .on_fallback must be called from antispam system instance

Anonymous use

@client.on_event(different_filters & personal_throttle(3).filter) # i.e. on_message, on_callback_query, etc.
def handler(c: Client, e: Event):
    ...

So, comparing ways to use, the advantage of normal use is that you can add fallback using .on_fallback, while main advantage of anonymous usage is absence of necessity to create named instance what gives us less code. You still can specify fallback when creating anomyous instance

def fallback_handler(c: Client, e: Event):
    ...

@client.on_event(different_filters & personal_throttle(3, fallback_handler).filter)
def handler(c: Client, e: Event):
    ...

Please note: Event objects (i.e. Message, CallbackQuery or InlineQuery) are patched, so they have attribute request_info with usefull info (more on RequestInfo class later).

RequestInfo

So, as it was mentioned before, all incoming events are patched, so they have attribute request_info with RequestInfo instance.

class pyrothrottle.RequestInfo

Attributes:

  • time(float) — timestamp of the moment when the event got into antispam system.
  • last_processed(float|list) — timestamp (or list of timestamps) of last processed event(s).
  • next_successful(float) — timestamp, when incoming event would be processed.
  • interval(int|float) — user-defined interval for antispam system.
  • amount(int, optional) — user-defined amount of events that should be processed during interval (only in ReqrateController)
  • cooldown(float) — amount of time till now to next successful processed event.
Slack bot to automatically delete yubisneeze / accidental yubikey presses

YubiSnooze Slack bot to automatically delete yubisneeze / accidental yubikey presses. It will search using the regex "[cbdefghijklnrtuv]{44}" and if t

Andrew MacPherson 3 Feb 09, 2022
Discord Token Generator - Python (Generates Tokens and Joins your Server Automatically) hCaptcha Bypass **FREE**

Best Discord Token Generator {hCaptcha bypass FREE Unlimited Memberboost} Install few requirements & run main.py it will redirect you to the Download

1 Oct 27, 2021
Program that uses Python to monitor grade updates in the Genesis Platform

Genesis-Grade-Monitor Program that uses Python to monitor grade updates in the Genesis Platform Guide: Install by either cloning the repo or downloadi

Steven Gatanas 1 Feb 12, 2022
Fetch Flipkart product details including name, price, MRP and Stock details in general as well as specific to a pincode

Fetch Flipkart product details including name, price, MRP and Stock details in general as well as specific to a pincode

Vishal Das 6 Jul 11, 2022
A head unit UI designed to replace the RTx/SMEG/RNEG/NG4/RCC/NAC

HeadUnit UI (Come discuss about it on our Discord!) Intro This is the UI part of a headunit project from OpenLeo, based on python and kivy, it looks l

OpenLeo 6 Nov 23, 2022
A simple API wrapper for the Tenor API

Gifpy A simple API wrapper for the Tenor API Installation Python 3.9 or higher is recommended python3 -m pip install gifpy Clone repository: $ git cl

Juan Ignacio Battiston 4 Dec 22, 2021
Estimate the total emissions for popular CryptoArt platforms.

cryptoart-footprint Estimate the total CO2 footprint for popular CryptoArt platforms. The goal is to accurately quantify the ecological damage of Ethe

Kyle McDonald 182 Oct 12, 2022
bot for hearthstone mercenaries

Hearthstone-Mercenaries-game-bot - prevention: Bot is not ready and now on the development stage estimated release date - 21.10.21 The main idea of th

Andrew Efimov 59 Dec 12, 2022
Simple Instagram Login Link Generator

instagram-account-login Simple Instagram Login Link Generator Info Program generates instagram login links and you may get into someone´s thought the

Kevin 5 Dec 03, 2022
A bot to get Statistics like the Playercount from your Minecraft-Server on your Discord-Server

Hey Thanks for reading me. Warning: My English is not the best I have programmed this bot to show me statistics about the player numbers and ping of m

spaffel 12 Sep 24, 2022
Python Script to download hundreds of images from 'Google Images'. It is a ready-to-run code!

Google Images Download Python Script for 'searching' and 'downloading' hundreds of Google images to the local hard disk! Documentation Documentation H

Hardik Vasa 8.2k Jan 05, 2023
Asynchronous Python Wrapper for the GoFile API

Asynchronous Python Wrapper for the GoFile API

Gautam Kumar 22 Aug 04, 2022
Documentation and Samples for the Official HN API

Hacker News API Overview In partnership with Firebase, we're making the public Hacker News data available in near real time. Firebase enables easy acc

Y Combinator Hacker News 9.6k Jan 03, 2023
Kakatua discord music bot

Donate Ayo donasi! Lokal Internasional Ucapan Terima Kasih Tentu saja, donatur Bunga dan talent-talent h!mawari. Semoga rezeki teman-teman semakin lan

1 Oct 30, 2021
Python interface to the LinkedIn API

Python LinkedIn Python interface to the LinkedIn API This library provides a pure Python interface to the LinkedIn Profile, Group, Company, Jobs, Sear

ozgur 844 Dec 27, 2022
Jackrabbit Relay is an API endpoint for stock, forex and cryptocurrency exchanges that accept REST webhooks.

JackrabbitRelay Jackrabbit Relay is an API endpoint for stock, forex and cryptocurrency exchanges that accept REST webhooks. Disclaimer Please note RA

Rose Heart 23 Jan 04, 2023
A modular Telegram Python bot running on python3 with a sqlalchemy database.

Nao Tomori Robot Found Me On Telegram As Nao Tomori 🌼 A modular Telegram Python bot running on python3 with a sqlalchemy database. How to setup/deplo

Stinkyproject 1 Nov 24, 2021
An anime themed telegram bot that can convert telegram media.

ShoukoKomiRobot • 𝕎𝕣𝕚𝕥𝕥𝕖𝕟 𝕀𝕟 Python3 • 𝕃𝕚𝕓𝕣𝕒𝕣𝕪 𝕌𝕤𝕖𝕕 Pyrogram • 𝕊𝕠𝕗𝕥𝕨𝕒𝕣𝕖 𝕌𝕤𝕖𝕕 Ebook-convert Deploy 𝔽𝕠𝕣𝕜 𝕥𝕙𝕚𝕤 𝕣

25 Aug 14, 2022
Simple Webhook Spammer with Optional Proxy Support

😎 �Simple Webhook Spammer with Optional Proxy Support:- [+] git clone https://g

Terminal1337 12 Sep 29, 2022
Automate saving your Discover Weekly Playlist using Python.

SpotWeekly Automate saving your Discover Weekly Playlist using Python. Made with 3 and FastAPI. The saved playlist link is sent to my discord server

shourya 6 Jan 03, 2022