6

I use telethon for sending messages to telegram using python script.
I did not find anything in telethon to search for groups and channels I like used to search on telegram app. Please see iamge. How can I get such list using telethon?

enter image description here

Alok
  • 7,734
  • 8
  • 55
  • 100
  • 3
    I believe this method is called [`contacts.search` in raw API](https://tl.telethon.dev/methods/contacts/search.html). – Lonami May 26 '20 at 12:18
  • Ironically enough, the `SearchGlobal` function of the API does not match up to the 'Global Search' functionality in the official TG app, but @Lonami (well, duh, it's Lonami after all) nailed it - `contacts.search` performs a general, catch-everything search. – ygesher Jun 29 '20 at 07:23
  • It seems that contacts.search just search within contacts and does not search globally. – keramat Dec 30 '22 at 07:11

2 Answers2

2

Create file with your secrets: config.ini

[Telegram]
# no need for quotes

# you can get telegram development credentials in telegram API Development Tools
api_id = 1234
api_hash = 1234

# use full phone number including + and country code
phone = +4411111111111
username = session_user

One solution is this, do_search.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Tested with Telethon version 1.14.0

import configparser
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon.sync import TelegramClient
from telethon import functions, types

# Reading Configs
config = configparser.ConfigParser()
config.read("config.ini")

# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
# (1) Use your own values here
api_id = config['Telegram']['api_id']
api_hash = config['Telegram']['api_hash']

# (2) Create the client and connect
phone = config['Telegram']['phone']
username = config['Telegram']['username']
client = TelegramClient(username, api_id, api_hash)

async def main():
    await client.start()
    # Ensure you're authorized
    if not await client.is_user_authorized():
        await client.send_code_request(phone)
        try:
            await client.sign_in(phone, input('Enter the code: '))
        except SessionPasswordNeededError:
            await client.sign_in(password=input('Password: '))

    search = 'linux'
    result = await client(functions.contacts.SearchRequest(
        q=search,
        limit=100
    ))
    print(result.stringify())

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

And don't forget to always thank @lonami for creating this awesome library Telethon :)

fquinto
  • 527
  • 7
  • 12
  • Thanks for answer but this is giving only 3 search results – Alok Jun 06 '20 at 02:28
  • I understand that if you receive 3 results, then this is a bad answer for you? Why are you thinking this? Can you argue with more details. Thanks. – fquinto Jun 09 '20 at 22:03
  • 1
    I did not mean nor I said that its a bad answer. I am sorry if you feel like that. I just want all the search results which are visible in image in question. – Alok Jun 10 '20 at 13:37
  • The number of the results depends strictly from your side. Test it with different account and you can see that the answer was correct. Is like to say that Google don't search well because don't find my word: ADGH5546SDF335y – fquinto Jun 16 '20 at 12:04
  • 2
    The question is not about how to connect to telegram API using telethon. The only part you should mention was the `contact.search` option. such long answers with unrelated information can be misleading for lots of people. – Amir Feb 15 '22 at 07:27
-2
result = client.get_entity('binanceorderschat')
if result.megagroup == True:
   print('its group')
  • Kindly add enough details to your answer so that it makes sense to future readers. – Abhyuday Vaish May 30 '22 at 11:47
  • 1
    [A code-only answer is not high quality](//meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please [edit] your answer to include explanation and link to relevant documentation. – Stephen Ostermiller May 30 '22 at 11:57