From eb52dccfa75387445c9743e65e07ffa3c0159644 Mon Sep 17 00:00:00 2001
From: Wizou <11647984+wiz0u@users.noreply.github.com>
Date: Wed, 27 Aug 2025 00:18:34 +0200
Subject: [PATCH] Helper OpenChat to monitor a group/channel without joining
(#333)
---
src/Client.Helpers.cs | 25 +++++++++++++++++++++++--
1 file changed, 23 insertions(+), 2 deletions(-)
diff --git a/src/Client.Helpers.cs b/src/Client.Helpers.cs
index bdfb48f..3cc888f 100644
--- a/src/Client.Helpers.cs
+++ b/src/Client.Helpers.cs
@@ -13,7 +13,6 @@ namespace WTelegram
{
partial class Client
{
- #region Client TL Helpers
/// Used to indicate progression of file download/upload
/// transmitted bytes
/// total size of file in bytes, or 0 if unknown
@@ -910,6 +909,28 @@ namespace WTelegram
}
return chat;
}
- #endregion
+
+ /// Receive updates for a given group/channel until cancellation is requested.
+ /// Group/channel to monitor without joining
+ /// Cancel token to stop the monitoring
+ /// After cancelling, you may still receive updates for a few more seconds
+ public async void OpenChat(InputChannel channel, CancellationToken ct)
+ {
+ var cts = CancellationTokenSource.CreateLinkedTokenSource(_cts.Token, ct);
+ try
+ {
+ while (!cts.IsCancellationRequested)
+ {
+ var diff = await this.Updates_GetChannelDifference(channel, null, 1, 1, true);
+ var timeout = diff.Timeout * 1000;
+ await Task.Delay(timeout != 0 ? timeout : 30000, cts.Token);
+ }
+ }
+ catch (Exception ex)
+ {
+ if (!cts.IsCancellationRequested)
+ Console.WriteLine($"An exception occured for OpenChat {channel.channel_id}: {ex.Message}");
+ }
+ }
}
}