From d50ac0ba51efdc43ea7fc8f4f29bab2367050642 Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Tue, 27 Jun 2023 16:00:55 +0200 Subject: [PATCH] - Fix Messages_GetAllDialogs (MessageEmpty) - Fix AnalyzeInviteLink (public channel with join request) - Added Contacts_ResolvedPeer.Channel property closes #166 --- .github/workflows/telegram-api.yml | 2 +- FAQ.md | 2 +- README.md | 4 +++- src/Client.Helpers.cs | 31 ++++++++++++++++++------------ src/TL.Helpers.cs | 4 +++- src/WTelegramClient.csproj | 2 +- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/.github/workflows/telegram-api.yml b/.github/workflows/telegram-api.yml index b9f2a14..ca93ff0 100644 --- a/.github/workflows/telegram-api.yml +++ b/.github/workflows/telegram-api.yml @@ -17,7 +17,7 @@ jobs: support-label: 'telegram api' issue-comment: > **Github Issues** should be used only for problems with the library itself. - + For questions about Telegram API usage, you can search the [API official documentation](https://core.telegram.org/api#getting-started) or [click here to ask your question on **StackOverflow**](https://stackoverflow.com/questions/ask?tags=c%23+wtelegramclient+telegram-api) so the whole community can help and benefit. close-issue: true diff --git a/FAQ.md b/FAQ.md index 63bae26..2a56bdd 100644 --- a/FAQ.md +++ b/FAQ.md @@ -203,7 +203,7 @@ If Telegram servers decide to shutdown this secondary connection, it's not an is This should be transparent and pending API calls should automatically be resent upon reconnection. You can choose to increase `MaxAutoReconnects` if it happens too often because your Internet connection is unstable. -3) If you reach `MaxAutoReconnects` disconnections, then the **OnOther** event handler will receive a `ReactorError` object to notify you of the problem, +3) If you reach `MaxAutoReconnects` disconnections or a reconnection fails, then the **OnOther** event handler will receive a `ReactorError` object to notify you of the problem, and pending API calls throw the network IOException. In this case, the recommended action would be to dispose the client and recreate one (see example [Program_ReactorError.cs](https://github.com/wiz0u/WTelegramClient/blob/master/Examples/Program_ReactorError.cs)) diff --git a/README.md b/README.md index ee61a5e..d985682 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ [![API Layer](https://img.shields.io/badge/API_Layer-158-blueviolet)](https://corefork.telegram.org/methods) [![NuGet version](https://img.shields.io/nuget/v/WTelegramClient?color=00508F)](https://www.nuget.org/packages/WTelegramClient/) -[![NuGet prerelease](https://img.shields.io/nuget/vpre/WTelegramClient?color=C09030&label=dev+nuget)](https://www.nuget.org/packages/WTelegramClient/) +[![NuGet prerelease](https://img.shields.io/nuget/vpre/WTelegramClient?color=C09030&label=dev+nuget)](https://www.nuget.org/packages/WTelegramClient/absoluteLatest) [![Donate](https://img.shields.io/badge/Help_this_project:-Donate-ff4444)](https://www.buymeacoffee.com/wizou) ## _Telegram Client API library written 100% in C# and .NET_ @@ -91,6 +91,8 @@ Since version 3.0.0, a new approach to login/configuration has been added. Some ```csharp WTelegram.Client client = new WTelegram.Client(YOUR_API_ID, "YOUR_API_HASH"); // this constructor doesn't need a Config method await DoLogin("+12025550156"); // initial call with user's phone_number +... +//client.Dispose(); // the client must be disposed when you're done running your userbot. async Task DoLogin(string loginInfo) // (add this method to your code) { diff --git a/src/Client.Helpers.cs b/src/Client.Helpers.cs index 4bfc62c..28602c8 100644 --- a/src/Client.Helpers.cs +++ b/src/Client.Helpers.cs @@ -473,10 +473,15 @@ namespace WTelegram { dialogList.AddRange(dialogs.Dialogs); messageList.AddRange(dialogs.Messages); - var lastDialog = dialogs.Dialogs[^1]; - var lastMsg = dialogs.Messages.LastOrDefault(m => m.Peer.ID == lastDialog.Peer.ID && m.ID == lastDialog.TopMessage); - var offsetPeer = dialogs.UserOrChat(lastDialog).ToInputPeer(); - dialogs = await this.Messages_GetDialogs(lastMsg?.Date ?? default, lastDialog.TopMessage, offsetPeer, folder_id: folder_id); + int last = dialogs.Dialogs.Length - 1; + var lastDialog = dialogs.Dialogs[last]; + var lastPeer = dialogs.UserOrChat(lastDialog).ToInputPeer(); + var lastMsgId = lastDialog.TopMessage; + retryDate: + var lastDate = dialogs.Messages.LastOrDefault(m => m.Peer.ID == lastDialog.Peer.ID && m.ID == lastDialog.TopMessage)?.Date ?? default; + if (lastDate == default) + if (--last < 0) break; else { lastDialog = dialogs.Dialogs[last]; goto retryDate; } + dialogs = await this.Messages_GetDialogs(lastDate, lastMsgId, lastPeer, folder_id: folder_id); if (dialogs is not Messages_Dialogs md) break; foreach (var (key, value) in md.chats) mds.chats[key] = value; foreach (var (key, value) in md.users) mds.users[key] = value; @@ -682,8 +687,8 @@ namespace WTelegram private static readonly char[] UrlSeparator = new[] { '?', '#', '/' }; - /// Return information about a chat/channel based on Invite Link - /// Public link or Invite link, like https://t.me/+InviteHash, https://t.me/joinchat/InviteHash or https://t.me/channelname
Also work without https:// prefix + /// Return information about a chat/channel based on Invite Link or Public Link + /// Public link or Invite link, like https://t.me/+InviteHash, https://t.me/joinchat/InviteHash or https://t.me/channelname
Works also without https:// prefix /// to also join the chat/channel /// previously collected chats, to prevent unnecessary ResolveUsername /// a Chat or Channel, possibly partial Channel information only (with flag ) @@ -703,10 +708,12 @@ namespace WTelegram { var chat = await CachedOrResolveUsername(url[start..end], chats); if (join && chat is Channel channel) - { - var res = await this.Channels_JoinChannel(channel); - chat = res.Chats[chat.ID]; - } + try + { + var res = await this.Channels_JoinChannel(channel); + chat = res.Chats[channel.id]; + } + catch (RpcException ex) when (ex.Code == 400 && ex.Message == "INVITE_REQUEST_SENT") { } return chat; } var chatInvite = await this.Messages_CheckChatInvite(hash); @@ -748,11 +755,11 @@ namespace WTelegram return null; } - /// Return chat and message details based on a message URL + /// Return chat and message details based on a Message Link (URL) /// Message Link, like https://t.me/c/1234567890/1234 or https://t.me/channelname/1234 /// previously collected chats, to prevent unnecessary ResolveUsername /// Structure containing the message, chat and user details - /// If link is for private group (t.me/c/..), user must have joined the group + /// If link is for private group (t.me/c/..), user must have joined that group public async Task GetMessageByLink(string url, IDictionary chats = null) { int start = url.IndexOf("//"); diff --git a/src/TL.Helpers.cs b/src/TL.Helpers.cs index d2c888a..4ff1deb 100644 --- a/src/TL.Helpers.cs +++ b/src/TL.Helpers.cs @@ -543,8 +543,10 @@ namespace TL public static implicit operator InputPeer(Contacts_ResolvedPeer resolved) => resolved?.UserOrChat.ToInputPeer(); /// A , or if the username was for a channel public User User => peer is PeerUser pu ? users[pu.user_id] : null; - /// A or , or if the username was for a user + /// A or , or if the username was for a user public ChatBase Chat => peer is PeerChannel or PeerChat ? chats[peer.ID] : null; + /// A , or if the username was for a user or for a forbidden channel + public Channel Channel => peer is PeerChannel pc ? chats[pc.channel_id] as Channel : null; } partial class Updates_ChannelDifferenceBase diff --git a/src/WTelegramClient.csproj b/src/WTelegramClient.csproj index ad1e28b..0fade3e 100644 --- a/src/WTelegramClient.csproj +++ b/src/WTelegramClient.csproj @@ -23,7 +23,7 @@ git Telegram;MTProto;Client;Api;UserBot;TLSharp README.md - $(ReleaseNotes.Replace("|", "%0D%0A").Replace(" - ","%0D%0A- ").Replace(" ", "%0D%0A%0D%0A")) + $(ReleaseNotes.Replace("\"", "%22").Replace("|", "%0D%0A").Replace(" - ","%0D%0A- ").Replace(" ", "%0D%0A%0D%0A")) 0419;1573;1591;NETSDK1138 TRACE;OBFUSCATION