public void When_response_throws_handle_and_try_next() { var responseMock1 = Substitute.For <IBotResponse>(); responseMock1.CanRespond(Arg.Any <ResponseContext>()).Returns(true); responseMock1.GetResponse(Arg.Any <ResponseContext>(), Arg.Any <RocketChatBot>()) .Throws(new Exception()); var responseMock2 = Substitute.For <IBotResponse>(); responseMock2.CanRespond(Arg.Any <ResponseContext>()).Returns(true); responseMock2.GetResponse(Arg.Any <ResponseContext>(), Arg.Any <RocketChatBot>()); var bot = new RocketChatBot(_driverMock, _loggerMock); bot.AddResponse(responseMock1); bot.AddResponse(responseMock2); var rocketMessage = AutoFixture.Create <RocketMessage>(); // Act _driverMock.MessageReceived += Raise.Event <MessageReceived>(rocketMessage); Thread.Sleep(200); // Assert responseMock1.Received().GetResponse(Arg.Any <ResponseContext>(), Arg.Any <RocketChatBot>()); responseMock2.Received().GetResponse(Arg.Any <ResponseContext>(), Arg.Any <RocketChatBot>()); }
private static async Task MainAsync() { const string username = "******"; const string password = "******"; const string rocketServerUrl = "dev0:3000"; // just the host and port const bool useSsl = false; // Basically use ws or wss. // Create the bot - an abstraction of the driver RocketChatBot bot = new RocketChatBot(rocketServerUrl, useSsl); // Connect to Rocket.Chat await bot.ConnectAsync(); // Login ILoginOption loginOption = new EmailLoginOption { Email = username, Password = password }; await bot.LoginAsync(loginOption); // Start listening for messages await bot.SubscribeAsync(); // Add possible responses to be checked in order // This is not thead safe, FYI IBotResponse giphyResponse = new GiphyResponse(); bot.AddResponse(giphyResponse); // And that's it // Checkout GiphyResponse in the example project for more info. }
static void Main(string[] args) { ILoginOption loginOption = new LdapLoginOption { Username = "******", Password = "******" }; // SetUp Bot bot = new RocketChatBot("192.168.99.100:5888", false, new Microsoft.Extensions.Logging.Console.ConsoleLogger("Logger", (a, b) => true, true)); bot.AddResponse(new TestResponse()); StartBot(loginOption); Console.ReadLine(); }
public void When_response_returns_false_dont_run() { var rocketMessage = AutoFixture.Create <RocketMessage>(); var responseMock = Substitute.For <IBotResponse>(); responseMock.CanRespond(Arg.Any <ResponseContext>()).Returns(false); var bot = new RocketChatBot(_driverMock, _loggerMock); bot.AddResponse(responseMock); // Act _driverMock.MessageReceived += Raise.Event <MessageReceived>(rocketMessage); Thread.Sleep(1 * 1000); // Assert responseMock.DidNotReceive().GetResponse(Arg.Any <ResponseContext>(), Arg.Any <RocketChatBot>()); }
private static async Task MainAsync() { const string username = "******"; const string password = "******"; const string rocketServerUrl = "dev0:3000"; // just the host and port const bool useSsl = false; // Basically use ws or wss. // Basic logger ILogger logger = new ConsoleLogger(); // Create the rocket driver - will connect the the server using websockets _driver = new RocketChatDriver(rocketServerUrl, useSsl, logger); // Request connection to Rocket.Chat await _driver.ConnectAsync(); // Login with a email address (opposed to logging in with LDAP or Username) await _driver.LoginWithEmailAsync(username, password); // Most rooms have a GUID - GENERAL is always called GENERAL string roomId = await _driver.GetRoomIdAsync("GENERAL"); // Join the room if not already joined await _driver.JoinRoomAsync(roomId); // Start listening for messages // Don't specify a roomId if you want to listen on all channels await _driver.SubscribeToRoomAsync(roomId); // Create the bot - an abstraction of the driver RocketChatBot bot = new RocketChatBot(_driver, logger); // Add possible responses to be checked in order // This is not thead safe, FYI IBotResponse giphyResponse = new GiphyResponse(); bot.AddResponse(giphyResponse); // And that's it // Checkout GiphyResponse in the example project for more info. }
public void On_message_receive_process_messages() { var rocketMessage = AutoFixture.Create <RocketMessage>(); var waitHandle = new AutoResetEvent(false); var responseMock = Substitute.For <IBotResponse>(); responseMock.CanRespond(Arg.Any <ResponseContext>()).Returns(true); responseMock.GetResponse(Arg.Do <ResponseContext>(message => waitHandle.Set()), Arg.Any <RocketChatBot>()); var bot = new RocketChatBot(_driverMock, _loggerMock); bot.AddResponse(responseMock); // Act _driverMock.MessageReceived += Raise.Event <MessageReceived>(rocketMessage); waitHandle.WaitOne(TimeSpan.FromSeconds(5)); // Assert responseMock.Received().GetResponse(Arg.Any <ResponseContext>(), bot); }
public void When_response_returns_unsupported_response_throw() { var rocketMessage = AutoFixture.Create <RocketMessage>(); var basicResponses = AutoFixture.Build <MockMessageResponse>().CreateMany().ToList(); var responseMock = Substitute.For <IBotResponse>(); responseMock.CanRespond(Arg.Any <ResponseContext>()).Returns(true); responseMock.GetResponse(Arg.Any <ResponseContext>(), Arg.Any <RocketChatBot>()).Returns(basicResponses); var bot = new RocketChatBot(_driverMock, _loggerMock); bot.AddResponse(responseMock); // Act _driverMock.MessageReceived += Raise.Event <MessageReceived>(rocketMessage); Thread.Sleep(100); // Assert _driverMock.DidNotReceive().SendMessageAsync(Arg.Any <string>(), Arg.Any <string>()); _driverMock.DidNotReceive().SendCustomMessageAsync(Arg.Any <Attachment>(), Arg.Any <string>()); }
public void When_response_returns_attachment_send_attachment() { var rocketMessage = AutoFixture.Create <RocketMessage>(); var basicResponses = AutoFixture.Build <AttachmentResponse>().CreateMany().ToList(); var responseMock = Substitute.For <IBotResponse>(); responseMock.CanRespond(Arg.Any <ResponseContext>()).Returns(true); responseMock.GetResponse(Arg.Any <ResponseContext>(), Arg.Any <RocketChatBot>()).Returns(basicResponses); var bot = new RocketChatBot(_driverMock, _loggerMock); bot.AddResponse(responseMock); // Act _driverMock.MessageReceived += Raise.Event <MessageReceived>(rocketMessage); Thread.Sleep(200); // Assert _driverMock.Received(basicResponses.Count) .SendCustomMessageAsync(Arg.Is <Attachment>(s => basicResponses.Any(x => x.Attachment.Equals(s))), Arg.Any <string>()); }