Extract Telegram Messages with Sleuth: A Step-by-Step Guide
Greetings, fellow tech enthusiasts! Today, I embark on a journey to explore the powerful realm of data extraction from Telegram chats using a Python library called Telegram Sleuth. This nifty tool allows you to unravel the secrets hidden in your Telegram conversations, extracting messages, media, and metadata for in-depth analysis.
Do you ever wish you could analyze the messages from your Telegram chats? With the powerful open-source Sleuth library for Python, extracting and exploring your Telegram data is easy. In this step-by-step guide, we’ll walk through installing Sleuth, connecting it to your Telegram account, and exporting your messages. Let’s dig in!
Step 1: Get Your Telegram API Credentials
To start digging into your Telegram data, you’ll need to get an API ID and hash from Telegram.
Here’s how:
- Go to https://my.telegram.org/apps and log in with your phone number. Telegram will send a confirmation code.
- Enter the code and you’ll see your API ID and hash. Copy these somewhere safe!
Step 2: Install the Sleuth Library
With your credentials ready, Open your idea, navigate to your terminal and summon Sleuth with a simple spell:
pip install telegram-sleuth
Step 3: Initialize Sleuth and Connect to Telegram
Import Sleuth and create a Sleuth object, pass your API credentials, target username, and any desired customizations:
from telegram_sleuth import Sleuth
sleuth = Sleuth(
api_id='123456678',
api_hash='aSd12Gfd87H4JI2k',
username='KotlinIndonesia'
)
Normally, you can find the username through the contact/group info, In this tutorial, I am targeting my kotlin group. You can also use the optional parameters in Sleuth,
- Specifying a date range for data extraction:
To specify a date range for data extraction, you can use the start_date
and end_date
parameters. For example, to extract data from a chat from November 20, 2023 to November 21, 2023:
sleuth = Sleuth(
...,
start_date='2023-11-20',
end_date='2023-11-21'
)
- Specifying a download path for media files:
To specify a download path for media files, you can use the download_path
parameter. For example, I’ll download media files to the folder C:\Users\naufa\Downloads\sleuth
:
sleuth = Sleuth(
...,
download_path=r'C:\Users\naufa\Downloads\sleuth'
)
What is r mean?
The r
before the string in download_path
is used to create a raw string literal in Python. A raw string literal is prefixed with the letter 'r' and is used to suppress the interpretation of backslashes (\
). In a regular string literal, backslashes are used for escaping special characters (like newline \n
or tab \t
).
- Printing messages to the console as they are extracted:
To print messages to the console as they are extracted, you can use the print_to_console
parameter. For example, to print messages to the console as they are extracted, you can use the following code:
sleuth = Sleuth(
...,
print_to_console=True
)
'''
Example Output:
2023-11-20 15:32:10+00:00 - john_doe: Hello everyone!
2023-11-20 15:35:12+00:00 - john_doe sent a image: downloads/images/image1.jpg
'''
Here’s the complete sleuth object with all the optional parameters:
sleuth = Sleuth(
api_id='123456678',
api_hash='aSd12Gfd87H4JI2k',
username='KotlinIndonesia',
start_date='2023-11-20',
end_date='2023-11-21',
download_path=r'C:\Users\naufa\Downloads\sleuth',
print_to_console=True
)
Step 4: Extract Messages and Media
To extract the data, call the dig()
method:
data = sleuth.dig()
This returns a dictionary with the extracted messages, media, and metadata!
Step 5: Export Data to CSV
To export the structured data to CSV, use export_to_csv()
:
sleuth.export_to_csv(r'C:\Users\naufa\Downloads\sleuth\kotlin_indonesia.csv')
Voila! Now you’ve got a CSV file to explore and analyze as you wish!
Here’s the complete code snippet:
from telegram_sleuth import Sleuth
sleuth = Sleuth(
api_id='123456678',
api_hash='aSd12Gfd87H4JI2k',
username='KotlinIndonesia',
start_date='2023-11-20',
end_date='2023-11-21',
download_path=r'C:\Users\naufa\Downloads\sleuth',
print_to_console=True
)
data = sleuth.dig()
sleuth.export_to_csv('kotlin_indonesia.csv')
In conclusion, Telegram Sleuth empowers you to navigate the intricacies of Telegram chats effortlessly. If you encounter any mystical challenges or seek guidance, reach out to the wise sages at naufalmng@gmail.com or commune with fellow adventurers on the GitHub page.
May your data extraction endeavours be fruitful, and may the Telegram Sleuth be ever in your favour! 🕵️♂️🔍
Sleuth provides powerful capabilities to unlock insights from your messaging history. Give it a try on your Telegram groups or chats and
Let’s Start Sleuthing!