diff --git a/.idea/.idea.NemesisAI/Docker/compose.generated.override.yml b/.idea/.idea.NemesisAI/Docker/compose.generated.override.yml index 9450ecb..2c709cc 100644 --- a/.idea/.idea.NemesisAI/Docker/compose.generated.override.yml +++ b/.idea/.idea.NemesisAI/Docker/compose.generated.override.yml @@ -1,37 +1,20 @@ # This is a generated file. Not intended for manual editing. services: - telegrambot: + nemesisBot: build: - context: "/home/redcode/dev/NemesisAI" + context: "I:\\NemesisAI" dockerfile: "TelegramBot/Dockerfile" - target: "base" - args: - BUILD_CONFIGURATION: "Debug" + target: "build" command: [] entrypoint: - - "/opt/JetBrains/RiderDebuggerTools/linux-x64/JetBrains.Debugger.Worker" - - "--runtimeconfig" - - "/opt/JetBrains/RiderDebuggerTools/JetBrains.Debugger.Worker.runtimeconfig.json" - - "--mode=server" - - "--frontend-port=57000" - - "--backend-port=57200" - - "--roslyn-worker-port=57400" - - "--timeout=60" + - "dotnet" + - "/app/bin/Debug/net9.0/TelegramBot.dll" environment: DOTNET_ENVIRONMENT: "Development" - DOTNET_USE_POLLING_FILE_WATCHER: "true" - RIDER_DEBUGGER_LOG_DIR: "/var/opt/JetBrains/RiderDebuggerTools" - RESHARPER_LOG_CONF: "/etc/opt/JetBrains/RiderDebuggerTools/backend-log.xml" image: "telegrambot:dev" - ports: - - "127.0.0.1:57000:57000" - - "127.0.0.1:57200:57200" - - "127.0.0.1:57400:57400" + ports: [] volumes: - - "/home/redcode/dev/NemesisAI/TelegramBot:/app:rw" - - "/home/redcode/dev/NemesisAI:/src:rw" - - "/home/redcode/.nuget/packages:/home/app/.nuget/packages" - - "/home/redcode/.local/share/JetBrains/RiderRemoteDebugger/2024.3.2/Linux64:/opt/JetBrains/RiderDebuggerTools" - - "/home/redcode/.local/share/JetBrains/Toolbox/apps/rider/bin/backend-log.xml:/etc/opt/JetBrains/RiderDebuggerTools/backend-log.xml" - - "/home/redcode/.cache/JetBrains/Rider2024.3/log/DebuggerWorker/JetBrains.Debugger.Worker.2024_12_24_23_05_14:/var/opt/JetBrains/RiderDebuggerTools:rw" + - "I:\\NemesisAI\\TelegramBot:/app:rw" + - "I:\\NemesisAI:/src:rw" + - "C:\\Users\\airon\\.nuget\\packages:/root/.nuget/packages" working_dir: "/app" diff --git a/TelegramBot/.env b/TelegramBot/.env index e13bb4b..5c9dcf2 100644 --- a/TelegramBot/.env +++ b/TelegramBot/.env @@ -1 +1,4 @@ -TELEGRAM_BOT_TOKEN=yourTokenHere \ No newline at end of file +TELEGRAM_BOT_TOKEN=yourTokenHere +LMSTUDIO_BASE_URL= +LMSTUDIO_MODEL= +LMSTUDIO_API_KEY= diff --git a/TelegramBot/Dockerfile b/TelegramBot/Dockerfile index 3b281c0..74491bb 100644 --- a/TelegramBot/Dockerfile +++ b/TelegramBot/Dockerfile @@ -1,8 +1,8 @@ -FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base +FROM mcr.microsoft.com/dotnet/dotnet:9.0 AS base USER $APP_UID WORKDIR /app -FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["TelegramBot/TelegramBot.csproj", "TelegramBot/"] diff --git a/TelegramBot/Program.cs b/TelegramBot/Program.cs index 27732ed..9a0dc86 100644 --- a/TelegramBot/Program.cs +++ b/TelegramBot/Program.cs @@ -1,9 +1,19 @@ -using Telegram.Bot; +using System.Diagnostics; +using Telegram.Bot; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; +using LMStudio; + +string baseUrl = Environment.GetEnvironmentVariable("LMSTUDIO_BASE_URL") ?? string.Empty; +string model = Environment.GetEnvironmentVariable("LMSTUDIO_MODEL") ?? string.Empty; +string apiKey = Environment.GetEnvironmentVariable("LMSTUDIO_API_KEY") ?? string.Empty; +var lmsclient = new ModelConnection(baseUrl, model, apiKey); +Dictionary lmsChats = new(); + +string token = Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN") ?? string.Empty; using var cts = new CancellationTokenSource(); -var bot = new TelegramBotClient(Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN"), cancellationToken:cts.Token); +var bot = new TelegramBotClient(token, cancellationToken:cts.Token); var me = bot.GetMe(); bot.OnMessage += OnMessage; Console.ReadLine(); @@ -13,19 +23,20 @@ async Task OnMessage(Message msg, UpdateType type) { //Discard any message that is not a text message if (msg.Type != MessageType.Text) return; - //Check if the message contains the bot's username - if (msg.Text!.Contains(me.Result.Username!, StringComparison.OrdinalIgnoreCase)) - await bot.SendMessage(msg.Chat, "You mentioned me!"); - //Check if the message is a reply to a message sent by the bot - else if(msg.ReplyToMessage != null && msg.ReplyToMessage.From!.Id == me.Result.Id){ - await bot.SendMessage(msg.Chat, "You replied to a message!"); + //Check if the message contains the bot's username or a reply to a message sent by the bot + if (msg.Text!.Contains(me.Result.Username!, StringComparison.OrdinalIgnoreCase) || + msg.ReplyToMessage != null && msg.ReplyToMessage.From!.Id == me.Result.Id) { + //Check if the chat is already in the dictionary + if (!lmsChats.ContainsKey(msg.Chat.Id)) + AddChatToDictionary(msg.Chat.Id); + + lmsChats[msg.Chat.Id].CreateChatCompletion(msg.Text); } - //We can safely return otherwise } -HttpClient AiClient = new HttpClient(); - -string getNemesisMsg() { - //AiClient.GetAsync() - return ""; +void AddChatToDictionary(long id) { + //Create a new chat object + var chat = new LMStudio.Chat(lmsclient); + //add the entry to the dictionary + lmsChats.Add(id, chat); } \ No newline at end of file diff --git a/TelegramBot/TelegramBot.csproj b/TelegramBot/TelegramBot.csproj index c7be57f..081076f 100644 --- a/TelegramBot/TelegramBot.csproj +++ b/TelegramBot/TelegramBot.csproj @@ -2,10 +2,11 @@ Exe - net8.0 + net9.0 enable enable Linux + latestmajor @@ -15,6 +16,7 @@ + diff --git a/compose.yaml b/compose.yaml index 7e86570..bcad2c1 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,5 +1,5 @@ services: - telegrambot: + nemesisBot: image: telegrambot build: context: . diff --git a/global.json b/global.json new file mode 100644 index 0000000..f4fd385 --- /dev/null +++ b/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "9.0.0", + "rollForward": "latestMajor", + "allowPrerelease": true + } +} \ No newline at end of file