Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events.NewMessage event is not triggered for certain channels #4345

Open
3 tasks done
M4jx opened this issue Apr 6, 2024 · 14 comments
Open
3 tasks done

events.NewMessage event is not triggered for certain channels #4345

M4jx opened this issue Apr 6, 2024 · 14 comments

Comments

@M4jx
Copy link

M4jx commented Apr 6, 2024

Code that causes the issue

@client.on(events.NewMessage(chats = list(channels_hashmap.keys())))
async def main(event):
     print(now.strftime("%d/%m/%Y %H:%M:%S") + f" Received from channel: {event.chat_id}")

Expected behavior

When a new message is sent in a channel passed to chats, the events.NewMessage should be triggered.

Actual behavior

When a new message is sent, the events.NewMessage is not being triggered for certain channels.

I have tested to create a channel in an account A and monitor it in account B (where telethon is running) and the events.NewMessage was triggered instantly. However, for certain public channels that I do not own, the event is not being triggered.

Tested on a newly created channel with 2 subscribers and it worked.
Tested on a public channel with 500K subscribers and it did not work.

Traceback

No crashes are happening.

Telethon version

1.34.0

Python version

3.10.12

Operating system (including distribution name and version)

Ubuntu 22.04.4 LTS

Other details

No response

Checklist

  • The error is in the library's code, and not in my own.
  • I have searched for this issue before posting it and there isn't an open duplicate.
  • I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip and triggered the bug in the latest version.
@abhinavbajpai2012

This comment was marked as duplicate.

@Lonami
Copy link
Member

Lonami commented Apr 8, 2024

Please react with the thumbs-up emoji instead of posting "me too" comments.

@M4jx
Copy link
Author

M4jx commented Apr 9, 2024

Update:
While Telethon is running, if a message is sent in the monitored channel and the events.NewMessage event is not triggered. It can be triggered manually if you open the channel and read the message in the Telegram app (from the same user account telethon is running on).
Once the channel is opened, and the message is read, telethon triggers the event instantly.

@khoben
Copy link
Contributor

khoben commented Apr 12, 2024

Update:
While Telethon is running, if a message is sent in the monitored channel and the events.NewMessage event is not triggered. It can be triggered manually if you open the channel and read the message in the Telegram app (from the same user account telethon is running on).
Once the channel is opened, and the message is read, telethon triggers the event instantly.

Telegram doesn't always send all updates to the client. Some channels can only be updated after manually calling for updates. You can try restarting your app, creating a new session, changing the IP or the account itself.

@iSte94
Copy link

iSte94 commented Apr 12, 2024

Same problem for me, and it's not the library. Same problem with Pyrogram

@bosslaiv
Copy link

I noticed the same ,,, few months back i was runing multiple clients via mobile IP , and received 100% all updates ,,, few months later , after a pause , i started a single client over VPN , and not all updates where being registered .

@Lonami
Copy link
Member

Lonami commented Apr 15, 2024

Some people have reported that some channels must be polled manually to receive updates as of recently.

Telethon does not have any built-in way to do this yet (but of course can be done via raw API; I just haven't checked so I don't know exactly how).

I don't know if this will ever be "fixed" to work automatically, because if the need for manual polling is true, Telethon would have no way to know what it needs to poll manually.

@Tidalikk
Copy link

I noticed the same ,,, few months back i was runing multiple clients via mobile IP , and received 100% all updates ,,, few months later , after a pause , i started a single client over VPN , and not all updates where being registered .

Since when did you start noticing this issue?

@francestu96
Copy link

francestu96 commented Apr 25, 2024

Update: While Telethon is running, if a message is sent in the monitored channel and the events.NewMessage event is not triggered. It can be triggered manually if you open the channel and read the message in the Telegram app (from the same user account telethon is running on). Once the channel is opened, and the message is read, telethon triggers the event instantly.

Actually, in my case, even the read of the message from the Telegram app does not trigger it. Is it only a problem of mine?

@sushilronghe99
Copy link

#4361

In my case the whole app just blackouts for randomly 1-2 minutes and then it receives the new messages updates properly. I have 10 channels subscribed and for 2 minutes app did not receive any update from any channels between 9:27-9:29 AM and 10:02-10:04 AM. Afterward, for the rest of the day, all updates were received properly.

So as suggested here NewMessage event is not reliable and we should poll those channels manually. I will try that and let you know how it goes.

@arvindavoudi
Copy link

I've encountered a peculiar situation with a message delivery order, and I'd like to explain it in hopes of shedding light on the issue.
Assume we have three messages (A, B, and C) sent in the following order with some minutes delay in between:

  • A
  • B
  • C

In my case, the following occurred:

  • Message (A) was received instantly.
  • Message (C) was received 4 minutes after it was sent.
  • Message (B) was not received until I manually opened the chat after receiving the update for message (C). This behavior aligns with the previous observation made by @M4jx.

To better understand and debug this issue, I have the following questions:

  • Why do we sometimes encounter situations where new messages are received instantly, even in problematic channels (although rare)?
  • How can a message be ignored and queued for later delivery, while the next message is received (albeit with a delay)?
  • Is it possible that the Telegram server attempts to transmit the message (B) first, but upon failing for several minutes, it proceeds with the next available message (C), causing the delay in receiving (C)?
  • What could be blocking the transmission of message (B)?
  • Could this issue be related to an update to one of Telegram's layers? Since Pyrogram is no longer maintained and exhibits the same issue, I can't help but suspect a problem with an unfixed layer.
  • Many users are encountering this issue, so it is a common problem corrupting people's bots, as well as mine. So how fast can we find a workaround to give Telethon back its correct functionality? Due to its critical consequences, is contacting Telegram Support considerable if we have no way to make it work as intended?

Checking the behavior of other packages written in other languages can also help us to confirm where the issue is from and why.

@Lonami
Copy link
Member

Lonami commented May 7, 2024

Out-of-order messages shouldn't occur because the library checks that the incoming pts come in order:

elif local_pts + pts.pts_count < pts.pts:

Make sure the message isn't being deleted and getting resent.

I have observed official clients displaying messages that had been deleted multiple times, and not too long ago.

If you really think Telethon is processing updates out of order, I either need someone with that problem to debug it themselves, or a way in which I can reliably reproduce it myself. Although I've already spent a significant amount of time going over the update handling code myself, so I doubt I would uncover anything.

As far as I know https://core.telegram.org/api/updates doesn't say anything Telethon isn't doing already (at the time of writing).


As for missing messages, or the library fetching them after a while. Yes, there is a default timeout after which the library will attempt to fetch updates:

NO_UPDATES_TIMEOUT = 15 * 60

This is in line with Telegram's documentation above:

Manually obtaining updates is also required in the following situations:

  • Long period without updates: no updates for 15 minutes or longer.

@arvindavoudi
Copy link

arvindavoudi commented May 7, 2024

Dear @Lonami, I'm sure that I received message (C) before (B) but I'm not sure if it was deleted and sent again or not. If it was deleted, I expected to receive that exact same message again after a while, right? but as you know, sometimes messages do not get delayed, they simply get ignored and never get transmitted. Even after opening that channel on my phone, message (B) was not received "twice", just only once and I also didn't receive message (C) again (meaning that message (C) was not deleted and got resent). As far as I consider these messy updates complicated and not quite trackable, I cannot really guarantee that this unordered updates issue is valid but It was my observation that I decided to share with you because my whole project is now unusable after a few months of not being on the server. It's not working and I'm very concerned. :)
With the all experience you have, can you consider submitting a bug to https://bugs.telegram.org/ as Telethon owner? I think it would help if you're sure there is no issue with Telethon itself since Pyrogram also does not work as expected and a working solution is needed widely in the industry.

P.S.: If you haven't any channel suffering from this updating issue (not the ordering one), I know one that I can send only privately to you.

@Lonami
Copy link
Member

Lonami commented May 8, 2024

I do not have interest in submitting bug reports to Telegram. You can certainly message me privately (or include the channel in a GitHub comment between <!-- HTML comments --> so that only maintainers can read it), and I may give it a go… eventually.

Perhaps trying this out would be a good excuse to make sure update handling works correctly in the (not ready for use) v2 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants