implement reply matching

This commit is contained in:
Laura Hausmann 2019-12-21 22:20:41 +01:00
parent 3a4cb9e025
commit a3bbbd09a8
Signed by: zotan
GPG key ID: 5EC1D38FFC321311
2 changed files with 81 additions and 4 deletions

View file

@ -29,6 +29,8 @@ namespace telegram {
new CloseCommand(), new CloseCommand(),
new EditCommand(), new EditCommand(),
new ReplyCommand(), new ReplyCommand(),
new ReplyOffsetCommand(),
new ReplyDirectCommand(),
new HistoryCommand(), new HistoryCommand(),
new OpenCommand(), new OpenCommand(),
new UnreadsCommand(), new UnreadsCommand(),
@ -669,7 +671,48 @@ namespace telegram {
} }
public class ReplyCommand : Command { public class ReplyCommand : Command {
public ReplyCommand() : base("r", "", "replies to message", "<offset> <message>", -1) { } public ReplyCommand() : base("r", "", "matches message in history to reply to", "<message query>", -1) { }
public override void Handler(List<string> inputParams) {
try {
if (inputParams.Count == 0) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid syntax, check /help for more information.");
return;
}
if (currentChatId == 0) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
return;
}
var history = GetHistory(currentChatId, 50);
history.Reverse();
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()));
if (result == null) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No match found.");
return;
}
currentInputLine = $"/rd {result.Id} ";
}
catch {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Unknown error searching for message.");
}
}
}
public class ReplyOffsetCommand : Command {
public ReplyOffsetCommand() : base("ro", "", "replies to message", "<offset> <message>", -1) { }
public override void Handler(List<string> inputParams) { public override void Handler(List<string> inputParams) {
try { try {
@ -679,6 +722,12 @@ namespace telegram {
return; return;
} }
if (currentChatId == 0) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
return;
}
var history = GetHistory(currentChatId, 50); var history = GetHistory(currentChatId, 50);
var parsed = int.TryParse(inputParams[0], out var offset); var parsed = int.TryParse(inputParams[0], out var offset);
inputParams.RemoveAt(0); inputParams.RemoveAt(0);
@ -693,13 +742,43 @@ namespace telegram {
var replyMessage = history[offset - 1]; var replyMessage = history[offset - 1];
SendMessage(message, currentChatId, replyMessage.Id);
}
catch {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Unknown error sending message.");
}
}
}
public class ReplyDirectCommand : Command {
public ReplyDirectCommand() : base("rd", "", "replies to message by id", "<id> <message>", -1) { }
public override void Handler(List<string> inputParams) {
try {
if (inputParams.Count < 2) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid syntax, check /help for more information.");
return;
}
if (currentChatId == 0) { if (currentChatId == 0) {
lock (@lock) lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue."); messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
return; return;
} }
SendMessage(message, currentChatId, replyMessage.Id); var parsed = long.TryParse(inputParams[0], out var replyId);
inputParams.RemoveAt(0);
var message = inputParams.Aggregate((current, param) => current + " " + param).Trim();
if (!parsed || string.IsNullOrWhiteSpace(message)) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}Invalid syntax, check /help for more information.");
return;
}
SendMessage(message, currentChatId, replyId);
} }
catch { catch {
lock (@lock) lock (@lock)

View file

@ -13,11 +13,9 @@ using static telegram.CommandManager;
namespace telegram { namespace telegram {
/* /*
* TODO: * TODO:
* fuzzy matching for replies?
* unreads are unreliable in secret chats! * unreads are unreliable in secret chats!
* mute,unmute chats * mute,unmute chats
* photo & document download & show externally * photo & document download & show externally
* publish AUR package
* cursor input nav (up/down history, (alt +) left/right) * cursor input nav (up/down history, (alt +) left/right)
* ref: http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html#cursor-navigation * ref: http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html#cursor-navigation
* ref: https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences * ref: https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences