using System; using robotManager.Helpful; using wManager.Wow.Helpers; using wManager.Wow.Enums; using wManager.Plugin; using robotManager.Products; using System.Net.Http; using System.Text; using Newtonsoft.Json; using System.Threading.Tasks; using System.Threading; public class Main : IPlugin { private bool _isLaunched; private readonly string _apiKey = "YOUR_OPENAI_API_KEY_HERE"; // Replace with your API key private readonly string _apiUrl = "https://api.openai.com/v1/chat/completions"; public void Initialize() { _isLaunched = true; Logging.Write("[WhisperResponder] Started."); var chat = new Channel(); while (_isLaunched && Products.IsStarted) { try { while (chat.ActuelRead != Channel.GetMsgActuelInWow && Products.IsStarted) { var msg = chat.ReadMsg(); if (msg.Channel == ChatTypeId.WHISPER) { Logging.Write("[WhisperResponder] Whisper from " + msg.UserName + ": " + msg.Msg); // Get response from ChatGPT string response = SendToOpenAIAsync(msg.Msg).Result; // Respond to the whisper Chat.SendChatMessageWhisper(response, msg.UserName); } } } catch (Exception e) { Logging.WriteError("[WhisperResponder]: " + e); } Thread.Sleep(150); } } public void Dispose() { _isLaunched = false; Logging.Write("[WhisperResponder] Stopped."); } public void Settings() { // No settings for this example } private async Task SendToOpenAIAsync(string userMessage) { try { using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _apiKey); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); var payload = new { model = "gpt-3.5-turbo", messages = new[] { new { role = "system", content = "You are an avid and helpful world of warcraft player" }, new { role = "user", content = userMessage } } }; var response = await client.PostAsync(_apiUrl, new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json")); var responseBody = await response.Content.ReadAsStringAsync(); // Extract the assistant's message from the response var jsonResponse = JsonConvert.DeserializeObject(responseBody); return jsonResponse.choices[0].message.content; } } catch (Exception ex) { Logging.WriteError("WhisperResponder SendToOpenAIAsync error: " + ex.Message); return "Sorry, I couldn't process your message."; } } }