Skip to content

Instantly share code, notes, and snippets.

@nedludd0
Last active January 13, 2021 14:40
Show Gist options
  • Select an option

  • Save nedludd0/c94a2491da397eed5af86c73c0a12d16 to your computer and use it in GitHub Desktop.

Select an option

Save nedludd0/c94a2491da397eed5af86c73c0a12d16 to your computer and use it in GitHub Desktop.
borg_pyrogram.py
"""
SINGLETON CLASS ENCAPSULATING A PYROGRAM TELEGRAM CLIENT
Credit: https://gist.github.com/skeeved/79dfe5652b20182586bb55c3cbc94250
"""
""" Config """
from os import environ
BOT_TOKEN = environ.get('BOT_TOKEN')
API_ID = environ.get('API_ID')
API_HASH = environ.get('API_HASH')
WORKDIR = environ.get('WORKDIR')
""" Borg pattern singleton """
from pyrogram import Client
import sys
class PyrogramClientClass:
__state = {}
def __init__(self):
self.__dict__ = self.__state
if not hasattr(self, 'client'):
self.client = Client('Example-Name-BOT', bot_token = BOT_TOKEN, api_id = API_ID, api_hash = API_HASH, workdir = WORKDIR)
if __name__ == '__main__':
pyrogram_client_obj = PyrogramClientClass().client
pyrogram_client_obj.start()
msg_sent = pyrogram_client_obj.send_message("TG_ID", "MESSAGE TEXT")
pyrogram_client_obj.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment