Skip to content

Instantly share code, notes, and snippets.

@shinenelson
Last active August 5, 2022 16:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinenelson/5ead5d23de008bcdf7c68433194a1c0b to your computer and use it in GitHub Desktop.
Save shinenelson/5ead5d23de008bcdf7c68433194a1c0b to your computer and use it in GitHub Desktop.
sort telegram dialog list by unread count

Sort Telegram Dialogs By Unread Count ( per folder )

When you have a large number of unread messages to catch up with, you tend to search for the dialog with the smallest unread count and finish those off first before moving on to the ones with larger unread messages.

Alas, Telegram does not provide an option to sort your dialogs by unread count. This script sorts your unreads by count and prints them out along with the number of unread messages. You can then pick those dialogs and start catching up with your unreads. This helps in finishing the smaller dialogs faster giving you some kind of satisfaction and motivation to keep going.

Dependencies

This script requires telethon ( source repository on GitHub ) to run.

Run pip3 install -r requirements.txt from the project's root directory.

And obviously, since this is a python script, it goes without saying that you would also need python3 installed and available in your PATH.

Telegram Configuration

  1. To run the script, you need to obtain API credentials for your Telegram account. You can also access your existing credentials
  2. Add the credentials obtained in Step 1 to config

Running the Script

The script is interactive by default. It will first fetch the different folders on the account and print them out so that the user can choose which folder to sort. After the user inputs a choice, the dialogs in the folder are then fetched, sorted and printed.

The script can also be supplied with an integer argument that represents the folder_id of the folder on the account.

#!/usr/bin/env python3
from sys import argv, exit # pylint: disable=redefined-builtin
from telethon.sync import TelegramClient
from telethon.utils import get_display_name
from telethon.tl.types import User
from telethon.tl.functions.messages import GetDialogFiltersRequest
# Telegram API Credentials
config = {
'session_name': 'telegram',
'api_id': 1234567,
'api_hash': '1234567890abcdef0987654321fedcba'
}
with TelegramClient(
config['session_name'],
config['api_id'],
config['api_hash']
) as client:
# get all dialogs on the account
dialogs = client.get_dialogs()
# sort dialogs by unread_count to be print-friendly
dialogs.sort(key=lambda dialog: dialog.unread_count)
# get all dialog_filters on the account
dialog_filters = client(GetDialogFiltersRequest())
# sort the list by id to be print-friendly
dialog_filters.sort(key=lambda dialog_filter: dialog_filter.id)
# look for folder_id in arguments
# and skip interactive prompt if found
if len(argv) > 1:
try:
folder_input = int(argv[1])
folder_input_set = True
except ValueError:
folder_input_set = False
else:
folder_input_set = False
while not folder_input_set:
try:
# print 'All chats' and 'Archived chats' manually
# because they are default and are not returned
# by GetDialogFiltersRequest
print('0 All chats\n1 Archived chats')
for dialog_filter in dialog_filters:
print(dialog_filter.id, dialog_filter.title)
folder_input = int(input('Choose folder to sort unreads : '))
folder_input_set = True
except ValueError:
print('ValueError: invalid literal')
if folder_input == 1:
dialogs = client.get_dialogs(folder=folder_input)
dialogs.sort(key=lambda dialog: dialog.unread_count)
elif folder_input > 1:
dialog_filter = next(filter(
lambda dialog_filter: dialog_filter.id == folder_input,
dialog_filters
), None)
# dialog_filter validations
if not dialog_filter:
exit('could not find dialog filter with ID %d' % folder_input)
if not dialog_filter.include_peers:
exit('could not find any peers to include')
for dialog in dialogs:
if dialog.unread_count != 0:
# User entities does not have the title attribute
# so set their display name as the title
if isinstance(dialog.entity, User):
dialog.entity.title = get_display_name(dialog.entity)
if folder_input > 1 and client.get_input_entity(dialog.entity) not in dialog_filter.include_peers:
continue
print(dialog.unread_count, dialog.date, dialog.entity.title, sep='\t')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment