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

NewMessage event stops triggering from channels after ~10 seconds since connection #654

Open
Metalingus opened this issue Mar 26, 2024 · 25 comments

Comments

@Metalingus
Copy link

Everything was working fine until today, NewMessage event stops firing from channels after around 10 seconds from starting the program. I am still getting NewMessage event from private chats. I tried periodically calling .getMe() but it didn't help.

@hoanglm4
Copy link

I got the same issue

@Metalingus
Copy link
Author

I wonder if it's telegram restricting the account because I have another account that works just fine with the same code. However when I check with the spam bot, it tells me that there are no restrictions applied to my account.

@Metalingus
Copy link
Author

Try to change DC: client.session.setDC(dcId: number, serverAddress: string, port: number); https://docs.pyrogram.org/faq/what-are-the-ip-addresses-of-telegram-data-centers I have a problem with DC4

Whenever I use the setDC it either freezes my current session or when I use it for a new session I get "Phone Migrated to DC 4"

@hoanglm4
Copy link

@Metalingus
Do you have any idea to workaround for this case ?

@Metalingus
Copy link
Author

@Metalingus Do you have any idea to workaround for this case ?

For now I implemented an ugly solution. I created 3 sessions that disconnect reconnect every 5 seconds. As mentioned earlier, this is related to DC4, I created a new account there and the same thing happened. I also tried telethon and it's happening too unless I keep interacting with the channel.

@mominak47
Copy link

@Metalingus Do you have any idea to workaround for this case ?

For now I implemented an ugly solution. I created 3 sessions that disconnect reconnect every 5 seconds. As mentioned earlier, this is related to DC4, I created a new account there and the same thing happened. I also tried telethon and it's happening too unless I keep interacting with the channel.

It happened with me in 2022 and the solution was to update the gramjs library. This time it didn't fix it. I am also having the same issue.

@awdr74100
Copy link

Hi guys, so this is an official telegram api issue? This issue is seriously hampering my development of telegram-forwarder, the listen function is very important to me, but so far the test result is that after 2 minutes I don't receive any new messages anymore!

@mominak47
Copy link

Hi guys, so this is an official telegram api issue? This issue is seriously hampering my development of telegram-forwarder, the listen function is very important to me, but so far the test result is that after 2 minutes I don't receive any new messages anymore!

I am also having the same issues with my telegram-forwarder. Did you find any solution yet?

@jekatigr
Copy link

Same issue for me in DC4. Change DC doesn't help: I've created a new account in DC5 but it has the same issue.

@mominak47
Copy link

Hi guys, so this is an official telegram api issue? This issue is seriously hampering my development of telegram-forwarder, the listen function is very important to me, but so far the test result is that after 2 minutes I don't receive any new messages anymore!

I fixed it by calling getDialogs on an interval of 30 seconds. It has been more than 24 hours already and it seems to be working fine for me.

@awdr74100
Copy link

@mominak47 Hi, Have you tested any methods other than getDialogs()? Like getMe() method or something like that. It seems that you have to call a specific method periodically during the listening process to maintain contact, am I correct in my understanding? Thank you for your reply.

@TwizzX17
Copy link

TwizzX17 commented Apr 1, 2024

@mominak47 Which version of the package are you running?

@mominak47
Copy link

@awdr74100 Yeah, you have to call getMe() once after client.connect() . I was using the same way for more than an year and somehow it recently stopped working. I managed to run it again after calling getDialogs on every 30 seconds.

@awdr74100
Copy link

@mominak47 Thank you for your immediate reply!

I'm still not sure if this is some kind of official limitation of the telegram api, where monitoring can be kept running 24 hours a day with infrequent channel updates, whereas with frequent channel updates, monitoring can only be kept running for 2 minutes, and then no more updates can be received, which means that the callback of the addEventHandler will never be invoked.

I've tried gramjs, tgsnake, mtkruto and all have the same problem.

I suspect the problem may be related to calls like UpdatesTooLong getDifference.

@maciejkwas
Copy link

I experience the same: NewMessage event is not triggered, BUT only for some "specific" groups. I don't know yet what makes those groups different. Appearantly even official app is working like that: I receive push notifications, but I can not see any chat updates on listing until I actually visit that channel/group (which makes me believe instead of NewMessage event triggered it's just getDifference that makes recent messages visible there).

@peacedoorballl
Copy link

peacedoorballl commented Apr 11, 2024

сообщения из reciver иногда приходят, иногда не приходят.
использую node-red-node-telegrambot 0.1.6
https://flows.nodered.org/node/node-red-node-telegrambot

@mominak47
Copy link

I experience the same: NewMessage event is not triggered, BUT only for some "specific" groups. I don't know yet what makes those groups different. Appearantly even official app is working like that: I receive push notifications, but I can not see any chat updates on listing until I actually visit that channel/group (which makes me believe instead of NewMessage event triggered it's just getDifference that makes recent messages visible there).

What version are you using?

@maciejkwas
Copy link

What version are you using?

recent (2.20.10) even though when you start the app it shows Running GramJS version 2.20.9 in logs. I downgraded back to 2.17.10 and had the same.

@ioeldev
Copy link

ioeldev commented Apr 11, 2024

Versions:
"telegram": "^2.20.10"

Curious if this issue would occur in a telegram python package, though i wouldn't want to switch from nodejs...
This also occurs for me.

async function listenToNewMessage(event: NewMessageEvent) {
  const message = event.message;
  if (
    message.isChannel &&
    (Number(message.chatId) == channels_id.CHANNEL_I_WANT_TO_LISTEN_TO ||
      Number(message.chatId) == channels_id.CHANNEL_I_WANT_TO_LISTEN_TO_2 ||
      Number(message.chatId) == channels_id.MY_TESTING_CHANNEL)
  ) {
    try {
      const translated = await gpt_translate(message.message);
      console.log("Translated message: ", translated);
      message.message = translated;
      await client.sendMessage(channel_name, {
        message: message.message,
      });
    } catch (e) {
      console.log(e);
    }
  }
}

// ...

client.addEventHandler(listenToNewMessage, new NewMessage({}));

My listener does work when the channel is channels_id.MY_TESTING_CHANNEL, but for the two others it doesn't even get called, like something is blocking the eventHandler when it's some specific channels, even though the config seems to be the same on the three channels (it is a public channel; I have joined the three channels).

Something i did pay attention is that on my local environment the eventHandler does work for the three channels, but when it's on my deployed EC2 server it never does.

NOTE:
I just saw that my local node version is v19.0.0 whereas my EC2 has 21.7.0 installed.
Checking now if downgrading to 19 will make a change, will update here :)

@ioeldev
Copy link

ioeldev commented Apr 11, 2024

After downgrading to 19.0.0, it still doesn't work on production, and work time to times in local, but most of the time it doesn't work

Versions: "telegram": "^2.20.10"

Curious if this issue would occur in a telegram python package, though i wouldn't want to switch from nodejs... This also occurs for me.

async function listenToNewMessage(event: NewMessageEvent) {
  const message = event.message;
  if (
    message.isChannel &&
    (Number(message.chatId) == channels_id.CHANNEL_I_WANT_TO_LISTEN_TO ||
      Number(message.chatId) == channels_id.CHANNEL_I_WANT_TO_LISTEN_TO_2 ||
      Number(message.chatId) == channels_id.MY_TESTING_CHANNEL)
  ) {
    try {
      const translated = await gpt_translate(message.message);
      console.log("Translated message: ", translated);
      message.message = translated;
      await client.sendMessage(channel_name, {
        message: message.message,
      });
    } catch (e) {
      console.log(e);
    }
  }
}

// ...

client.addEventHandler(listenToNewMessage, new NewMessage({}));

My listener does work when the channel is channels_id.MY_TESTING_CHANNEL, but for the two others it doesn't even get called, like something is blocking the eventHandler when it's some specific channels, even though the config seems to be the same on the three channels (it is a public channel; I have joined the three channels).

Something i did pay attention is that on my local environment the eventHandler does work for the three channels, but when it's on my deployed EC2 server it never does.

NOTE: I just saw that my local node version is v19.0.0 whereas my EC2 has 21.7.0 installed. Checking now if downgrading to 19 will make a change, will update here :)

@onurcansevinc
Copy link

I have the same problem. Has anyone found a solution?

@thegauravparmar
Copy link

Anyone found any solution for this? I am also stuck on same.

@peacedoorballl
Copy link

Проблема в том что не может одновременно работать отправка и прием сообщений в одном питоновском скрипте.

Я сделал два питоновских скрипта в разных окружениях, на прием и на отправку. И все работает.

@thegauravparmar
Copy link

I solved it by using another telegram account. Its not working in the same account.

@peacedoorballl
Copy link

Работает. Нужно лишь второе окружение.

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