Reputation: 11
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?
Upvotes: 0
Views: 309
Reputation: 41
I've used your code as a base for my implementation, get no public reposts, but when i changed offset_rate=0
and offset_id=0
it started working as intended.
Working example to view all public reposts of 100 last posts on given channel in reverse chronological order:
#!/usr/bin/env python
from pyrogram import Client
from pyrogram.raw import functions, types
import datetime
# get yours appid and apphash at my.telegram.org
APPID =
APPHASH = ""
CHANNEL_ID="-1001883618231" # @mapgleos
app = Client("amogus", api_id=APPID, api_hash=APPHASH)
async def main():
async with app:
ch_data = await app.resolve_peer(CHANNEL_ID)
channel_id = ch_data.channel_id
access_hash = ch_data.access_hash
channel = types.InputChannel(
channel_id=channel_id,
access_hash=access_hash)
offset_peer = types.InputPeerChannel(
channel_id=channel_id,
access_hash=access_hash)
last_message = await app.get_chat_history(CHANNEL_ID).__anext__()
last_message_id = last_message.id
reposts = []
for i in range(100):
message_id = last_message_id - i
try:
r = await app.invoke(
functions.stats.GetMessagePublicForwards(
channel=channel,
msg_id=message_id,
offset_rate=0,
offset_peer=offset_peer,
offset_id=0,
limit=100))
except Exception:
print(f"got error on t.me/mapgleos/{message_id}")
continue
for msg, chan in zip(r.messages, r.chats):
repost_dict = {"m_id":msg.id, "date":msg.date, "views":msg.views, "c_id":chan.id,
"title":chan.title, "username":chan.username,
"subs":chan.participants_count}
reposts.append(repost_dict)
for r in sorted(reposts, key=lambda x:-x["date"]):
date_obj = datetime.datetime.fromtimestamp(r["date"])
date_str = date_obj.strftime("%m/%d/%Y, %H:%M:%S")
views_str = f"{r['views']:5} views"
url_str = f"t.me/{r['username']}/{r['m_id']}"
name_str = f"{r['title']}"
print(f"{date_str} {views_str}\n{url_str} {name_str}")
app.run(main())
Upvotes: 0
Reputation: 7066
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.
Upvotes: 0