-1

So I've been trying for a long time, I found some pre-made programs, but they costs 100$. I've tried multiple apps and programs like Telegram Auto and Telegram Kit, but they cost a lot and I don't have such money right now.

I am trying to do it in Python and Telethon(Don't have a lot of experience in it)

I already made an app on telegram developer tools, got the API Number and Hash, and found the following code online

from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
import csv

api_id = My API ID
api_hash = 'MY API HASH'
phone = 'MY PHONE'
client = TelegramClient(phone, api_id, api_hash)

client.connect()

chats = []
last_date = None
chunk_size = 200
groups=[]

result = client(GetDialogsRequest(
             offset_date=last_date,
             offset_id=0,
             offset_peer=InputPeerEmpty(),
             limit=chunk_size,
             hash = 0
         ))
chats.extend(result.chats)

for chat in chats:
    try:
        if chat.megagroup== True:
            groups.append(chat)
    except:
        continue

print('Choose a group to scrape members from:')
i=0
for g in groups:
    print(str(i) + '- ' + g.title)
    i+=1

g_index = input("Enter a Number: ")
target_group=groups[int(g_index)]

print('Fetching Members...')
all_participants = []
all_participants = client.get_participants(target_group, aggressive=True)

print('Saving In file...')
with open("members.csv","w",encoding='UTF-8') as f:
    writer = csv.writer(f,delimiter=",",lineterminator="\n")
    writer.writerow(['username','user id', 'access hash','name','group', 'group id'])
    for user in all_participants:
        if user.username:
            username= user.username
        else:
            username= ""
        if user.first_name:
            first_name= user.first_name
        else:
            first_name= ""
        if user.last_name:
            last_name= user.last_name
        else:
            last_name= ""
        name= (first_name + ' ' + last_name).strip()
        writer.writerow([username,user.id,user.access_hash,name,target_group.title, target_group.id])      
print('Members scraped successfully.')

I entered My information, and I started the program, but I keep getting this error.

Traceback (most recent call last): File "c:\Users\User\Desktop\export.py", line 23, in hash = 0 File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\telethon\sync.py", line 39, in syncified return loop.run_until_complete(coro) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\asyncio\base_events.py", line 579, in run_until_complete return future.result() File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\telethon\client\users.py", line 64, in call result = await future telethon.errors.rpcerrorlist.AuthKeyUnregisteredError: The key is not registered in the system (caused by GetDialogsRequest)

I searched everywhere for a fix or a tutorial and I didn't find anything. My only other choice was coming here.

Please Help.

Regards, Daniel

Daniel Halabi
  • 21
  • 1
  • 1
  • 1
    That's considered spam is and is against telegram's ToS. your account will be banned if you do so. – painor Sep 06 '19 at 14:15

1 Answers1

1

Admittedly, the docs aren't very clear about what that error means, but from the looks of it, you might be suffering at the hands of an unmanaged resource. The docs themselves suggest here:

The TelegramClient aggregates several mixin classes to provide all the common functionality in a nice, Pythonic interface. Each mixin has its own methods, which you all can use.

In short, to create a client you must run:

   from telethon import TelegramClient

   client = TelegramClient(name, api_id, api_hash)

   async def main():
       # Now you can use all client methods listed below, like for example...
       await client.send_message('me', 'Hello to myself!')

   with client:
       client.loop.run_until_complete(main())

You don’t need to import these AuthMethods, MessageMethods, etc. Together they are the TelegramClient and you can access all of their methods.

See Client Reference for a short summary.

Consider using python's with statement to help manage client.

As an aside, did you know that one of the devs who contributed to Telethon has already written a free and open source scraper?

Community
  • 1
  • 1
Nick Reed
  • 4,989
  • 4
  • 17
  • 37