skip to Main Content

I am trying to run the GetMessagePublicForwards function for a public channel on Telegram, using either the telethon or the pyrogram Python libraries. That function should allow to "Obtains a list of messages, indicating to which other public channels was a channel message forwarded".

I have trouble understanding what are the inputs of the function. Moreover, I have an hard time obtainaing the access_hash for a public telegram channel. I would appreciate any help in that direction too (in that regards, I tried what was said on this link, without success).

So far I tried either:

from pyrogram import Client
from pyrogram.raw import functions, types

api_id = MY_ID
api_hash = MY_HASH

async with Client("my_account", api_id, api_hash) as app:
    chat_id = CHAT_ID  # the channel id
    message_id = MESSAGE_ID  # message id
    
    access_hash= ACCESS_HASH # for the public channel
    
    channel = types.InputChannel(
        channel_id=chat_id, 
        access_hash=access_hash)
    
    offset_peer = types.InputPeerChannel(
        channel_id=chat_id, 
        access_hash=access_hash)
    
    r = await app.invoke(
        functions.stats.GetMessagePublicForwards(
        channel=channel,
        msg_id=message_id,
        offset_rate=1,
        offset_peer=offset_peer,
        offset_id=1,
        limit=100))

    print(r)

on pyrogram or

from telethon.sync import TelegramClient
from telethon import functions, types

api_id = MY_ID
api_hash = MY_HASH

async with TelegramClient('me', api_id, api_hash) as client:
    
    chat_id = CHAT_ID  # the public channel
    message_id = MESSAGE_ID  # message id
    access_hash= ACCESS_HASH # for the public channel
    
    channel = types.InputChannel(
        channel_id=chat_id, 
        access_hash=access_hash)
    
    offset_peer = types.InputPeerChannel(
        channel_id=chat_id, 
        access_hash=access_hash)
    
    result = await client(functions.stats.GetMessagePublicForwardsRequest(
        channel=channel,
        msg_id=message_id,
        offset_rate=42,
        offset_peer=offset_peer,
        offset_id=42,
        limit=100
    ))

on telethon.

In both cases, I get an error:

Chat admin privileges are required to do that in the specified chat (for example, to send a message in a channel which is not yours), or invalid permissions used for the channel or group (caused by GetMessagePublicForwardsRequest)

For telethon, and

ChannelInvalid: Telegram says: [400 CHANNEL_INVALID] - The channel parameter is invalid (caused by "stats.GetMessagePublicForwards")

for pyrogram.

This makes me suspect there is something wrong in my code since the channel is public. Moreover, I know that here one can find the same information I am looking for, which makes me think that the my code should work for that function (otherwise it would have to be explained how such information is available at TgStat).

Has anybody used this function before? Could they help me understand if there’s any bug in my code?

2

Answers


  1. More likely than not, you lack sufficient permissions in the channel you’re trying to query from. There may be other limitations, such as number of participants or how the channel is setup, that cause the issue, but Telegram could (confusingly for us) be reusing the same error code.

    Unfortunately, the official documentation for stats.getMessagePublicForwards does not say much more.

    Login or Signup to reply.
  2. Used your code as a base for my solution, get no public reposts, but when i changed offset_rate=0 and offset_id=0 it started working as intended

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search