From 3e1506d0a77262db5f186b61ee810b3fbf2ccfb4 Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Mon, 1 Aug 2022 19:06:31 +0200 Subject: [PATCH] Throw exception if calling API without connecting first. --- EXAMPLES.md | 2 +- Examples/Program_ListenUpdates.cs | 15 ++++++++------- FAQ.md | 6 +++--- src/Client.cs | 1 + 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index cf38563..e0ac068 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -345,7 +345,7 @@ See [Examples/Program_CollectAccessHash.cs](Examples/Program_CollectAccessHash.c ### Use a proxy to connect to Telegram -SOCKS/HTTP proxies can be used through the `client.TcpHandler` delegate and a proxy library like [StarkSoftProxy](https://www.nuget.org/packages/StarkSoftProxy/): +SOCKS/HTTPS proxies can be used through the `client.TcpHandler` delegate and a proxy library like [StarkSoftProxy](https://www.nuget.org/packages/StarkSoftProxy/): ```csharp using var client = new WTelegram.Client(Environment.GetEnvironmentVariable); client.TcpHandler = async (address, port) => diff --git a/Examples/Program_ListenUpdates.cs b/Examples/Program_ListenUpdates.cs index 34567fb..9060e7b 100644 --- a/Examples/Program_ListenUpdates.cs +++ b/Examples/Program_ListenUpdates.cs @@ -32,16 +32,16 @@ namespace WTelegramClientTest } } - // in this example, we're not using async/await, so we just return Task.CompletedTask - private static Task Client_OnUpdate(IObject arg) + // if not using async/await, we could just return Task.CompletedTask + private static async Task Client_OnUpdate(IObject arg) { - if (arg is not UpdatesBase updates) return Task.CompletedTask; + if (arg is not UpdatesBase updates) return; updates.CollectUsersChats(Users, Chats); foreach (var update in updates.UpdateList) switch (update) { - case UpdateNewMessage unm: DisplayMessage(unm.message); break; - case UpdateEditMessage uem: DisplayMessage(uem.message, true); break; + case UpdateNewMessage unm: await DisplayMessage(unm.message); break; + case UpdateEditMessage uem: await DisplayMessage(uem.message, true); break; case UpdateDeleteChannelMessages udcm: Console.WriteLine($"{udcm.messages.Length} message(s) deleted in {Chat(udcm.channel_id)}"); break; case UpdateDeleteMessages udm: Console.WriteLine($"{udm.messages.Length} message(s) deleted"); break; case UpdateUserTyping uut: Console.WriteLine($"{User(uut.user_id)} is {uut.action}"); break; @@ -53,10 +53,10 @@ namespace WTelegramClientTest case UpdateUserPhoto uup: Console.WriteLine($"{User(uup.user_id)} has changed profile photo"); break; default: Console.WriteLine(update.GetType().Name); break; // there are much more update types than the above cases } - return Task.CompletedTask; } - private static void DisplayMessage(MessageBase messageBase, bool edit = false) + // in this example method, we're not using async/await, so we just return Task.CompletedTask + private static Task DisplayMessage(MessageBase messageBase, bool edit = false) { if (edit) Console.Write("(Edit): "); switch (messageBase) @@ -64,6 +64,7 @@ namespace WTelegramClientTest case Message m: Console.WriteLine($"{Peer(m.from_id) ?? m.post_author} in {Peer(m.peer_id)}> {m.message}"); break; case MessageService ms: Console.WriteLine($"{Peer(ms.from_id)} in {Peer(ms.peer_id)} [{ms.action.GetType().Name[13..]}]"); break; } + return Task.CompletedTask; } private static string User(long id) => Users.TryGetValue(id, out var user) ? user.ToString() : $"User {id}"; diff --git a/FAQ.md b/FAQ.md index 6a08d53..50f1d90 100644 --- a/FAQ.md +++ b/FAQ.md @@ -177,7 +177,7 @@ To help determine if `chats.chats` is empty or does not contain a certain chat, or simply use a debugger: Place a breakpoint after the Messages_GetAllChats call, run the program up to there, and use a Watch pane to display the content of the chats.chats dictionary. -#### 10. I get "Connection shut down" errors in my logs +#### 11. I get "Connection shut down" errors in my logs There are various reasons why you may get this error. Here are the explanation and how to solve it: @@ -200,7 +200,7 @@ you might also get Connection shutdown because your client couldn't send Pings t In this case, you can use the `PingInterval` property to increase the delay between pings *(for example 300 seconds instead of 60)*. -#### 11. How to migrate from TLSharp? How to sign-in/sign-up/register account properly? +#### 12. How to migrate from TLSharp? How to sign-in/sign-up/register account properly? First, make sure you read the [ReadMe documentation](README.md) completely, it contains essential information and a quick tutorial to easily understand how to correctly use the library. @@ -226,7 +226,7 @@ In particular, it will detect and handle automatically and properly the various Contrary to TLSharp, WTelegramClient supports MTProto v2.0 (more secured), transport obfuscation, protocol security checks, MTProto Proxy, real-time updates, multiple DC connections, API documentation in Intellisense... -#### 12. How to host my userbot online? +#### 13. How to host my userbot online? If you need your userbot to run 24/7, you would typically design your userbot as a Console program, compiled for Linux or Windows, and hosted online on any [VPS Hosting](https://www.google.com/search?q=vps+hosting) (Virtual Private Server). diff --git a/src/Client.cs b/src/Client.cs index d4dd9fb..d189979 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -1046,6 +1046,7 @@ namespace WTelegram private async Task SendAsync(IObject msg, bool isContent, Rpc rpc = null) { + if (_reactorTask == null) throw new ApplicationException("You must connect to Telegram first"); isContent &= _dcSession.AuthKeyID != 0; (long msgId, int seqno) = NewMsgId(isContent); if (rpc != null)