0

I'm currently working on developing a Telegram API bot using Pyrogram, but I've encountered a problem that I'm struggling to fix. The objective is to collect a phone number from the user, save it, establish a session with that number, and request a login code. In the next step, the user needs to send their login code. However, with every attempt to create a client, I receive a new code, making it challenging to maintain an incomplete session to retrieve the login code.

Here's a snippet of my code:


# Function to create and initialize the Pyrogram client
async def create_and_initialize_client(api_id, api_hash, phone_number):
    # Provide a unique name for your client
    client: Client = Client(phone_number, api_id, api_hash, phone_number=phone_number)
    client.start()
    
async def continue_session(api_id, api_hash, phone_number, login_code):
    client: Client = Client(phone_number, api_id, api_hash, phone_number=phone_number)
    # Add code for continuing the session

I also face a secondary issue: I can't find documentation explaining how to pass the login code into the session. Any guidance or assistance on these matters would be greatly appreciated.

Thank you in advance!

What I've Tried:

I've attempted to create and initialize the Pyrogram client using the provided code snippets. I expected that the client would retain the session across multiple attempts, allowing me to receive the login code for the ongoing session. However, each time I create a new client, a new code is generated, preventing the establishment of a continuous session.

Expected Outcome:

Ideally, I would like to understand how to create a client that maintains the session state so that I can successfully receive the login code within the same session. Additionally, guidance on passing the login code into the session would be greatly appreciated.

1
  • Telegram will invalidate any login codes sent by the user it's for. So when a user sends your bot their login token, it'll be immediately invalid and not work anymore. Commented Dec 10, 2023 at 16:28

1 Answer 1

0

If your question is about not knowing how to enter the code after you've requested it, then you should pay attention to the following functionality:

from pyrogram import Client


async def main():
    api_id = YOUR_API_ID
    api_hash = "YOUR_API_HASH"
    phone_number = "YOUR_PHONE_NUMBER"
    client = Client(":memory:", api_id, api_hash)
    await client.connect()
    sent_code_info = await client.send_code(phone_number)
    phone_code = input("Please enter your phone code: ")  # Sent phone code using last function
    await client.sign_in(phone_number, sent_code_info.phone_code_hash, phone_code)

However, if you're unclear why the code sent by a user in the Telegram bot isn't working, I must disappoint you that such functionality cannot be implemented. This issue was addressed by a user in the comments under the post, stating that the code becomes inactive immediately.

It's possible to implement a workaround if you're familiar with web development. You can issue a link for users to enter the code, generating a new link for each user. This method can circumvent the issue, but it depends on the tasks you want to accomplish.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.