From 6382f10dae0f6095bfcdcc55c0d86eb4a927d7eb Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sun, 15 Jan 2023 03:08:14 +0100 Subject: [PATCH] Add /me command --- tgcli/Command.cs | 52 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/tgcli/Command.cs b/tgcli/Command.cs index fd1e705..c11449d 100644 --- a/tgcli/Command.cs +++ b/tgcli/Command.cs @@ -29,6 +29,7 @@ namespace tgcli { new CloseCommand(), new EditCommand(), new ReplyCommand(), + new MeCommand(), new ReplyOffsetCommand(), new ReplyDirectCommand(), new HistoryCommand(), @@ -226,7 +227,7 @@ namespace tgcli { return; } - if (GetSecretChats().Count(p => ((TdApi.ChatType.ChatTypeSecret) p.Type).UserId == userId) > 0) { + if (GetSecretChats().Count(p => ((TdApi.ChatType.ChatTypeSecret)p.Type).UserId == userId) > 0) { lock (@lock) messageQueue.Add($"{Ansi.Red}[tgcli] You already have a secret chat with the specified user."); return; @@ -331,7 +332,7 @@ namespace tgcli { return; } - var chat = GetSecretChats().Find(p => ((TdApi.ChatType.ChatTypeSecret) p.Type).UserId == userId); + var chat = GetSecretChats().Find(p => ((TdApi.ChatType.ChatTypeSecret)p.Type).UserId == userId); if (chat == null) { lock (@lock) messageQueue.Add($"{Ansi.Red}[tgcli] No matching secret chat found."); @@ -550,7 +551,7 @@ namespace tgcli { messageQueue.Add($"{Ansi.Yellow}[tgcli] Listing {chats.Count} chats."); chats.ForEach(chat => { string line; - var type = (TdApi.ChatType.ChatTypePrivate) chat.Type; + var type = (TdApi.ChatType.ChatTypePrivate)chat.Type; var user = GetUser(type.UserId); line = $"{Ansi.Bold}{Ansi.Yellow}@{user.Usernames.ActiveUsernames.First()} {Ansi.Magenta}{chat.Title}"; messageQueue.Add(line); @@ -589,7 +590,7 @@ namespace tgcli { lock (@lock) { messageQueue.Add($"{Ansi.Yellow}[tgcli] Listing {secretChats.Count} secret chats:"); secretChats.ForEach(chat => { - messageQueue.Add($"{Ansi.Bold}{Ansi.Red}[sec] {chat.Title} -> {chat.Id} ({GetSecretChat(((TdApi.ChatType.ChatTypeSecret) chat.Type).SecretChatId).State.DataType})"); + messageQueue.Add($"{Ansi.Bold}{Ansi.Red}[sec] {chat.Title} -> {chat.Id} ({GetSecretChat(((TdApi.ChatType.ChatTypeSecret)chat.Type).SecretChatId).State.DataType})"); }); } } @@ -629,7 +630,7 @@ namespace tgcli { public override void Handler(List inputParams) { try { if (inputParams.Count == 0) { - SetInputLine("/e " + ((TdApi.MessageContent.MessageText) lastMessage?.Content)?.Text?.Text); + SetInputLine("/e " + ((TdApi.MessageContent.MessageText)lastMessage?.Content)?.Text?.Text); Emojis.ForEach(em => SetInputLine(currentInputLine.Replace(em.Item2, em.Item1))); return; } @@ -670,6 +671,43 @@ namespace tgcli { } } + public class MeCommand : Command { + public MeCommand() : base("me", "", "sends an action message", "", -1) { } + + public override void Handler(List inputParams) { + try { + if (inputParams.Count == 0) { + lock (@lock) + messageQueue.Add($"{Ansi.Red}[tgcli] Invalid syntax, check /help for more information."); + return; + } + + var message = inputParams.Aggregate((current, param) => current + " " + param).Trim(); + + if (currentChatId == 0) { + lock (@lock) + messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue."); + return; + } + + if (string.IsNullOrWhiteSpace(message)) { + lock (@lock) + messageQueue.Add($"{Ansi.Red}[tgcli] No message specified, cannot continue."); + return; + } + + var user = client.GetMeAsync().Result; + var username = user.Usernames?.ActiveUsernames?.FirstOrDefault() ?? user.FirstName; + + SendMessage($"* {username} {message}", currentChatId); + } + catch { + lock (@lock) + messageQueue.Add($"{Ansi.Red}[tgcli] Unknown error sending action messsage."); + } + } + } + public class ReplyCommand : Command { public ReplyCommand() : base("r", "", "matches message in history to reply to", "", -1) { } @@ -692,9 +730,7 @@ namespace tgcli { var query = inputParams.Aggregate((current, param) => current + " " + param).Trim(); var result = history.Where(m => m.Content is TdApi.MessageContent.MessageText) - .FirstOrDefault(m => ((TdApi.MessageContent.MessageText) m.Content) - .Text.Text.ToLowerInvariant() - .Contains(query.ToLower())); + .FirstOrDefault(m => ((TdApi.MessageContent.MessageText)m.Content).Text.Text.ToLowerInvariant().Contains(query.ToLower())); if (result == null) { lock (@lock)