diff --git a/TelegramBot/Program.cs b/TelegramBot/Program.cs index 1c4c312..3fb3f28 100644 --- a/TelegramBot/Program.cs +++ b/TelegramBot/Program.cs @@ -1,6 +1,7 @@ using OpenAI; using OpenAI.Chat; using System.ClientModel; +using System.Collections; using Telegram.Bot; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; @@ -23,6 +24,21 @@ string nemesisPrompt = File.ReadAllText($"prompt/{GetEnv("NEMESIS_PROMPT_FILE")} Dictionary> oaiChats = new(); +//Ratelimit +TimeSpan rateLimit = new(0, 0, 0, 30); +Dictionary lastMessage = new(); + +bool IsRateLimited(long chatId) { + if (lastMessage.ContainsKey(chatId)) { + if (DateTime.Now - lastMessage[chatId] < rateLimit) { + return true; + } + } + lastMessage[chatId] = DateTime.Now; + return false; +} + + var options = new OpenAIClientOptions() { Endpoint = new(baseUrl), NetworkTimeout = new TimeSpan(0, 0, 30) @@ -52,12 +68,18 @@ async Task OnMessage(Message msg, UpdateType type) if (msg.Text!.Contains(me.Result.FirstName!, StringComparison.OrdinalIgnoreCase) || msg.ReplyToMessage != null && msg.ReplyToMessage.From!.Id == me.Result.Id || msg.Chat.Type == ChatType.Private) { + var chatid = msg.Chat.Id; Console.WriteLine( $""" - Received message from {msg.Chat.Id} Type: {type} + Received message from {chatid} Type: {type} Message: {msg.Text} """); - var chatid = msg.Chat.Id; + //Check if the chat (group) is rate limited + if (IsRateLimited(chatid)) { + Console.WriteLine("No response due to ratelimit."); + return; + } + //Check if the message is a reset command if (msg.Text.StartsWith("/reset")) { ResetChat(chatid); @@ -106,10 +128,7 @@ void AddChatToDictionary(long id) { void ChatMessageRotate(long chatId, ChatMessage message){ //Remove the first message from the chat if the chat has more than 5 couples of messages (0 is our prompt) - if (oaiChats[chatId].Count > 10) { - oaiChats[chatId].RemoveAt(1); - oaiChats[chatId].RemoveAt(2); - } + if (oaiChats[chatId].Count > 10) oaiChats[chatId].RemoveRange(1, 2); //Add the new message to the chat oaiChats[chatId].Add(message);