My python project is supposed to listen to an online data stream, analyze it and inform me about certain events. Thus, I have it structured into main.py, streamer.py, analyzer.py and communications.py. The latter uses Telethon to send messages to my phone via Telegram:
from telethon import TelegramClient, sync, events
class telegram_messenger:
def __init__(self):
api_id = somevalue
api_hash = 'anothervalue'
bot_token = 'differentvalue'
client = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)
self.client=client
client.connect()
print("Messenger is online.")
def send_msg(self, message):
self.client.send_message('target', message)
Now my understanding of object-oriented coding is somewhat limited but I understood that it’s probably smart to establish the messenger function as an object, thus I initialize it as
messenger=communication.telegram_messenger()
The thing is now: Can I have a single messenger object that is initialized once and is then accessible from main.py, streamer.py, analyzer.py and possibly more? Or do I need to create individual messengers like streamer_messenger and analyzer_messenger? Maybe an important additional information: In the next expansion stage, I would like to have the messenger being able to receive messages from me/my phone at all times, too, and react to it so it would need to constantly exist and be listening.
I hope you can help me with this issue. Maybe it’s easy but I’m still struggling to wrap my head around object-oriented coding so please bear with me.