0

I have a problem with starting pyrogram Client.

I've created an app in https://my.telegram.org/ and copied creds from there.
Now im starting client with:

from pyrogram import Client


client = Client(
    "occasion",
    api_id=os.environ.get('TG_API_ID'), 
    api_hash=os.environ.get('TG_API_HASH')
)

client.start()
client.send_message('me', 'some_message_text')
client.stop()

And it raises an error at the string client.start()
And error says:

Traceback (most recent call last):
  File "C:\Users\Artur_Epremian\AppData\Local\Programs\Python\Python310\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "C:\Users\Artur_Epremian\AppData\Local\pypoetry\Cache\virtualenvs\occasion-back-UNW6TZnG-py3.10\lib\site-packages\pyrogram\sync.py", line 66, in async_to_sync_wrap
    return loop.run_until_complete(coroutine)
  File "C:\Users\Artur_Epremian\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 646, in run_until_complete
    return future.result()
  File "C:\Users\Artur_Epremian\AppData\Local\pypoetry\Cache\virtualenvs\occasion-back-UNW6TZnG-py3.10\lib\site-packages\pyrogram\methods\utilities\start.py", line 58, in start
    is_authorized = await self.connect()
  File "C:\Users\Artur_Epremian\AppData\Local\pypoetry\Cache\virtualenvs\occasion-back-UNW6TZnG-py3.10\lib\site-packages\pyrogram\methods\auth\connect.py", line 40, in connect
    await self.load_session()
  File "C:\Users\Artur_Epremian\AppData\Local\pypoetry\Cache\virtualenvs\occasion-back-UNW6TZnG-py3.10\lib\site-packages\pyrogram\client.py", line 564, in load_session
    await self.storage.open()
  File "C:\Users\Artur_Epremian\AppData\Local\pypoetry\Cache\virtualenvs\occasion-back-UNW6TZnG-py3.10\lib\site-packages\pyrogram\storage\file_storage.py", line 58, in open
    self.conn = sqlite3.connect(str(path), timeout=1, check_same_thread=False)
sqlite3.OperationalError: unable to open database file

Can you please help me to find what is a reason?

2
  • I bet that at least one of these env vars is empty or just wrong Commented Jun 13, 2022 at 13:23
  • what coult it be? All the creds are right. And it ran right without pyrogram with just tg api Commented Jun 13, 2022 at 13:26

2 Answers 2

0

So my problem was that I did it inside the app (django) before I started a session. So if you see this problem you should start a session manually (from terminal, python console, etc.) with your creds:

client = Client('name', api_id='', api_hash='')
client.start()  # here it will ask you to enter your phone number and confirmation code
client.export_session_string() 

# ->  and here you will get a string, save it in credentials too

client.stop()

And after you saved the session string you can run your app, you just have to pass parameter session_string in Client

client = Client(
    "occasion",
    api_id=os.environ.get('TG_API_ID'), 
    api_hash=os.environ.get('TG_API_HASH'),
    session_string=os.environ.get('TG_SESSION_STRING')
)
Sign up to request clarification or add additional context in comments.

Comments

0

start and stop methods are asynchronous. Try using the with statement:

from pyrogram import Client

client = Client(
    "occasion",
    api_id=os.environ.get('TG_API_ID'), 
    api_hash=os.environ.get('TG_API_HASH')
)

with client:
  client.send_message('me', 'some_message_text')

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.