Skip to content

telegram_message_scraper

ILoginContext

ILoginContext(client: TelegramClient, is_sync=True)

Bases: ABC

The client will be disconnected when the login process is done. Args: client (TelegramClient): The fresh Telegram client to use for authentication. sync (bool): If True, the starting the login process will wait for the authentication to complete.

auth_state property

auth_state: AuthState

Get the current authentication state.

get_session_string

get_session_string()

Get the session string for the authenticated session.

subscribe_done

subscribe_done(callback: Callable[[AuthState], None])

Subscribe a callback that gets called when the login process is done. This callback will be run on the background thread monitoring the login process. If the login process is already done, the callback will be called immediately (sync).

run async

run() -> AuthState

Orchestrates the login process.

monitor async

monitor() -> AuthState

Orchestrate monitoring the login process.

Returns:

Name Type Description
AuthState AuthState

The final authentication state.

QrLoginContext

QrLoginContext(client: TelegramClient, is_sync: bool)

Bases: ILoginContext

get_qr_url

get_qr_url() -> str

Get the URL for the QR code login.

AuthCodeLoginContext

AuthCodeLoginContext(client: TelegramClient, is_sync: bool, phone_number: str, password: str = None, timeout: float = 120)

Bases: ILoginContext

push_auth_code

push_auth_code(auth_code: str)

Push the authentication code to the auth code login process.

TelegramMessageScraper

TelegramMessageScraper(api_id: str, api_hash: str, phone_number: str = None, session_name: str = None, password=None, sync_auth: bool = True, login_type: LoginType = LoginType.QRCode, defer_auth=False)

Bases: IAsyncScraper

A scraper that uses the telethon library to pull the contents of Telegram messages.

Attributes:

Name Type Description
api_id str

The API ID for the Telegram client.

api_hash str

The API hash for the Telegram client.

phone_number str

The phone number associated with the Telegram account.

Parameters:

Name Type Description Default
api_id str

The Telegram API ID.

required
api_hash str

The Telegram API hash.

required
phone_number str

The phone number for authentication.

None
session_name str

The name of the session. If None, a temporary StringSesssion will be used

None
password str

The password for two-factor authentication.

None
sync_auth bool

If True, the authentication process will be synchronous.

True
login_type LoginType

The type of login to use (QRCode or AuthCode).

QRCode
defer_auth bool

If True, the scraper will be initialized without authentication.

False

ClientConnection

ClientConnection(client: TelegramClient, client_lock: Lock = None)

Bases: AsyncContextManager[TelegramClient]

Context manager for connecting->disconnecting a new Telegram client.

update_session_file

update_session_file(session_name: str, session_string: str, file_version: int)

Update the session file with the given session string.

load_session_file

load_session_file(session_name: str) -> str

Load the session string from the given session file.

get_session_filename

get_session_filename(session_name: str) -> str

Get the session filename for the given session name.

is_authenticating

is_authenticating() -> bool

Check if the client is currently authenticating.

requires_interaction

requires_interaction() -> bool

Check if the client requires user interaction for authentication.

Returns:

Name Type Description
bool bool

True if user interaction is required, False otherwise.

authenticate

authenticate(login_type: LoginType = None, sync_auth: bool = None) -> AuthState

Create a new login context and authenticate the user.

Parameters:

Name Type Description Default
login_type LoginType

The type of login to use (QRCode or AuthCode). If None, the instance's login_type will be used.

None
sync_auth bool

If True, the authentication process will be synchronous. If None, the instance's sync_auth will be used.

None

async_scrape async

async_scrape(url: str) -> ScrapeResult

Asynchronously scrape the content of a Telegram message from a URL.

Parameters:

Name Type Description Default
url str

A URL formatted as 'https://t.me/{username}/{message_id}'.

required

Returns:

Name Type Description
ScrapeResult ScrapeResult

An object representing the success or failure of the scraping process.

The method validates the URL, extracts the username and message ID, and retrieves the message content.