Move ParseX logic out of TL.MTProto

This commit is contained in:
Wizou 2022-04-13 15:53:06 +02:00
parent 796b49546e
commit 0f928d4992
4 changed files with 24 additions and 28 deletions

View file

@ -23,8 +23,8 @@ More examples can also be found in answers to [StackOverflow questions](https://
<a name="msg-by-name"></a> <a name="msg-by-name"></a>
### Send a message to someone by @username ### Send a message to someone by @username
```csharp ```csharp
var resolved = await client.Contacts_ResolveUsername("username"); // without the @ var resolved = await client.Contacts_ResolveUsername("MyEch0_Bot"); // username without the @
await client.SendMessageAsync(resolved, "Hello!"); await client.SendMessageAsync(resolved, "/start");
``` ```
*Note: This also works if the @username points to a channel/group, but you must already have joined that channel before posting there. *Note: This also works if the @username points to a channel/group, but you must already have joined that channel before posting there.
If the username is invalid/unused, the API call raises an exception.* If the username is invalid/unused, the API call raises an exception.*

View file

@ -1,13 +1,15 @@
[![NuGet version](https://img.shields.io/nuget/v/WTelegramClient)](https://www.nuget.org/packages/WTelegramClient/) [![NuGet version](https://img.shields.io/nuget/v/WTelegramClient)](https://www.nuget.org/packages/WTelegramClient/)
[![Build Status](https://img.shields.io/azure-devops/build/wiz0u/WTelegramClient/7)](https://dev.azure.com/wiz0u/WTelegramClient/_build?definitionId=7) [![Build Status](https://img.shields.io/azure-devops/build/wiz0u/WTelegramClient/7)](https://dev.azure.com/wiz0u/WTelegramClient/_build?definitionId=7)
[![API Layer](https://img.shields.io/badge/API_Layer-139-blueviolet)](https://corefork.telegram.org/methods) [![API Layer](https://img.shields.io/badge/API_Layer-140-blueviolet)](https://corefork.telegram.org/methods)
[![dev nuget](https://img.shields.io/badge/dynamic/json?color=ffc040&label=dev%20nuget&query=%24.versions%5B0%5D&url=https%3A%2F%2Fpkgs.dev.azure.com%2Fwiz0u%2F81bd92b7-0bb9-4701-b426-09090b27e037%2F_packaging%2F46ce0497-7803-4bd4-8c6c-030583e7c371%2Fnuget%2Fv3%2Fflat2%2Fwtelegramclient%2Findex.json)](https://dev.azure.com/wiz0u/WTelegramClient/_packaging?_a=package&feed=WTelegramClient&package=WTelegramClient&protocolType=NuGet) [![dev nuget](https://img.shields.io/badge/dynamic/json?color=ffc040&label=dev%20nuget&query=%24.versions%5B0%5D&url=https%3A%2F%2Fpkgs.dev.azure.com%2Fwiz0u%2F81bd92b7-0bb9-4701-b426-09090b27e037%2F_packaging%2F46ce0497-7803-4bd4-8c6c-030583e7c371%2Fnuget%2Fv3%2Fflat2%2Fwtelegramclient%2Findex.json)](https://dev.azure.com/wiz0u/WTelegramClient/_packaging?_a=package&feed=WTelegramClient&package=WTelegramClient&protocolType=NuGet)
[![Support Chat](https://img.shields.io/badge/Chat_with_us-on_Telegram-0088cc)](https://t.me/WTelegramClient) [![Support Chat](https://img.shields.io/badge/Chat_with_us-on_Telegram-0088cc)](https://t.me/WTelegramClient)
[![Donate](https://img.shields.io/badge/Help_this_project:-Donate-ff4444)](http://wizou.fr/donate.html) [![Donate](https://img.shields.io/badge/Help_this_project:-Donate-ff4444)](http://wizou.fr/donate.html)
## _Telegram Client API library written 100% in C# and .NET Standard_ ## _Telegram Client API library written 100% in C# and .NET Standard_
This ReadMe is a **quick but important tutorial** to learn the fundamentals about this library. Please read it all. This ReadMe is a **quick but important tutorial** to learn the fundamentals about this library. Please read it all.
This library allows you to connect to Telegram and control a user programmatically (or a bot, but [Telegram.Bot](https://github.com/TelegramBots/Telegram.Bot) is much easier for that).
All the Telegram Client APIs are supported so you can do everything the user could do with a full Telegram GUI client.
>⚠️ This library relies on asynchronous C# programming (`async/await`) so make sure you are familiar with this advanced topic before proceeding. >⚠️ This library relies on asynchronous C# programming (`async/await`) so make sure you are familiar with this advanced topic before proceeding.
>If you are a beginner in C#, starting a project based on this library might not be a great idea. >If you are a beginner in C#, starting a project based on this library might not be a great idea.

View file

@ -1110,11 +1110,21 @@ namespace WTelegram
switch (result) switch (result)
{ {
case T resultT: return resultT; case T resultT: return resultT;
case RpcError rpcError: case RpcError { error_code: var code, error_message: var message }:
var x = rpcError.ParseX(); int x = -1;
if (rpcError.error_code == 303 && rpcError.error_message.EndsWith("_MIGRATE_X")) for (int index = message.Length - 1; index > 0 && (index = message.LastIndexOf('_', index - 1)) >= 0;)
if (message[index + 1] is >= '0' and <= '9')
{
int end = ++index;
do end++; while (end < message.Length && message[end] is >= '0' and <= '9');
x = int.Parse(message[index..end]);
message = $"{message[0..index]}X{message[end..]}";
break;
}
if (code == 303 && message.EndsWith("_MIGRATE_X"))
{ {
if (rpcError.error_message != "FILE_MIGRATE_X") if (message != "FILE_MIGRATE_X")
{ {
// this is a hack to migrate _dcSession in-place (staying in same Client): // this is a hack to migrate _dcSession in-place (staying in same Client):
Session.DCSession dcSession; Session.DCSession dcSession;
@ -1129,7 +1139,7 @@ namespace WTelegram
goto retry; goto retry;
} }
} }
else if (rpcError.error_code == 420 && rpcError.error_message.EndsWith("_WAIT_X")) else if (code == 420 && message.EndsWith("_WAIT_X"))
{ {
if (x <= FloodRetryThreshold) if (x <= FloodRetryThreshold)
{ {
@ -1137,17 +1147,17 @@ namespace WTelegram
goto retry; goto retry;
} }
} }
else if (rpcError.error_code == -503 && !got503) else if (code == -503 && !got503)
{ {
got503 = true; got503 = true;
goto retry; goto retry;
} }
else if (rpcError.error_code == 500 && rpcError.error_message == "AUTH_RESTART") else if (code == 500 && message == "AUTH_RESTART")
{ {
_session.UserId = 0; // force a full login authorization flow, next time _session.UserId = 0; // force a full login authorization flow, next time
lock (_session) _session.Save(); lock (_session) _session.Save();
} }
throw new RpcException(rpcError.error_code, rpcError.error_message, x); throw new RpcException(code, message, x);
case ReactorError: case ReactorError:
goto retry; goto retry;
default: default:

View file

@ -198,22 +198,6 @@ namespace TL
{ {
public int error_code; public int error_code;
public string error_message; public string error_message;
public int ParseX() // ⚠ Method replace number in error_message with a X
{
for (int index = error_message.Length - 1; index > 0 && (index = error_message.LastIndexOf('_', index - 1)) >= 0;)
{
if (error_message[index + 1] is >= '0' and <= '9')
{
int end = ++index;
do end++; while (end < error_message.Length && error_message[end] is >= '0' and <= '9');
var x = int.Parse(error_message[index..end]);
error_message = $"{error_message[0..index]}X{error_message[end..]}";
return x;
}
}
return -1;
}
} }
public abstract class RpcDropAnswer : IObject { } public abstract class RpcDropAnswer : IObject { }