Files
NemesisAI/TelegramBot/Program.cs
2024-12-26 19:51:17 +01:00

131 lines
4.3 KiB
C#

using OpenAI.Chat;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;using TelegramBot;
using File = System.IO.File;
string baseUrl = Env.Get("OPENAI_BASE_URL");
string apiKey = Env.Get("OPENAI_API_KEY");
string model = Env.Get("OPENAI_MODEL");
var oaiAgent = new OpenAiAgent(baseUrl, apiKey, model);
Console.WriteLine("Starting the bot...");
string nemesisPrompt = File.ReadAllText($"prompt/{Env.Get("NEMESIS_PROMPT_FILE")}");
string krolikPrompt = File.ReadAllText($"prompt/{Env.Get("KROLIK_PROMPT_FILE")}");
//Ratelimit
TimeSpan rateLimit = new(0, 0, 0, 10);
Dictionary<long, DateTime> lastMessage = new();
HashSet<long> unlimitedChats = new();
bool IsRateLimited(long chatId) {
if (lastMessage.ContainsKey(chatId) && DateTime.Now - lastMessage[chatId] < rateLimit) return true;
lastMessage[chatId] = DateTime.Now;
return false;
}
#region TelegramBot Startup
string nemesisToken = Env.Get("NEMESIS_BOT_TOKEN");
string krolikToken = Env.Get("KROLIK_BOT_TOKEN");
using var nemcts = new CancellationTokenSource();
using var krocts = new CancellationTokenSource();
var nemesisBot = new TelegramBotClient(nemesisToken, cancellationToken:nemcts.Token);
var krolikBot = new TelegramBotClient(krolikToken, cancellationToken:krocts.Token);
var nemProfile = nemesisBot.GetMe();
Agent Nemesis = new(
Actor.Nemesis,
nemProfile.Result.Id,
nemProfile.Result.FirstName,
nemProfile.Result.Username!,
nemesisBot
);
var kroProfile = krolikBot.GetMe();
Agent Krolik = new(
Actor.Krolik,
kroProfile.Result.Id,
kroProfile.Result.FirstName,
kroProfile.Result.Username!,
krolikBot
);
nemesisBot.OnMessage += OnNemMessage;
krolikBot.OnMessage += OnKroMessage;
await nemesisBot.DropPendingUpdates();
Console.WriteLine("Nemesis Bot running");
await krolikBot.DropPendingUpdates();
Console.WriteLine("Krolik Bot running");
Thread.Sleep(Timeout.Infinite);
nemcts.Cancel(); // stop nembot
krocts.Cancel(); // stop krobot
#endregion
async Task OnNemMessage(Message msg, UpdateType type) {
//Discard any message that is not a text message
if (msg.Type != MessageType.Text) return;
await OnMessage(msg, Nemesis);
}
async Task OnKroMessage(Message msg, UpdateType type) {
//Discard any message that is not a text message
if (msg.Type != MessageType.Text) return;
await OnMessage(msg, Krolik);
}
//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;
}
// Otherwise process it normally
if (msg.Text!.Contains(agent.Name, StringComparison.OrdinalIgnoreCase) ||
msg.ReplyToMessage?.From?.Id == agent.TelegramId || msg.Chat.Type == ChatType.Private) {
//Check if the chat (group) is rate limited
if (IsRateLimited(chatid)) {
Console.WriteLine("No response due to ratelimit.");
return;
}
await AnswerChat(chatid, msg.Text, tokenlenght, agent.Actor);
}
}
async Task AnswerChat(long chatId, string input, int tokenLenght, Actor actor) {
//Limit the message to 1024 characters to avoid out of context jump
string text = input;
if (input.Length > 1024) text = input.Substring(0, 1024);
//Get the response from the OpenAI API
var result = oaiAgent.GetChatResponse(chatId, actor);
//Send the response to the user
await krolikBot.SendMessage(chatId, result);
}