DEV Community

Yusuf Turhan Papurcu
Yusuf Turhan Papurcu

Posted on

Get Last 1 Hour Telegram Chat

For a fun project I need get last 1 hour of telegram chat. So I figured out how can I do this. And now I’m writing here.

I used Python and telethon library for this project. First install them to the system. Actually this is really easy. Just install pip and telethon with these commands.

python3 -m pip install --upgrade pip
python3 -m pip install --upgrade telethon
Enter fullscreen mode Exit fullscreen mode

If cryptg is installed, the library will work a lot faster, since encryption and decryption will be made in C instead of Python. But I don’t need this because performance is not important for me.

After this you will need API_ID and API_HASH for access telegram api’s from telethon library. Actually, this part was easier than I thought.

  • Login to your Telegram account with the phone number of the developer account to use. Don’t afraid writing your number here. Because in this part we are not using any 3rd party app or library. It’s all telegram api environments part.
  • Click under API Development tools.
  • A Create new application window will appear. Fill in your application details. There is no need to enter any URL, and only the first two fields (App title and Short name) can currently be changed later.
  • Click on Create application at the end. Remember that your API hash is secret and Telegram won’t let you revoke it. Don’t post it anywhere!

So we have API_ID and API_HASH now. Let’s use it them. First we write a script for create session for ourself.

from telethon import TelegramClient

# Use your own values from my.telegram.org
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'

# The first parameter is the .session file name (absolute paths allowed)
with TelegramClient('anon', api_id, api_hash) as client:
    client.loop.run_until_complete(client.send_message('me', 'Hello, myself!'))
Enter fullscreen mode Exit fullscreen mode

You must write your API_ID and API_HASH. And run program.

Image description

Program will ask you your number and send verification code to your number.

After program finished successfully you will have anon.session file. This file is your session file. Do not share this file and your keys.

Image description

And you can see in your message box “Hello, myself!”. This written by our code. If you see this message, you successfully loaded environment and get authorization.

Now let’s get some telegram chat messages. This part also very easy. You just need chat name of you want to get messages.

from telethon import TelegramClient
from datetime import datetime, timedelta, timezone

# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'

client = TelegramClient('anon.session', api_id, api_hash) # You need run twice because this part create session in first run.
client.start()

user_name = "me" # Write here username you want to get last 1 hour of chat.

for message in client.iter_messages(user_name,offset_date=datetime.now(tz=timezone.utc) - timedelta(hours=1),reverse=True):
    print(message.sender_id, ':', message.text)
Enter fullscreen mode Exit fullscreen mode

Also I have a problem with utf-8 characters and group names. And I found a solution for this. You can get all of your chat names and chat ids with this script.

from telethon import TelegramClient

# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'

client = TelegramClient('anon.session', api_id, api_hash) # You need run twice because this part create session in first run.
client.start()

for chat in client.iter_dialogs():
    print(chat.name," --- ", chat.id)
Enter fullscreen mode Exit fullscreen mode

After you get id just go and write instead of user_name. Write as integer not a string.

I hope this little scripts help you about using telegram api.

Top comments (0)