Throw exception if calling API without connecting first.

This commit is contained in:
Wizou 2022-08-01 19:06:31 +02:00
parent 668b19e3e8
commit 3e1506d0a7
4 changed files with 13 additions and 11 deletions

View file

@ -345,7 +345,7 @@ See [Examples/Program_CollectAccessHash.cs](Examples/Program_CollectAccessHash.c
<a name="proxy"></a> <a name="proxy"></a>
### Use a proxy to connect to Telegram ### 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 ```csharp
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable); using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
client.TcpHandler = async (address, port) => client.TcpHandler = async (address, port) =>

View file

@ -32,16 +32,16 @@ namespace WTelegramClientTest
} }
} }
// in this example, we're not using async/await, so we just return Task.CompletedTask // if not using async/await, we could just return Task.CompletedTask
private static Task Client_OnUpdate(IObject arg) 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); updates.CollectUsersChats(Users, Chats);
foreach (var update in updates.UpdateList) foreach (var update in updates.UpdateList)
switch (update) switch (update)
{ {
case UpdateNewMessage unm: DisplayMessage(unm.message); break; case UpdateNewMessage unm: await DisplayMessage(unm.message); break;
case UpdateEditMessage uem: DisplayMessage(uem.message, true); 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 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 UpdateDeleteMessages udm: Console.WriteLine($"{udm.messages.Length} message(s) deleted"); break;
case UpdateUserTyping uut: Console.WriteLine($"{User(uut.user_id)} is {uut.action}"); 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; 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 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): "); if (edit) Console.Write("(Edit): ");
switch (messageBase) 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 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; 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}"; private static string User(long id) => Users.TryGetValue(id, out var user) ? user.ToString() : $"User {id}";

6
FAQ.md
View file

@ -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. 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.
<a name="shutdown"></a> <a name="shutdown"></a>
#### 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: 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)*. In this case, you can use the `PingInterval` property to increase the delay between pings *(for example 300 seconds instead of 60)*.
<a name="TLSharp"></a> <a name="TLSharp"></a>
#### 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. 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... 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...
<a name="heroku"></a><a name="vps"></a><a name="host"></a> <a name="heroku"></a><a name="vps"></a><a name="host"></a>
#### 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, 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). and hosted online on any [VPS Hosting](https://www.google.com/search?q=vps+hosting) (Virtual Private Server).

View file

@ -1046,6 +1046,7 @@ namespace WTelegram
private async Task SendAsync(IObject msg, bool isContent, Rpc rpc = null) 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; isContent &= _dcSession.AuthKeyID != 0;
(long msgId, int seqno) = NewMsgId(isContent); (long msgId, int seqno) = NewMsgId(isContent);
if (rpc != null) if (rpc != null)