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.
𝗖𝝠𝝦𝝩𝝠𝝞𝝥 𝝦𝗥𝝞𝗖𝝽°™️ 🇱🇰 Is An All In One Media Inline Bot Made For Inline Your Media Effectively With Some Advance Security Tools♥️

𝗖𝝠𝝦𝝩𝝠𝝞𝝥 𝝦𝗥𝝞𝗖𝝽° ™️ 🇱🇰 𝗙𝗘𝝠𝝩𝗨𝗥𝗘𝗦 Auto Filter IMDB Admin Commands Broadcast Index IMDB Search Inline Search Random Pics Ids & User I

Kɪꜱᴀʀᴀ Pᴇꜱᴀɴᴊɪᴛʜ Pᴇʀᴇʀᴀ 〄 13 Jun 21, 2022
The purpose of this bot is to take soundcloud track requests, that are posted in the stream-requests channel, and add them to a playlist, to make the process of scrolling through the requests easier for Root

Discord Song Collector Dont know if anyone is actually going to read this, but the purpose of this bot is to check every message in the stream-request

2 Mar 01, 2022
A Python wrapper around the Twitter API.

Python Twitter A Python wrapper around the Twitter API. By the Python-Twitter Developers Introduction This library provides a pure Python interface fo

Mike Taylor 3.4k Jan 01, 2023
A Twitch bot to provide information from the WebNowPlayingCompanion extension

WebNowPlayingTwitch A Twitch bot made using TwitchIO which displays information obtained from the WebNowPlaying Compaion browser extension. Image is o

NiceAesth 1 Mar 21, 2022
Projeto sobre BioInformática - MoA (mecanismos de ação)

Projeto: MoA no Paredawn Projeto sobre Bioinformatica - Mecanismos de Ação (MoA) MODELO PREDITIVO PARA PREVER O ATIVAMENTO DO MOA E MODELO PARA PREVER

Junior Torres 36 Feb 15, 2022
A python script that can send notifications to your phone via SMS text

Discord SMS Notification A python script that help you send text message to your phone one of your desire discord channel have a new message. The proj

2 Apr 25, 2022
Aula-API - a school system widely used in Denmark, as you can see and read about in the python file

Information : Hello, thank you for reading this first of all. This is a Aula-API

Binary.club 2 May 28, 2022
AWS SQS event redrive Lambda

This repository contains the Lambda function to redrive sqs events from source to destination queue while controlling maxRetry per event.

1 Oct 19, 2021
Track live sentiment for stocks from Reddit and Twitter and identify growing stocks

Market Sentiment About This repository can mainly be used for two things. a. Tracking the live sentiment of stocks from Reddit and Twitter b. Tracking

Market Sentiment 345 Dec 17, 2022
A GUI Application that creates a Spotify Playlist from any year in the past, by just entering your preferred date

A GUI Application that creates a Spotify Playlist from any year in the past, by just entering your preferred date

David .K. Danso 1 Jan 17, 2022
Python SDK for IEX Cloud

iexfinance Python SDK for IEX Cloud. Architecture mirrors that of the IEX Cloud API (and its documentation). An easy-to-use toolkit to obtain data for

Addison Lynch 640 Jan 07, 2023
A Telegram Bot which notifies the user when a vaccine is available on CoWin Platform.

Cowin Vaccine Availability Notifier Telegram Bot A bot that notifies the available vaccines at given district in realtime. Introduction • Requirements

Arham Shah 7 Jul 31, 2021
:cloud: Python API for ThePirateBay.

Unofficial Python API for ThePirateBay. Build Status Test Coverage Version Downloads (30 days) Installation $ pip install ThePirateBay Note that ThePi

Karan Goel 334 Oct 21, 2022
DongTai API SDK For Python

DongTai-SDK-Python Quick start You need a config file config.json { "DongTai":{ "token":"your token", "url":"http://127.0.0.1:90"

huoxian 50 Nov 24, 2022
Automatically pulls specified repository whenever a specified file is pushed. Great for working collaboratively when you need to run something locally.

autopull Simple python tool that allows you to automatically pull from a github repository whenever a file with a specified name is uploaded installat

carreb 0 Sep 27, 2022
An API that uses NLP and AI to let you predict possible diseases and symptoms based on a prompt of what you're feeling.

Disease detection API for MediSearch An API that uses NLP and AI to let you predict possible diseases and symptoms based on a prompt of what you're fe

Sebastian Ponce 1 Jan 15, 2022
Bomber-X - A SMS Bomber made with Python

Bomber-X A SMS Bomber made with Python Linux/Termux apt update apt upgrade apt i

S M Shahriar Zarir 2 Mar 10, 2022
Finds Jobs on LinkedIn using web-scraping

Find Jobs on LinkedIn 📔 This program finds jobs by scraping on LinkedIn 👨‍💻 Relies on User Input. Accepts: Country, City, State 📑 Data about jobs

Matt 44 Dec 27, 2022
A Telegram bot for playing Trop Bon Cadavre.

Trop Bon Cadavre A Telegram bot for playing Trop Bon Cadavre (english: very good corpse), a game freely adapted from the french Cadavre exquis. A game

4 Oct 26, 2021
Automated crypto trading bot as adapted from Algovibes.

crypto-trading-bot Automated crypto trading bot as adapted from Algovibes. Pre-requisites Ensure that you have created a Binance API key before procee

Kai Koh 33 Nov 01, 2022