Search code examples
python-3.xsshplesktelethon

Telethon client login through the presence of a session file does not work on plesk scheduled task but it runs normaly via SSH


I have a VPS running on plesk (debian 10). I've created a python script for telegram using telethon.

I used ssh to run the script for the first time in order to input the code received on my phone so that it created a .session file and goes without the need of the code for future executions.

The script performed as expected and does not requires a code verification anymore as the .session file is present in the directory.

I tried scheduling a cron task via plesk for the script but it does not work, the script act like it requires a code, and i'm receiving a code verification on my phone, so it is manifesting as if the .session file was not there.

from telethon.sync import TelegramClient
api_id = 'xxxxxx'
api_hash = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
phone = '+1xxxxxxx' #the same as session name: '+1xxxxxxxx.session'

client = TelegramClient('+15812571726', api_id, api_hash)

client.connect()

if not client.is_user_authorized():
    client.send_code_request(phone)
    client.sign_in(phone, input('Enter the code: '))

....do stuff

Does anyone have an idea why is this failing ? Can I load the session before calling the client.connect() so this way i'm sure it is loaded ?

Help is very much appreciated.


Solution

  • the issue is most likely a path issue. try using an absolute path since the session file is created in the folder where the script is called.