Skip to main content

A Python library to handle steps in pyrogram framework.

Project description

pyrostep

Downloads

You can implement conversation in your project based on pyrogram very easy with pyrostep.

Features:

  • Full type hint
  • Support step handling
  • Support asking
  • Change core settings of pyrogram

Installing

pip3 install -U pyrostep

Content

Tutorial

Use pyrostep.listen() to start listening on your client:

from pyrogram import Client
import pyrostep

# ...

client = Client("myaccount")
pyrostep.listen(client)

[!NOTE]
Always use pyrostep.listen() before adding your handlers.

After that, we have two ways to make conversation: wait_for method or step handling


Wait for method

⏰ In this way we can use pyrostep.wait_for function, that waits for an update (e.g message) from your target.

client = Client("myaccount")
pyrostep.listen(client)

# ...

@client.on_message()
async def get_name(_, message):
    await message.reply("Send your name?")
    
    answer = await pyrostep.wait_for(message.from_user.id)
    await message.reply(f"Your name is {answer.text}")

[!TIP]
You can specify how long it will wait with timeout parameter. see this example:

client = Client("myaccount")
pyrostep.listen(client)

# ...

@client.on_message()
async def get_name(_, message):
    await message.reply("Send your name?")

    try:
        answer = await pyrostep.wait_for(message.from_user.id, timeout=10)
    except TimeoutError:
        await message.reply("Timed out")
    else:
        await message.reply(f"Your name is {answer.text}")

🔗 Related functions:

  • clear(): remove all registered steps (and cancels all wait_for's).

Step handling

🎯 In this way we will use this functions:

  • pyrostep.register_next_step()

We will specify a function that should process the next update from the target with pyrostep.register_next_step().

[!IMPORTANT]
In this way we cannot specify a timeout.

client = Client("myaccount")
pyrostep.listen(client)

# ...

@client.on_message()
async def get_name(_, message):
    await message.reply("Send your name?")
    await pyrostep.register_next_step(
        message.from_user.id, get_age
    )

async def get_age(_, message):
    await message.reply("OK, Send your age?")
    await pyrostep.register_next_step(
        message.from_user.id,
        say_info,
        kwargs={"name": message.text}
    )

async def say_info(_, message, name: str = None):
    await message.reply(f"Name: {name} - Age: {message.text}")

🔗 Related functions:

  • unregister_steps(id): remove registered step for id.
  • clear(): remove all registered steps (and cancels all wait_for's).

Plugins

📁 If you're using plugins in pyrogram, maybe you cannot use pyrostep.listen(), so you can use pyrostep.listening_handler function.

How? there's an example:

# plugin file
from pyrogram import Client
import pyrostep

Client.on_message()
async def stephandler(client, message):
    await pyrostep.listening_handler(client, message)

# your other handlers

[!WARNING]
We didn't test it completely.

Shortcuts

✂️ pyrostep have some shortcuts and shorthands for you.

pyrostep.shortcuts.split_list()

split_list splites your list.

example:

>>> from pyrostep import shortcuts
>>> split_list([1, 2, 3, 4, 5, 6], 2)
[[1, 2], [3, 4], [5, 6]]
>>> split_list([1, 2, 3], 2)
[[1, 2], [3]]

pyrostep.shortcuts.keyboard()

keyboard creates ReplyKeyboardMarkup from your list.

example:

>>> from pyrostep import shortcuts
>>> buttons = [
...     [["Top Left"], ["Top Right"]],
...     [["Bottom | Request Contact", True, "request_contact"]]
... ]
>>> shortcuts.keyboard(buttons)
ReplyKeyboardMarkup(keyboard=[[KeyboardButton(text='Top Left'), KeyboardButton(text='Top Right')], [KeyboardButton(text='Bottom | Request Contact', request_contact=True)]])

pyrostep.shortcuts.inlinekeyboard()

inlinekeyboard creates InlineKeyboardMarkup from your list.

example:

>>> from pyrostep import shortcuts
>>> buttons = [
...     [["Top Left", "data_1"], ["Top Right", "data_2"]],
...     [["Bottom", "Your URL", "url"]]
... ]
>>> shortcuts.inlinekeyboard(buttons)
InlineKeyboardMarkup(inline_keyboard=[[InlineKeyboardButton(text='Top Left', callback_data='data_1'), InlineKeyboardButton(text='Top Right', callback_data='data_2')], [InlineKeyboardButton(text='Bottom', url='Your URL')]])

pyrostep.shortcuts.validation_channels()

validation_channels checks user is already in channels or not. returns True if user is already in channels, returns False otherwise.

example:

>>> from pyrostep import shortcuts
>>> user_id = 56392019
>>> channels = [-10279279837, -10823827873, 'channel_username']
>>> await validation_channels(app, user_id, channels)
True

connection package

This package helps you to change pyrogram connection settings.

[!NOTE]
All of these functions should be used before importing pyrogram

pyrostep.connection.connection_max_retries()

How many times does it try to connect (to proxy or telegram)?


pyrostep.connection.invoke_max_retries()

How many times does it try to invoke a method?


pyrostep.connection.session_start_timeout()

How many seconds to wait for connection?


pyrostep.connection.session_max_retries()

How many times does it try to authenticate?


Example:

from pyrostep import connection
connection.connection_max_retries(3)
connection.invoke_max_retries(3)
connection.session_start_timeout(20)
connection.session_max_retries(2)

from pyrogram import Client
# code ...

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

pyrostep-2.10.21.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

pyrostep-2.10.21-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file pyrostep-2.10.21.tar.gz.

File metadata

  • Download URL: pyrostep-2.10.21.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for pyrostep-2.10.21.tar.gz
Algorithm Hash digest
SHA256 0ab106dc0be8336faa8def6f25b1234e203ffc55fca243b3534452a9c48c2d40
MD5 2a87833d95ea41341df2046b4d714675
BLAKE2b-256 e831064ccbe10fd951bcea3dff414cd8287d7b264088cf8125a1f8555bb75d6f

See more details on using hashes here.

File details

Details for the file pyrostep-2.10.21-py3-none-any.whl.

File metadata

  • Download URL: pyrostep-2.10.21-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for pyrostep-2.10.21-py3-none-any.whl
Algorithm Hash digest
SHA256 7d91fe1c08c45925beca33d1078a71a52fd66f82a2b39100f357187d6078bc32
MD5 a3ffdbfbc5e17fc289c2b2e836b07f54
BLAKE2b-256 ae785bfa131ae3c73986adbfe3065abf970ed01857384dc08e6a0790a1e8aba8

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