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>
|
<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.*
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
[](https://www.nuget.org/packages/WTelegramClient/)
|
[](https://www.nuget.org/packages/WTelegramClient/)
|
||||||
[](https://dev.azure.com/wiz0u/WTelegramClient/_build?definitionId=7)
|
[](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://dev.azure.com/wiz0u/WTelegramClient/_packaging?_a=package&feed=WTelegramClient&package=WTelegramClient&protocolType=NuGet)
|
||||||
[](https://t.me/WTelegramClient)
|
[](https://t.me/WTelegramClient)
|
||||||
[](http://wizou.fr/donate.html)
|
[](http://wizou.fr/donate.html)
|
||||||
|
|
@ -8,6 +8,8 @@
|
||||||
## _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.
|
||||||
|
|
|
||||||
|
|
@ -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')
|
||||||
{
|
{
|
||||||
if (rpcError.error_message != "FILE_MIGRATE_X")
|
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 (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:
|
||||||
|
|
|
||||||
|
|
@ -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 { }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue