LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!

Home Page:https://docs.telethon.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Best practice with needing Bot in multiple places

Skowt opened this issue · comments

Description

It's unclear what the best practice is for needing a bot client in multiple places. I've got a Bot that I establish connection with using:

telethon = TelegramClient('bot', API_ID, API_HASH)
await telethon.start(bot_token=BOT_TOKEN)
telethon.add_event_handler(event_debugger, events.NewMessage())
await telethon.run_until_disconnected()

This works on its own. Then I've got two other functions that run that aren't related to event handlers and are located in other modules but also need to send Telegram messages.

Example code in another module, which errors out because the DB is locked by the other function:

def send_error_message(msg):
telethon = TelegramClient('bot', API_ID, API_HASH)
telegram.send_error_message(telethon, message)

I'm wondering if the best practice is to start 3 sessions, e.g. telethon = TelegramClient('bot2', API_ID, API_HASH) and telethon = TelegramClient('bot3', API_ID, API_HASH) to be able to send messages in those other places, even if they're running in the same process.

Alternatively is there a way to check for a running TelegramClient and reuse it from other parts of the code? I'm trying to do this with having some logic to reconnect if the connection is closed for any reason 🤔

Checklist

  • This is a documentation problem, not a question or a bug report.
  • I have searched for this issue before posting it and there isn't a duplicate.

Hi, this is not an issue with the library but rather a question. The issues section in GitHub is only to report bugs or enhancements in the library itself. If you have usage questions, you should ask those in StackOverflow or the @TelethonChat group.