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. }
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>()); }
public async Task On_disconnect_and_if_logged_in_resume_old_session() { var loginOption = Substitute.For <ILoginOption>(); var loginResult = AutoFixture.Build <MethodResult <LoginResult> >() .With(x => x.Error, null) .Create(); _driverMock.LoginAsync(loginOption).Returns(Task.FromResult(loginResult)); var resumeResult = AutoFixture.Build <MethodResult <LoginResult> >() .With(x => x.Error, null) .Create(); _driverMock.LoginResumeAsync(loginResult.Result.Token) .Returns(Task.FromResult(resumeResult)); var bot = new RocketChatBot(_driverMock, _loggerMock); await bot.LoginAsync(loginOption); // Act _driverMock.DdpReconnect += Raise.Event <DdpReconnect>(); Thread.Sleep(200); // Assert await _driverMock.Received().LoginResumeAsync(Arg.Any <string>()); }
public async Task On_unsuccessful_resume_throw() { var loginOption = Substitute.For <ILoginOption>(); var loginResult = AutoFixture.Build <MethodResult <LoginResult> >() .With(x => x.Error, null) .Create(); _driverMock.LoginAsync(loginOption).Returns(Task.FromResult(loginResult)); var resumeResult = AutoFixture.Build <MethodResult <LoginResult> >() .Create(); _driverMock.LoginResumeAsync(loginResult.Result.Token) .Returns(Task.FromResult(resumeResult)); var bot = new RocketChatBot(_driverMock, _loggerMock); // Act await bot.LoginAsync(loginOption); Action resumeAction = () => bot.ResumeAsync().Wait(); // Assert resumeAction.ShouldThrow <Exception>(); }
public void When_constructed_set_driver() { // Act var bot = new RocketChatBot(_driverMock, _loggerMock); // Assert bot.Driver.Should().Be(_driverMock); }
public void On_resume_throw_if_not_logged_in() { var bot = new RocketChatBot(_driverMock, _loggerMock); // Act Action resumeAction = () => bot.ResumeAsync().Wait(); // Assert resumeAction.ShouldThrow <InvalidOperationException>(); }
public async Task On_subscribe_subscribe_to_all_rooms() { var bot = new RocketChatBot(_driverMock, _loggerMock); // Act await bot.SubscribeAsync(); // Assert await _driverMock.Received().SubscribeToRoomAsync(); }
public async Task On_connect_driver_should_connect() { var bot = new RocketChatBot(_driverMock, _loggerMock); // Act await bot.ConnectAsync(); // Assert await _driverMock.Received().ConnectAsync(); }
public IEnumerable<IMessageResponse> GetResponse(ResponseContext context, RocketChatBot caller) { var message = context.Message; var search = message.Message.Replace(GiphyCommand, "").Trim(); var url = GetGiphy(search); var attachment = new Attachment { ImageUrl = url }; yield return message.CreateAttachmentReply(attachment); }
public IEnumerable <IMessageResponse> GetResponse(ResponseContext context, RocketChatBot caller) { var message = context.Message; var search = message.Message.Replace(GiphyCommand, "").Trim(); var url = GetGiphy(search); var attachment = new Attachment { ImageUrl = url }; yield return(message.CreateAttachmentReply(attachment)); }
public void On_unsuccessful_login_throw() { var loginOption = Substitute.For <ILoginOption>(); var loginResult = AutoFixture.Create <MethodResult <LoginResult> >(); _driverMock.LoginAsync(loginOption).Returns(Task.FromResult(loginResult)); var bot = new RocketChatBot(_driverMock, _loggerMock); // Act Action loginAction = () => bot.LoginAsync(loginOption).Wait(); // Assert var exception = loginAction.ShouldThrow <Exception>().And; exception.Message.Should().Contain(loginResult.Error.Message); }
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 async Task On_successful_login_set_login_token() { var loginOption = Substitute.For <ILoginOption>(); var loginResult = AutoFixture.Build <MethodResult <LoginResult> >() .With(x => x.Error, null) .Create(); _driverMock.LoginAsync(loginOption).Returns(Task.FromResult(loginResult)); var bot = new RocketChatBot(_driverMock, _loggerMock); // Act await bot.LoginAsync(loginOption); // Assert bot.LoginToken.Should().Be(loginResult.Result.Token); }
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>()); }
public static async Task SubscribeToStreamNotifyAll(this RocketChatBot bot, StreamNotifyAllEvents @event, bool addEvent = false) { await bot.Driver.SubscribeToAsync("stream-notify-all", streamNotifyAllMaps[(int)@event], addEvent); }
public static async Task SubscribeToStreamRoomMessages(this RocketChatBot bot, string room = null, bool addEvent = false) { await bot.Driver.SubscribeToRoomAsync(room); }
public IEnumerable <IMessageResponse> GetResponse(ResponseContext context, RocketChatBot caller) { return(null); }