mirror of
https://github.com/wiz0u/WTelegramClient.git
synced 2025-12-06 06:52:01 +01:00
Move ParseX logic out of TL.MTProto
This commit is contained in:
parent
796b49546e
commit
0f928d4992
|
|
@ -23,8 +23,8 @@ More examples can also be found in answers to [StackOverflow questions](https://
|
|||
<a name="msg-by-name"></a>
|
||||
### Send a message to someone by @username
|
||||
```csharp
|
||||
var resolved = await client.Contacts_ResolveUsername("username"); // without the @
|
||||
await client.SendMessageAsync(resolved, "Hello!");
|
||||
var resolved = await client.Contacts_ResolveUsername("MyEch0_Bot"); // username without the @
|
||||
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.
|
||||
If the username is invalid/unused, the API call raises an exception.*
|
||||
|
|
|
|||
|
|
@ -1,13 +1,15 @@
|
|||
[](https://www.nuget.org/packages/WTelegramClient/)
|
||||
[](https://dev.azure.com/wiz0u/WTelegramClient/_build?definitionId=7)
|
||||
[](https://corefork.telegram.org/methods)
|
||||
[](https://corefork.telegram.org/methods)
|
||||
[](https://dev.azure.com/wiz0u/WTelegramClient/_packaging?_a=package&feed=WTelegramClient&package=WTelegramClient&protocolType=NuGet)
|
||||
[](https://t.me/WTelegramClient)
|
||||
[](http://wizou.fr/donate.html)
|
||||
|
||||
## _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.
|
||||
>If you are a beginner in C#, starting a project based on this library might not be a great idea.
|
||||
|
|
|
|||
|
|
@ -1110,11 +1110,21 @@ namespace WTelegram
|
|||
switch (result)
|
||||
{
|
||||
case T resultT: return resultT;
|
||||
case RpcError rpcError:
|
||||
var x = rpcError.ParseX();
|
||||
if (rpcError.error_code == 303 && rpcError.error_message.EndsWith("_MIGRATE_X"))
|
||||
case RpcError { error_code: var code, error_message: var message }:
|
||||
int x = -1;
|
||||
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):
|
||||
Session.DCSession dcSession;
|
||||
|
|
@ -1129,7 +1139,7 @@ namespace WTelegram
|
|||
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)
|
||||
{
|
||||
|
|
@ -1137,17 +1147,17 @@ namespace WTelegram
|
|||
goto retry;
|
||||
}
|
||||
}
|
||||
else if (rpcError.error_code == -503 && !got503)
|
||||
else if (code == -503 && !got503)
|
||||
{
|
||||
got503 = true;
|
||||
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
|
||||
lock (_session) _session.Save();
|
||||
}
|
||||
throw new RpcException(rpcError.error_code, rpcError.error_message, x);
|
||||
throw new RpcException(code, message, x);
|
||||
case ReactorError:
|
||||
goto retry;
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -198,22 +198,6 @@ namespace TL
|
|||
{
|
||||
public int error_code;
|
||||
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 { }
|
||||
|
|
|
|||
Loading…
Reference in a new issue