diff --git a/SecondBotEvents/Commands/RabbitMQ.cs b/SecondBotEvents/Commands/RabbitMQ.cs new file mode 100644 index 0000000..9782e3a --- /dev/null +++ b/SecondBotEvents/Commands/RabbitMQ.cs @@ -0,0 +1,58 @@ +using Newtonsoft.Json; +using OpenMetaverse; +using SecondBotEvents.Services; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace SecondBotEvents.Commands +{ + [ClassInfo("Talk back to the rabbit MQ")] + public class RabbitMQ(EventsSecondBot setmaster) : CommandsAPI(setmaster) + { + [About("Fetchs the current region type the bot is in")] + [ArgHints("Qname", "Name of the queue to send the message to")] + [ArgHints("message", "Message to send to the queue")] + [ReturnHints("Message sent to the queue")] + [ReturnHintsFailure("Error sending message: ...")] + [ReturnHintsFailure("RabbitMQ service not available")] + [ReturnHintsFailure("RabbitMQ service not running")] + [ReturnHintsFailure("Qname is empty")] + [ReturnHintsFailure("Message is empty")] + [CmdTypeDo()] + public object SendMessageToQ(string Qname,string message) + { + if(master.RabbitService == null) + { + return Failure("RabbitMQ service not available"); + } + if(master.RabbitService.isRunning() == false) + { + return Failure("RabbitMQ service not running"); + } + if((Qname == null) || (Qname.Length < 1)) + { + return Failure("Qname is empty"); + } + if ((message == null) || (message.Length < 1)) + { + return Failure("Message is empty"); + } + try + { + KeyValuePair reply = master.RabbitService.SendMessage(Qname, message); + if(reply.Key == false) + { + return Failure("Error sending message: " + reply.Value); + } + return BasicReply("Message sent successfully to " + Qname); + } + catch (Exception ex) + { + return Failure("Error sending message: " + ex.Message); + } + } + + + } +} diff --git a/SecondBotEvents/Services/RabbitService.cs b/SecondBotEvents/Services/RabbitService.cs index 1913019..9bc21ec 100644 --- a/SecondBotEvents/Services/RabbitService.cs +++ b/SecondBotEvents/Services/RabbitService.cs @@ -9,6 +9,7 @@ using RabbitMQ.Client.Events; using Newtonsoft.Json; using Swan; using OpenMetaverse; +using Microsoft.Extensions.Logging.Abstractions; namespace SecondBotEvents.Services { @@ -169,6 +170,45 @@ namespace SecondBotEvents.Services master.BotClientNoticeEvent += BotClientRestart; } + public KeyValuePair SendMessage(string Qname,string message) + { + if (connection == null || connection.IsOpen == false) + { + LogFormater.Warn("Rabbit service [Connection is not open] cannot send message to queue " + Qname); + return new KeyValuePair(false, "Connection is not open"); + } + if (string.IsNullOrEmpty(Qname) || string.IsNullOrEmpty(message)) + { + LogFormater.Warn("Rabbit service [Queue name or message is empty] cannot send message to queue " + Qname); + return new KeyValuePair(false, "Queue name or message is empty"); + } + IChannel channel = null; + if (channels.ContainsKey(Qname) == false) + { + channel = connection.CreateChannelAsync().Await(); + channel.QueueDeclareAsync(queue: Qname, durable: true, exclusive: false, autoDelete: false, + arguments: null); + channels.Add(Qname, channel); + } + channel = channels[Qname]; + if (channel == null || channel.IsOpen == false) + { + LogFormater.Warn("Rabbit service [Channel is not open] " + Qname + " reconnecting"); + channel = connection.CreateChannelAsync().Await(); + channel.QueueDeclareAsync(queue: Qname, durable: true, exclusive: false, autoDelete: false, + arguments: null); + channels[Qname] = channel; + } + if (channel == null || channel.IsOpen == false) + { + LogFormater.Warn("Rabbit service [Channel is still not open] " + Qname + " cannot send message"); + return new KeyValuePair(false, "Channel is still not open"); + } + var body = Encoding.UTF8.GetBytes(message); + channel.BasicPublishAsync(exchange: "", routingKey: Qname, body: body); + return new KeyValuePair(true, "Message sent to queue " + Qname); + } + protected void connectService() { if (connection != null && connection.IsOpen == true)