Zaehzar
Zaehzar

Reputation: 11

Member Count of Telegram group using Telethon

I want to monitor a Telegram group (EOS) and see how it developed over time. For this I wrote a small Python/Telethon script, that fetches all messages together with the constructor data and writes this to a MySQL database.

My idea was to sum up MessageActionChatAddUser and MessageActionChatJoinedByLink and after that subtract MessageActionChatDeleteUser.

However I am missing approx. 10k Users in total and furthermore only 3 MessageActionChatDeleteUser occured, which I highly doubt in such a big Channel.

MySQL Query looks as following:

USE db_test;
SELECT COUNT(*) 
FROM telegram_raw
WHERE srvNme = 'MessageActionChatDeleteUser';

The part from my Python script which is evaluating the fetched messages from the channel is:

    if payload[cnt].__class__.__name__ == 'MessageService':
        is_message = False
        is_msg_srv = True
        constr_id = payload[cnt].action.CONSTRUCTOR_ID
        act_nme = payload[cnt].action.__class__.__name__

    elif payload[cnt].__class__.__name__ == 'Message':
        is_message = True
        is_msg_srv = False
        constr_id = 0
        act_nme = ''

    else:
        is_message = False
        is_msg_srv = False
        constr_id = 0
        act_nme = ''

EDIT: Upon looking further into my issue I found that several messages are returned as empty, here an example:

client = TelegramClient(username, api_id, api_hash)
client.start()

if not client.is_user_authorized():
    client.send_code_request(phone)
    try:
        client.sign_in(phone, input('Enter the code: '))
    except SessionPasswordNeededError:
        client.sign_in(password=input('Password: '))

me = client.get_me()
print(me)

try:
    enty = client.get_entity('t.me/EOSproject')
    messages = client.get_messages(entity=enty, ids=37)
finally:
    client.disconnect()

Upvotes: 1

Views: 1937

Answers (1)

Zaehzar
Zaehzar

Reputation: 11

I think I have an answer, however it's not good. Upon playing around and asking in the telethon telegram channel I found, that:

  1. Messages are returned as 'Nonetype' aka empties when they are hidden for example -> no way of retrieving the info as ordinary member in a group

  2. the big miscalculation in the users in the channel leads from data inconsistency on telegrams side (which I didn't quite understand) as well as on the 'Nonetype' messages. A solution would be to go for the admin log where such events are all logged. -> not possible as ordinary member in a group

If anyone finds a solution to this feel free to comment and I will mark it as the right answer.

Upvotes: 0

Related Questions