Skip to main content

A monkeypatcher add-on for Pyrogram

Project description

pyroaddon

A monkeypatcher add-on for Pyrogram

Introduction

pyroaddon is a compilation of utils i developed for extend my personal use of Pyrogram. Then i started to use it and more bots and now i published it to make it easier to be installed in new projects. It works together with pyrogram, this is not a fork nor modded version. It does monkey patching to add features to Pyrogram classes.

IMPORTANT: you should have installed asyncio pyrogram.

Usage

Import pyroaddon at least one time in your script, so you'll be able to use modified pyrogram in all files of the same proccess. Example:

# config.py
import pyroaddon.listen
from pyrogram import Client

app = Client('my_session')
# any other .py
from config import app
# no need to import pyroaddon again, pyrogram is already monkeypatched globally (at the same proccess)

I separated the patches between packages to allow you to import only what you want. The __init__.py of each package does the monkeypatch automatically as soon as they are imported (except for pyroaddon.helpers, which provides classes and functions that should be explicitely imported).

pyroaddon.listen

Just import it, it will automatically do the monkeypatch and you'll get these new methods:

  • await pyrogram.Client.listen(chat_id, filters=None, timeout=30) Awaits for a new message in the specified chat and returns it You can pass Update Filters to the filters parameter just like you do for the update handlers. e.g. filters=filters.photo & filters.bot

  • await pyrogram.Client.ask(text, chat_id, filters=None, timeout=30) Same of .listen() above, but sends a message before awaiting You can pass custom parameters to its send_message() call. Check the example below.

  • The bound methods Chat.listen, User.listen, Chat.ask and User.ask

Example:

from pyroaddon import listen # or import pyroaddon.listen
from pyrogram import Client
client = Client(...)
...
    answer = await client.ask(chat_id, '*Send me your name:*', parse_mode='Markdown')
    await client.send_message(chat_id, f'Your name is: {answer.text}')    

pyroaddon.filters

Import it and the following Update Filters will be monkeypatched to pyrogram.filters:

  • filters.dice A dice message.

pyroaddon.nav

Tools for creating navigation keyboards.

  • pyroaddon.nav.Pagination Creates a full pagination keyboard. Usage:
from pyrogram import Client, filters
from pyroaddon.nav import Pagination
from pyroaddon.helpers import ikb

def page_data(page):
    return f'view_page {page}'
def item_data(item, page):
    return f'view_item {item} {page}'
def item_title(item, page):
    return f'Item {item} of page {page}'

@Client.on_message(filters.regex('/nav'))
async def on_nav(c,m):
    objects = [*range(1,100)]
    page = Pagination(
        objects,
        page_data=page_data, # callback to define the callback_data for page buttons in the bottom
        item_data=item_data, # callback to define the callback_data for each item button
        item_title=item_title # callback to define the text for each item button
    )
    index = 0 # in which page is it now?
    lines = 5 # how many lines of the keyboard to include for the items
    columns = how many columns include in each items' line
    kb = page.create(index, lines, columns)
    await m.reply('Test', reply_markup=ikb(kb))

pyroaddon.helpers

Tools for creating inline keyboards a lot easier.

  • pyroaddon.helpers.ikb Creates a inline keyboard. It's first and only argument must be a list (the keyboard) containing lists (the lines) of buttons. The buttons can also be lists or tuples. I use tuples to not have to deal with a lot of brackets. The button syntax must be this: (TEXT, CALLBACK_DATA) or (TEXT, VALUE, TYPE), where TYPE can be 'url' or any other supported button type and VALUE is its value. This syntax will be converted to {"text": TEXT, TYPE: VALUE). If TYPE is CALLBACK_DATA, you can omit it, just like the fist syntax above. Examples:
from pyroaddon.helpers import ikb
...
keyboard = ikb([
    [('Button 1', 'call_1'), ('Button 2', 'call_2')],
    [('Another button', 't.me/pyroaddon', 'url')]
])
await message.reply('Test', reply_markup=keyboard)
  • pyroaddon.helpers.array_chunk Chunk the elements of a list into small lists. i.e. [1, 2, 3, 4] can become [[1,2], [3,4]]. This is extremely useful if you want to build a keyboard dinamically with more than 1 column. You just put all buttons together in a list and run:
lines = array_chunk(buttons, 2) # generate a list of lines with 2 buttons on each
keyboard = ikb(lines)

Copyright & License

This project may include snippets of Pyrogram code

Licensed under the terms of the GNU Lesser General Public License v3 or later (LGPLv3+)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyroaddon-0.1.0.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyroaddon-0.1.0-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

Details for the file pyroaddon-0.1.0.tar.gz.

File metadata

  • Download URL: pyroaddon-0.1.0.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pyroaddon-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4185737fd7997850960739d6f8454054ac848902bc4811075fb8d36a08e3668e
MD5 9103bb117caadcda30e7c3c2e1609137
BLAKE2b-256 2a5f8d9e76ba7dd4f883c78892abe88321eb8e71f26f6199b58fc1a78015c03f

See more details on using hashes here.

File details

Details for the file pyroaddon-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyroaddon-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 25.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pyroaddon-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb0dc83194f9b981e64f95bcd29609589a9a675fe2daa8e7158cbd2c40165bc7
MD5 1815c9c1cd6a2a9ba8d888a85a9e8e2d
BLAKE2b-256 074bc6830143a5c8c6d5f843b73dfa686f653a94925af5c876566fe6072d5f37

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page