search_posts()

async Client.search_posts()

Search public posts by hashtag or by text.

If you want to get the posts count only, see search_posts_count().

Usable by Users Bots
Parameters:
  • hashtag (str, optional) – Hashtag to search for, with or without the leading #.

  • query (str, optional) – Text to search for instead of a hashtag. Searching by text requires a Premium account; Telegram answers [403 PREMIUM_ACCOUNT_REQUIRED] otherwise.

  • limit (int, optional) – Limits the number of posts to be retrieved. By default, no limit is applied and all posts are returned.

Returns:

Generator – A generator yielding Message objects.

Raises:

ValueError – In case neither hashtag nor query is given.

Example

# Search for posts with a hashtag
async for post in app.search_posts(hashtag="news", limit=20):
    print(post.chat.title, post.text)

# Search for posts mentioning "telegram" (Premium only)
async for post in app.search_posts(query="telegram", limit=50):
    print(post.chat.title, post.text)