adds new rabbit SendMessageToQ command

This commit is contained in:
Madpeter 2025-07-09 20:19:27 +01:00
parent 10dc20e350
commit 8f95809c80
2 changed files with 98 additions and 0 deletions

View file

@ -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<bool,string> 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);
}
}
}
}

View file

@ -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<bool,string> 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<bool, string>(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<bool, string>(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<bool, string>(false, "Channel is still not open");
}
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublishAsync(exchange: "", routingKey: Qname, body: body);
return new KeyValuePair<bool, string>(true, "Message sent to queue " + Qname);
}
protected void connectService()
{
if (connection != null && connection.IsOpen == true)