diff --git a/TelegramBot/Program.cs b/TelegramBot/Program.cs index cc9fbab..43640f5 100644 --- a/TelegramBot/Program.cs +++ b/TelegramBot/Program.cs @@ -92,25 +92,35 @@ async Task OnKroMessage(Message msg, UpdateType type) { //TODO: currently we only take in account private messages and messages directed to the bot/mentioning them. // We should also take in account the last x messages in groups to add more context async Task OnMessage(Message msg, Agent agent) { - //Check if the message contains the bot's username or a reply to a message sent by the bot or a private chat - var chatid = msg.Chat.Id; - var tokenlenght = oaiAgent.GetTokenLenght(msg.Text); - Console.WriteLine( - $""" - {agent.Name} has received message from {chatid} TokenLenght: {tokenlenght} - Message: {msg.Text} - """); - //Add the message to the chat history - oaiAgent.ChatHistoryAppend(Actor.User, chatid, "User: " + msg.Text); - + //Check if the message is a reset command if (msg.Text == "/reset" || msg.Text == "/reset@" + agent.Username) { oaiAgent.ResetChat(chatid); await agent.Bot.SendMessage(chatid, "Chat context has been reset"); return; } + + if (msg.Text == "/history @"+agent.Username) { + + var history = oaiAgent.GetChatHistory(chatid, agent.Actor); + var historyText = string.Join("\n", history.Select(x => x.Item1.Content[0].Text)); + await agent.Bot.SendMessage(chatid, historyText); + return; + } + var text = $"{msg.From?.FirstName} {msg.From?.LastName}: {msg.Text}"; + var tokenlenght = oaiAgent.GetTokenLenght(msg.Text!); + Console.WriteLine( + $""" + {agent.Name} has received message from {chatid} TokenLenght: {tokenlenght} + Message: {msg.Text} + """); + //Add the message to the chat history + oaiAgent.ChatHistoryAppend(Actor.User, chatid, text); + + + //Check if the message contains the bot's username or a reply to a message sent by the bot or a private chat // Otherwise process it normally if (msg.Text!.Contains(agent.Name, StringComparison.OrdinalIgnoreCase) || msg.ReplyToMessage?.From?.Id == agent.TelegramId || @@ -125,7 +135,6 @@ async Task OnMessage(Message msg, Agent agent) { } } - async Task AnswerChat(long chatId, Agent agent) { //Get the response from the OpenAI API var result = oaiAgent.GetChatResponse(chatId, agent);