telegram_message_scraper
ILoginContext
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.
subscribe_done
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).
QrLoginContext
Bases: ILoginContext
AuthCodeLoginContext
AuthCodeLoginContext(client: TelegramClient, is_sync: bool, phone_number: str, password: str = None, timeout: float = 120)
Bases: ILoginContext
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
Bases: AsyncContextManager[TelegramClient]
Context manager for connecting->disconnecting a new Telegram client.
update_session_file
Update the session file with the given session string.
load_session_file
Load the session string from the given session file.
get_session_filename
Get the session filename for the given session name.
requires_interaction
Check if the client requires user interaction for authentication.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if user interaction is required, False otherwise. |
authenticate
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
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.