DEV Community

Parvat R
Parvat R

Posted on

2 1

New to Pyrogram? Take a look here.

Define a client in different way:

/main.py:

import pyrogram

client = pyrogram.Client("Session-Name")
Enter fullscreen mode Exit fullscreen mode

/config.ini:

[pyrogram]
api_id=1234567
api_HASH=1108f129f7afa345076448421e535bbd
bot_token=1234567890:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Enter fullscreen mode Exit fullscreen mode

You can get your api_id, api_hash from telegram. And, you can get the token from @botfather


Do long time process, without disturbing the bot

import _thread

def some_long_process(param1, param2):
    # do something that takes long time
    param2.reply_text('Process Over.')
    ...


@client.on_message(
    pyrogram.filters.command('do_big')
)
def do_big(_c, message):
    _thread.start_new_thread(some_long_process, (_c, message))
Enter fullscreen mode Exit fullscreen mode

You can pass on the client and the message parameter to some function, which takes a long time to complete, and has to notify the user when it completes. During this process, some times the bot might stop responding to other users, or become slow.

So, use _thread module to start a new thread which runs in background without disturbing your bot, and making it faster to the core.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay