B
B
Banjiro2022-03-16 13:55:09
Python
Banjiro, 2022-03-16 13:55:09

How to parse telegram posts using pyrogram?

Hi, I couldn’t figure out the pyrogram documentation and didn’t find how I can receive posts from a channel in telegram live. I would be extremely grateful even for hints or links to the dock

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Golovanenko, 2022-03-16
@drygdryg

First you need to know the ID of the target channel. This can be done using third-party Telegram clients (Kotatogram for PC or Graph for Android, for example).
Then write the following message handler that will only respond to messages from the channel whose ID is set by the CHANNEL_ID variable:

from pyrogram import Client
from pyrogram import types, filters


CHANNEL_ID = -11012345678

app = Client(
    "my_account",
    api_id=12345,
    api_hash="0123456789abcdef0123456789abcdef"
)


@app.on_message(filters=filters.channel)
def my_handler(client: Client, message: types.Message):
    if message.chat.id != CHANNEL_ID:
        return
    print("Получено новое сообщение с ID", message.message_id)
    # Как-то обработать сообщение с канала, например, напечатать его текст
    print("Текст:", message.text)


app.run()

Here you can see the description of the Message object in order to work with it in the future: https://docs.pyrogram.org/api/types/Message#pyrogr...
You can also exclude message editing events (receiving edited messages from the channel), for this You need to pass the following filters to the decorator:
@app.on_message(filters=filters.channel & ~filters.edited)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question