Converted send message method to async (also created a synchronous method that shadows the async method). Added a method to send multiple messages at once and message truncation with a choice of strategies

This commit is contained in:
barelyprofessional
2024-09-17 00:57:11 +08:00
parent 4753783c41
commit 5b3f394c29
14 changed files with 152 additions and 49 deletions

View File

@@ -24,7 +24,7 @@ public class EditTestCommand : ICommand
var iterations = 3;
var i = 0;
var delay = 1000;
var reference = botInstance.SendChatMessage($"{msg} {i}", true);
var reference = await botInstance.SendChatMessageAsync($"{msg} {i}", true);
while (reference.Status == SentMessageTrackerStatus.WaitingForResponse)
{
await Task.Delay(100, ctx);
@@ -83,4 +83,40 @@ public class ExceptionTestCommand : ICommand
{
throw new Exception("Caused by the test exception command");
}
}
public class LengthLimitTestCommand : ICommand
{
public List<Regex> Patterns => [
new Regex("^test lengthlimit$")
];
public string? HelpText => null;
public UserRight RequiredRight => UserRight.Admin;
// Increased timeout as it has to wait for Sneedchat to echo the message and that can be slow sometimes
public TimeSpan Timeout => TimeSpan.FromSeconds(15);
public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments, CancellationToken ctx)
{
var logger = LogManager.GetCurrentClassLogger();
var niceTruncation = await botInstance.SendChatMessageAsync("The quick brown fox jumps over the lazy dog.",
true, ChatBot.LengthLimitBehavior.TruncateNicely, 20);
var exactTruncation = await botInstance.SendChatMessageAsync("The quick brown fox jumps over the lazy dog.",
true, ChatBot.LengthLimitBehavior.TruncateExactly, 20);
// Would normally get eaten but because we artificially lowered the length limit, it should get sent as-is
var doNothing = await botInstance.SendChatMessageAsync("The quick brown fox jumps over the lazy dog.",
true, ChatBot.LengthLimitBehavior.DoNothing, 20);
var refuseToSend = await botInstance.SendChatMessageAsync("The quick brown fox jumps over the lazy dog.",
true, ChatBot.LengthLimitBehavior.RefuseToSend, 20);
await Task.Delay(TimeSpan.FromSeconds(5), ctx);
logger.Info($"niceTruncation => {niceTruncation.Status}; exactTruncation => {exactTruncation.Status}; doNothing => {doNothing.Status}; refuseToSend => {refuseToSend.Status}");
if (niceTruncation.ChatMessageId != null)
botInstance.KfClient.DeleteMessage(niceTruncation.ChatMessageId.Value);
if (exactTruncation.ChatMessageId != null)
botInstance.KfClient.DeleteMessage(exactTruncation.ChatMessageId.Value);
if (doNothing.ChatMessageId != null)
botInstance.KfClient.DeleteMessage(doNothing.ChatMessageId.Value);
// Should never happen
if (refuseToSend.ChatMessageId != null)
botInstance.KfClient.DeleteMessage(refuseToSend.ChatMessageId.Value);
}
}