Pyrogram客户端启动错误(sqlite)

gzszwxb4  于 2023-10-23  发布在  SQLite
关注(0)|答案(2)|浏览(119)

我在启动pyrogram客户端时遇到问题。
我在https://my.telegram.org/中创建了一个应用程序,并从那里复制了creds。
现在我开始客户端:

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()

它在字符串client.start()处引发错误
错误说:

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

你能帮我找个理由吗?

cld4siwp

cld4siwp1#

所以我的问题是,我在开始会话之前在应用程序(django)中做了这件事。因此,如果你看到这个问题,你应该用你的creds手动启动一个会话(从终端,python控制台等):

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()

保存会话字符串后,您可以运行应用程序,只需在客户端中传递参数session_string

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')
)
dxpyg8gm

dxpyg8gm2#

start和stop方法是异步的。尝试使用with语句:

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')

相关问题