ToString for SendMessageAction

This commit is contained in:
Wizou 2021-10-01 05:46:51 +02:00
parent b01eed2042
commit 2520a57f20
3 changed files with 31 additions and 12 deletions

View file

@ -24,7 +24,7 @@ namespace WTelegramClientTest
await client.ConnectAsync(); await client.ConnectAsync();
var my = await client.LoginUserIfNeeded(); var my = await client.LoginUserIfNeeded();
users[my.id] = my; users[my.id] = my;
// note that on logging, Telegram may sends a bunch of updates/messages that happened in the past and were not acknowledged // note that on login Telegram may sends a bunch of updates/messages that happened in the past and were not acknowledged
Console.WriteLine($"We are logged-in as {my.username ?? my.first_name + " " + my.last_name} (id {my.id})"); Console.WriteLine($"We are logged-in as {my.username ?? my.first_name + " " + my.last_name} (id {my.id})");
var dialogsBase = await client.Messages_GetDialogs(default, 0, null, 0, 0); var dialogsBase = await client.Messages_GetDialogs(default, 0, null, 0, 0);
if (dialogsBase is Messages_Dialogs dialogs) if (dialogsBase is Messages_Dialogs dialogs)
@ -46,7 +46,7 @@ namespace WTelegramClientTest
private static readonly Dictionary<long, UserBase> users = new(); private static readonly Dictionary<long, UserBase> users = new();
private static readonly Dictionary<long, ChatBase> chats = new(); private static readonly Dictionary<long, ChatBase> chats = new();
private static string AUser(long user_id) => users.TryGetValue(user_id, out var user) ? user.DisplayName : $"User {user_id}"; private static string AUser(long user_id) => users.TryGetValue(user_id, out var user) ? user.DisplayName : $"User {user_id}";
private static string AChat(long chat_id) => chats.TryGetValue(chat_id, out var chat) ? chat.Title : $"Chat {chat_id}"; private static string AChat(long chat_id) => chats.TryGetValue(chat_id, out var chat) ? chat.ToString() : $"Chat {chat_id}";
private static string APeer(Peer peer) => peer is null ? null : peer is PeerUser user ? AUser(user.user_id) private static string APeer(Peer peer) => peer is null ? null : peer is PeerUser user ? AUser(user.user_id)
: peer is PeerChat chat ? AChat(chat.chat_id) : peer is PeerChannel channel ? AChat(channel.channel_id) : $"Peer {peer.ID}"; : peer is PeerChat chat ? AChat(chat.chat_id) : peer is PeerChannel channel ? AChat(channel.channel_id) : $"Peer {peer.ID}";
@ -80,9 +80,9 @@ namespace WTelegramClientTest
case UpdateEditMessage uem: Console.Write("(Edit): "); DisplayMessage(uem.message); break; case UpdateEditMessage uem: Console.Write("(Edit): "); DisplayMessage(uem.message); break;
case UpdateDeleteChannelMessages udcm: Console.WriteLine($"{udcm.messages.Length} message(s) deleted in {AChat(udcm.channel_id)}"); break; case UpdateDeleteChannelMessages udcm: Console.WriteLine($"{udcm.messages.Length} message(s) deleted in {AChat(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($"{AUser(uut.user_id)} is {uut.action.GetType().Name[11..^6]}"); break; case UpdateUserTyping uut: Console.WriteLine($"{AUser(uut.user_id)} is {uut.action}"); break;
case UpdateChatUserTyping ucut: Console.WriteLine($"{APeer(ucut.from_id)} is {ucut.action.GetType().Name[11..^6]} in {AChat(ucut.chat_id)}"); break; case UpdateChatUserTyping ucut: Console.WriteLine($"{APeer(ucut.from_id)} is {ucut.action} in {AChat(ucut.chat_id)}"); break;
case UpdateChannelUserTyping ucut2: Console.WriteLine($"{APeer(ucut2.from_id)} is {ucut2.action.GetType().Name[11..^6]} in {AChat(ucut2.channel_id)}"); break; case UpdateChannelUserTyping ucut2: Console.WriteLine($"{APeer(ucut2.from_id)} is {ucut2.action} in {AChat(ucut2.channel_id)}"); break;
case UpdateChatParticipants { participants: ChatParticipants cp }: Console.WriteLine($"{cp.participants.Length} participants in {AChat(cp.chat_id)}"); break; case UpdateChatParticipants { participants: ChatParticipants cp }: Console.WriteLine($"{cp.participants.Length} participants in {AChat(cp.chat_id)}"); break;
case UpdateUserStatus uus: Console.WriteLine($"{AUser(uus.user_id)} is now {uus.status.GetType().Name[10..]}"); break; case UpdateUserStatus uus: Console.WriteLine($"{AUser(uus.user_id)} is now {uus.status.GetType().Name[10..]}"); break;
case UpdateUserName uun: Console.WriteLine($"{AUser(uun.user_id)} has changed profile name: @{uun.username} {uun.first_name} {uun.last_name}"); break; case UpdateUserName uun: Console.WriteLine($"{AUser(uun.user_id)} has changed profile name: @{uun.username} {uun.first_name} {uun.last_name}"); break;

View file

@ -1,7 +1,7 @@
[![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/)
[![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)
[![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-133-blueviolet)](https://schema.horner.tj) [![API Layer](https://img.shields.io/badge/API_Layer-133-blueviolet)](https://corefork.telegram.org/methods)
[![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)
# <img src="logo.png" width="32"/> WTelegramClient # <img src="logo.png" width="32"/> WTelegramClient
@ -27,7 +27,7 @@ Then it will attempt to sign-in as a user for which you must enter the **phone_n
If the verification succeeds but the phone number is unknown to Telegram, the user might be prompted to sign-up (accepting the Terms of Service) and enter their **first_name** and **last_name**. If the verification succeeds but the phone number is unknown to Telegram, the user might be prompted to sign-up (accepting the Terms of Service) and enter their **first_name** and **last_name**.
And that's it, you now have access to the [full range of Telegram services](https://core.telegram.org/methods), mainly through calls to `await client.Some_TL_Method(...)` And that's it, you now have access to the [full range of Telegram services](https://corefork.telegram.org/methods), mainly through calls to `await client.Some_TL_Method(...)`
# Saved session # Saved session
If you run this program again, you will notice that only **api_id** and **api_hash** are requested, the other prompts are gone and you are automatically logged-on and ready to go. If you run this program again, you will notice that only **api_id** and **api_hash** are requested, the other prompts are gone and you are automatically logged-on and ready to go.
@ -64,12 +64,12 @@ Its `int` argument is the log severity, compatible with the classic [LogLevel en
The Telegram API makes extensive usage of base and derived classes, so be ready to use the various syntaxes C# offer to check/cast base classes into the more useful derived classes (`is`, `as`, `case DerivedType` ) The Telegram API makes extensive usage of base and derived classes, so be ready to use the various syntaxes C# offer to check/cast base classes into the more useful derived classes (`is`, `as`, `case DerivedType` )
To find which derived classes are available for a given base class, the fastest is to check our [TL.Schema.cs](src/TL.Schema.cs) source file as they are listed in groups. To find which derived classes are available for a given base class, the fastest is to check our [TL.Schema.cs](https://github.com/wiz0u/WTelegramClient/blob/master/src/TL.Schema.cs) source file as they are listed in groups.
Intellisense tooltips on API structures/methods will also display a web link to the adequate Telegram documentation page. Intellisense tooltips on API structures/methods will also display a web link to the adequate Telegram documentation page.
The Telegram [API object classes](https://core.telegram.org/schema) are defined in the `TL` namespace, and the [API functions](https://core.telegram.org/methods) are available as async methods of `Client`. The Telegram [API object classes](https://corefork.telegram.org/schema) are defined in the `TL` namespace, and the [API functions](https://corefork.telegram.org/methods) are available as async methods of `Client`.
Below is an example of calling the [messages.getAllChats](https://core.telegram.org/method/messages.getAllChats) API function and enumerating the various groups/channels the user is in, and then using `client.SendMessageAsync` helper function to easily send a message: Below is an example of calling the [messages.getAllChats](https://corefork.telegram.org/method/messages.getAllChats) API function and enumerating the various groups/channels the user is in, and then using `client.SendMessageAsync` helper function to easily send a message:
```csharp ```csharp
using TL; using TL;
... ...
@ -98,7 +98,7 @@ await client.SendMessageAsync(target, "Hello, World");
The Client class also offers an `Update` event that is triggered when Telegram servers sends unsollicited Updates or notifications/information/status/service messages, independently of your API requests. The Client class also offers an `Update` event that is triggered when Telegram servers sends unsollicited Updates or notifications/information/status/service messages, independently of your API requests.
An invalid API request can result in a `RpcException` being raised, reflecting the [error code and status text](https://core.telegram.org/api/errors) of the problem. An invalid API request can result in a `RpcException` being raised, reflecting the [error code and status text](https://revgram.github.io/errors.html) of the problem.
The other configuration items that you can override include: **session_pathname, server_address, device_model, system_version, app_version, system_lang_code, lang_pack, lang_code, user_id** The other configuration items that you can override include: **session_pathname, server_address, device_model, system_version, app_version, system_lang_code, lang_pack, lang_code, user_id**
@ -106,7 +106,7 @@ Optional API parameters have a default value of `null` when unset. Passing `null
I've added several useful converters or implicit cast to various API object so that they are more easy to manipulate. I've added several useful converters or implicit cast to various API object so that they are more easy to manipulate.
Beyond the TL async methods, the Client class offers a few other methods to simplify the sending of files, medias or messages. Beyond the TL async methods, the Client class offers a few other methods to simplify the sending/receiving of files, medias or messages.
This library works best with **.NET 5.0+** and is also available for **.NET Standard 2.0** (.NET Framework 4.6.1+ & .NET Core 2.0+) This library works best with **.NET 5.0+** and is also available for **.NET Standard 2.0** (.NET Framework 4.6.1+ & .NET Core 2.0+)

View file

@ -279,4 +279,23 @@ namespace TL
return sb.Append('}').ToString(); return sb.Append('}').ToString();
} }
} }
partial class SendMessageAction
{
public override string ToString()
{
var type = GetType().Name[11..^6];
for (int i = 1; i < type.Length; i++)
if (char.IsUpper(type[i]))
return type.ToLowerInvariant().Insert(i, "ing ");
return type.ToLowerInvariant();
}
}
partial class SpeakingInGroupCallAction { public override string ToString() => "speaking in group call"; }
partial class SendMessageTypingAction { public override string ToString() => "typing"; }
partial class SendMessageCancelAction { public override string ToString() => "stopping"; }
partial class SendMessageGeoLocationAction { public override string ToString() => "selecting a location"; }
partial class SendMessageGamePlayAction { public override string ToString() => "playing a game"; }
partial class SendMessageHistoryImportAction{ public override string ToString() => "importing history"; }
} }