Fixed code, enabled to also always answer in a private chat
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
TELEGRAM_BOT_TOKEN=yourTokenHere
|
||||
OPENAI_BASE_URL=llm-server
|
||||
OPENAI_MODEL=Qwen2.5-7B-Instruct-Q8_0
|
||||
OPENAI_API_KEY=
|
||||
OPENAI_BASE_URL=http://llm-server/
|
||||
OPENAI_MODEL=Qwen2.5-7B-Instruct-Q8_0.gguf
|
||||
OPENAI_API_KEY=MyApiKey
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using OpenAI;
|
||||
using OpenAI.Chat;
|
||||
using System.ClientModel;
|
||||
using System.Threading.Channels;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
@@ -8,6 +9,13 @@ using Telegram.Bot.Types.Enums;
|
||||
string baseUrl = Environment.GetEnvironmentVariable("OPENAI_BASE_URL") ?? "https://api.openai.com";
|
||||
string model = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? string.Empty;
|
||||
string apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? string.Empty;
|
||||
Console.WriteLine("Starting the bot...");
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
Base URL: {baseUrl}
|
||||
Model: {model}
|
||||
API Key: {apiKey}
|
||||
""");
|
||||
|
||||
string nemesisPrompt =
|
||||
"""
|
||||
@@ -91,20 +99,29 @@ var openAiClient = new OpenAIClient(openAiApiKey, options);
|
||||
var chatClient = openAiClient.GetChatClient(model);
|
||||
|
||||
string token = Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN") ?? string.Empty;
|
||||
Console.WriteLine("OpenAI Chat Client created");
|
||||
|
||||
using var cts = new CancellationTokenSource();
|
||||
var bot = new TelegramBotClient(token, cancellationToken:cts.Token);
|
||||
var me = bot.GetMe();
|
||||
bot.OnMessage += OnMessage;
|
||||
Console.ReadLine();
|
||||
cts.Cancel();
|
||||
Console.WriteLine("Bot running");
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
cts.Cancel(); // stop the bot
|
||||
|
||||
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 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 message contains the bot's username or a reply to a message sent by the bot or a private chat
|
||||
if (msg.Text!.Contains(me.Result.FirstName!, StringComparison.OrdinalIgnoreCase) ||
|
||||
msg.ReplyToMessage != null && msg.ReplyToMessage.From!.Id == me.Result.Id ||
|
||||
msg.Chat.Type == ChatType.Private) {
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
Received message from {msg.Chat.Id} Type: {type}
|
||||
Message: {msg.Text}
|
||||
""");
|
||||
var chatid = msg.Chat.Id;
|
||||
//Check if the chat is already in the dictionary
|
||||
if (!oaiChats.ContainsKey(chatid))
|
||||
@@ -114,7 +131,7 @@ async Task OnMessage(Message msg, UpdateType type)
|
||||
//fetch existing messages history
|
||||
var messages = oaiChats[chatid];
|
||||
//Fetch the response from the model
|
||||
var result = chatClient.CompleteChat(messages).Value.Content.ToString()!;
|
||||
var result = chatClient.CompleteChat(messages).Value.Content[0].Text;
|
||||
//Add the response to the chat
|
||||
oaiChats[chatid].Add(new AssistantChatMessage(result));
|
||||
//Send the response to the user
|
||||
|
||||
Reference in New Issue
Block a user