Search code examples
pythonsessiontelethon

Telethon change session in @client


I want to change my sessions while being in @client if an exception happens. How exactly do I do that? Ive tried a few ideas but none of them worked out.

For Example:

api_id = 123456789
api_hash = 'apihash'
CHANNEL = [-100123456789]
session= ['phone number', 'phone number2']
msg['mymessage']
client = TelegramClient(session, api_id, api_hash)

@client.on(events.NewMessage(CHANNEL))
async def main(event):
    try:
        await client.send_message(event.chat_id, msg, comment_to=event.id)
    except:
        client.disconnect()
        session+= 1
        client.start()


client.start()
client.run_until_disconnected()

This didnt work out. Any ideas? Thanks in advance!


Solution

  • You need to create a new TelegramClient if you want to use a different session. This means the new client would need to register all the handlers again, too.