Skip to main content

A step handler library for pyrogram framework

Project description

Pyrostep

Downloads Python Pyrogram

Pyrostep helps you to use pyrogram:

  • step handling
  • new filters may you need
  • change connection timeout, retries, e.g.
  • and other helper methods

I tried to provide the best speed ...

  1. Install
  2. Usage
    1. Step handling
    2. New filters
    3. Connection
    4. Other
  3. TODO

Install

pip3 install -U pyrostep

Usage

before learn, You should know that this library only works as async.

Step handling

import pyrostep.steps first:

from pyrostep import steps

Now see simple example:

@app.on_message(filters.command("start"))
async def step1(c, msg):
    await app.send_message(msg.chat.id, "what is your name?")
    steps.register_next_step(msg.from_user.id, step2)

async def step2(c, msg):
    await app.send_message(msg.chat.id, "your name is: %s" % msg.text)
    steps.unregister_steps(msg.from_user.id)

steps.listen(app)

First we create a function named step1, we want user name. ask user to send name with send_message and set next step for user with register_next_step. after all, we use unregister_steps to remove steps for user.

end of code, you can see steps.listen function. this function listen updates which sends to your client.

you must use listen after all decorators.

Now see ask method, this is make code easy for you. see example:

@app.on_message(filters.command("start"))
async def step1(c, msg):
    await steps.ask(
        c, step2, msg.chat.id, "what is your name?",
        user_id=msg.from_user.id
    )

async def step2(c, msg):
    await app.send_message(msg.chat.id, "your name is: %s" % msg.text)
    steps.unregister_steps(msg.from_user.id)

steps.listen(app)

If you don't like this step handling, can use ask_wait function. see example:

@app.on_message(filters.command("start"))
async def step1(c, msg):
    result = await steps.ask_wait(
        c, step2, msg.chat.id, "what is your name?",
        user_id=msg.from_user.id
    )
    await app.send_message(msg.chat.id, "your name is: %s" % result.text)

steps.listen(app)

Let's not forget the listen_on_the_side function. Use this instead listen method if you have a decorator without any filter.

Example:

@app.on_message() # or @app.on_message(filters.all)
@steps.listen_on_the_side
async def step1(c, msg):
    result = await steps.ask_wait(
        c, step2, msg.chat.id, "what is your name?",
        user_id=msg.from_user.id
    )
    await app.send_message(msg.chat.id, "your name is: %s" % result.text)

New filters

from pyrostep import filters
  • ttl_message: Filter ttl messages ( ttl photo message or ttl video message ).

  • video_sticker: Filter video sticker messages.

  • entities: Filter messages include entities.

  • photo_size: Filter photo messages with width and height.

  • member_of_chats: Filter users who are members of chats.

Connection

Function connection_max_retries:

Change connection max retries. (default 3)

retries message: Unable to connect due to network issues: ...

Return: returns MAX_RETRIES if max_retries is None

Function invoke_max_retries:

Change invoke max retries. (default 5)

retries message: [...] Waiting for ... seconds before continuing (required by "...")

Return: returns MAX_RETRIES if max_retries is None

Function session_start_timeout:

Change start timeout. (default 1)

Return: returns START_TIMEOUT if timeout is None.

Function session_max_retries:

Change session max retries.

retries message: Connection failed! Trying again...

What is mode? TCP Connection mode.

  • TCP Modes:
    • TCPFull
    • TCPAbridged
    • TCPIntermediate
    • TCPAbridgedO
    • TCPIntermediateO

Other

import shortcuts:

from pyrostep import shortcuts

Now see methods:

split_list splites lst list:

>>> shortcuts.split_list([1, 2, 3, 4, 5, 6], 2)
# [[1, 2], [3, 4], [5, 6]]
>>> shortcuts.split_list([1, 2, 3], 2)
# [[1, 2], [3]]

keyboard creates ReplyKeyboardMarkup from your list:

buttons = [
    [
        ["Top Left"], ["Top Right"]
    ],
    [
        ["Bottom | Request Contact", True, "request_contact"]
    ]
]
kb = shortcuts.keyboard(buttons)

inlinekeyboard creates InlineKeyboardMarkup from your list:

buttons = [
    [
        ["Top Left", "data_1"], ["Top Right", "data_2"]
    ],
    [
        ["Bottom", "Your URL", "url"]
    ]
]
ikb = inlinekeyboard(buttons)

validation_channels checks user already in channels or not:

user_id = 56392019
channels = [-102792837, -10823823, 'channel_username']

is_joined = await validation_channels(
    app, user_id, channels
)
# ...
async def invite(app, id, channels) -> None:
    print(
        f"User {id} is not member of channels ({channels})"
    )

is_joined = await validation_channels(
    app, user_id, channels,
    invite_func=invite
)

TODO

  • Add examples
  • Do other tests

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.0.0.tar.gz (10.5 kB view hashes)

Uploaded Source

Built Distribution

pyrostep-2.0.0-py3-none-any.whl (17.5 kB view hashes)

Uploaded Python 3

Supported by

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