@@ -57,6 +57,31 @@ def json_serializer(obj):
5757 raise TypeError (f"Object of type { type (obj )} is not JSON serializable" )
5858
5959
60+ def get_entity_type (entity : Any ) -> str :
61+ """Return a normalized, human-readable chat/entity type."""
62+ if isinstance (entity , User ):
63+ return "User"
64+ if isinstance (entity , Chat ):
65+ return "Group (Basic)"
66+ if isinstance (entity , Channel ):
67+ if getattr (entity , "megagroup" , False ):
68+ return "Supergroup"
69+ return "Channel" if getattr (entity , "broadcast" , False ) else "Group"
70+ return type (entity ).__name__
71+
72+
73+ def get_entity_filter_type (entity : Any ) -> Optional [str ]:
74+ """Return list_chats-compatible filter type: user/group/channel."""
75+ entity_type = get_entity_type (entity )
76+ if entity_type == "User" :
77+ return "user"
78+ if entity_type in ("Group (Basic)" , "Group" , "Supergroup" ):
79+ return "group"
80+ if entity_type == "Channel" :
81+ return "channel"
82+ return None
83+
84+
6085load_dotenv ()
6186
6287TELEGRAM_API_ID = int (os .getenv ("TELEGRAM_API_ID" ))
@@ -935,16 +960,7 @@ async def list_chats(chat_type: str = None, limit: int = 20) -> str:
935960 entity = dialog .entity
936961
937962 # Filter by type if requested
938- current_type = None
939- if isinstance (entity , User ):
940- current_type = "user"
941- elif isinstance (entity , Chat ):
942- current_type = "group"
943- elif isinstance (entity , Channel ):
944- if getattr (entity , "broadcast" , False ):
945- current_type = "channel"
946- else :
947- current_type = "group" # Supergroup
963+ current_type = get_entity_filter_type (entity )
948964
949965 if chat_type and current_type != chat_type .lower ():
950966 continue
@@ -960,7 +976,7 @@ async def list_chats(chat_type: str = None, limit: int = 20) -> str:
960976 name += f" { entity .last_name } "
961977 chat_info += f", Name: { name } "
962978
963- chat_info += f", Type: { current_type } "
979+ chat_info += f", Type: { get_entity_type ( entity ) } "
964980
965981 if hasattr (entity , "username" ) and entity .username :
966982 chat_info += f", Username: @{ entity .username } "
@@ -1005,20 +1021,11 @@ async def get_chat(chat_id: Union[int, str]) -> str:
10051021 result = []
10061022 result .append (f"ID: { entity .id } " )
10071023
1008- is_channel = isinstance (entity , Channel )
1009- is_chat = isinstance (entity , Chat )
10101024 is_user = isinstance (entity , User )
10111025
10121026 if hasattr (entity , "title" ):
10131027 result .append (f"Title: { entity .title } " )
1014- chat_type = (
1015- "Channel" if is_channel and getattr (entity , "broadcast" , False ) else "Group"
1016- )
1017- if is_channel and getattr (entity , "megagroup" , False ):
1018- chat_type = "Supergroup"
1019- elif is_chat :
1020- chat_type = "Group (Basic)"
1021- result .append (f"Type: { chat_type } " )
1028+ result .append (f"Type: { get_entity_type (entity )} " )
10221029 if hasattr (entity , "username" ) and entity .username :
10231030 result .append (f"Username: @{ entity .username } " )
10241031
@@ -1034,7 +1041,7 @@ async def get_chat(chat_id: Union[int, str]) -> str:
10341041 if entity .last_name :
10351042 name += f" { entity .last_name } "
10361043 result .append (f"Name: { name } " )
1037- result .append (f"Type: User " )
1044+ result .append (f"Type: { get_entity_type ( entity ) } " )
10381045 if entity .username :
10391046 result .append (f"Username: @{ entity .username } " )
10401047 if entity .phone :
@@ -1171,7 +1178,7 @@ async def get_contact_chats(contact_id: Union[int, str]) -> str:
11711178 try :
11721179 common = await client .get_common_chats (contact )
11731180 for chat in common :
1174- chat_type = "Channel" if getattr (chat , "broadcast" , False ) else "Group"
1181+ chat_type = get_entity_type (chat )
11751182 chat_info = f"Chat ID: { chat .id } , Title: { chat .title } , Type: { chat_type } "
11761183 results .append (chat_info )
11771184 except :
@@ -3758,7 +3765,7 @@ async def get_folder(folder_id: int) -> str:
37583765 "id" : entity .id ,
37593766 "name" : getattr (entity , "title" , None )
37603767 or getattr (entity , "first_name" , "Unknown" ),
3761- "type" : type (entity ). __name__ ,
3768+ "type" : get_entity_type (entity ),
37623769 }
37633770 if hasattr (entity , "username" ) and entity .username :
37643771 chat_info ["username" ] = entity .username
@@ -3775,7 +3782,7 @@ async def get_folder(folder_id: int) -> str:
37753782 "id" : entity .id ,
37763783 "name" : getattr (entity , "title" , None )
37773784 or getattr (entity , "first_name" , "Unknown" ),
3778- "type" : type (entity ). __name__ ,
3785+ "type" : get_entity_type (entity ),
37793786 }
37803787 excluded_chats .append (chat_info )
37813788 except Exception :
@@ -3790,7 +3797,7 @@ async def get_folder(folder_id: int) -> str:
37903797 "id" : entity .id ,
37913798 "name" : getattr (entity , "title" , None )
37923799 or getattr (entity , "first_name" , "Unknown" ),
3793- "type" : type (entity ). __name__ ,
3800+ "type" : get_entity_type (entity ),
37943801 }
37953802 pinned_chats .append (chat_info )
37963803 except Exception :
0 commit comments