public TelegramBot(TelegramBotConfiguration config) { _messageHandlers = new List <Action <Message> >(); _client = new TelegramBotClient(config.Token); _client.StartReceiving(); _client.OnMessage += OnMessage; }
public TelegramUpdateHandler(ILogger <TelegramUpdateHandler> logger, IHandleTelegramMessage messageHandler, ITelegramBotService botService, IOptions <TelegramBotConfiguration> options) { _logger = logger; _messageHandler = messageHandler; _botService = botService; _configuration = options.Value; }
public void Constructor_should_create_client() { var fakeOptions = new Mock <IOptions <TelegramBotConfiguration> >(); var fakeConfig = new TelegramBotConfiguration { BotToken = "1234567:4TT8bAc8GHUspu3ERYn-KGcvsvGB9u_n4ddy" }; fakeOptions.SetupGet(x => x.Value).Returns(fakeConfig); var service = new TelegramBotService(fakeOptions.Object); service.Client.Should().NotBeNull(); }
public async Task HandleAsync_does_not_report_or_catch_exception_if_message_handler_fails() { var fakeLogger = new Mock <ILogger <TelegramUpdateHandler> >(); var fakeMessageHandler = new Mock <IHandleTelegramMessage>(); var fakeOptions = new Mock <IOptions <TelegramBotConfiguration> >(); var fakeBotService = new Mock <ITelegramBotService>(); var exception = new Exception("whoops"); fakeMessageHandler.Setup(x => x.HandleAsync(It.IsAny <TelegramMessage>())) .ThrowsAsync(exception); var fakeClient = new Mock <ITelegramBotClient>(); fakeBotService.SetupGet(x => x.Client).Returns(fakeClient.Object); var fakeConfig = new TelegramBotConfiguration { EnableExceptionForwarding = false, ExceptionChatId = 42 }; fakeOptions.SetupGet(x => x.Value).Returns(fakeConfig); var handler = new TelegramUpdateHandler( fakeLogger.Object, fakeMessageHandler.Object, fakeBotService.Object, fakeOptions.Object); Func <Task> action = async() => await handler.HandleAsync(TelegramUpdateFactory.CreateFakeTelegramUpdate(UpdateType.Message)); await action.Should().ThrowAsync <Exception>(); fakeClient.VerifyNoOtherCalls(); }
public async Task HandleAsync_catches_and_reports_exception_when_message_handler_throws_and_exception_forwarding_is_enabled() { var fakeLogger = new Mock <ILogger <TelegramUpdateHandler> >(); var fakeMessageHandler = new Mock <IHandleTelegramMessage>(); var fakeOptions = new Mock <IOptions <TelegramBotConfiguration> >(); var fakeBotService = new Mock <ITelegramBotService>(); var exception = new Exception("whoops"); fakeMessageHandler.Setup(x => x.HandleAsync(It.IsAny <TelegramMessage>())) .ThrowsAsync(exception); var fakeClient = new Mock <ITelegramBotClient>(); fakeBotService.SetupGet(x => x.Client).Returns(fakeClient.Object); var fakeConfig = new TelegramBotConfiguration { EnableExceptionForwarding = true, ExceptionChatId = 42 }; fakeOptions.SetupGet(x => x.Value).Returns(fakeConfig); var handler = new TelegramUpdateHandler( fakeLogger.Object, fakeMessageHandler.Object, fakeBotService.Object, fakeOptions.Object); await handler.HandleAsync(TelegramUpdateFactory.CreateFakeTelegramUpdate(UpdateType.Message)); var expectedMessage = $"```\n{exception}\n```"; fakeClient.VerifySendTextMessageAsync(fakeConfig.ExceptionChatId, expectedMessage, Times.Once(), ParseMode.Markdown); }
public TelegramBotService(IOptions <TelegramBotConfiguration> config) { _config = config.Value; _bot = new TelegramBotClient(_config.BotToken); _bot.OnMessage += BotOnMessageReceived; }
public BotController(TelegramBotConfiguration telegramBotConfiguration) { TelegramBotConfiguration = telegramBotConfiguration; TelegramBotClient = new TelegramBotClient(TelegramBotConfiguration.Token); }
// TODO: This needs unit tests public static IServiceCollection AddTelegramBot(this IServiceCollection services, TelegramBotConfiguration configuration) { var config = configuration ?? throw new ArgumentNullException(nameof(configuration)); services.AddSingleton <ITelegramBotService, TelegramBotService>(); services.Configure <TelegramBotConfiguration>(options => { options.BotToken = config.BotToken; options.Socks5Host = config.Socks5Host; options.Socks5Port = config.Socks5Port; }); services.AddTransient <IHandleTelegramUpdate, TelegramUpdateHandler>(); services.AddTransient <IHandleTelegramMessage, TelegramMessageHandler>(); services.AddTransient <IHandleTelegramTextMessage, TelegramTextMessageHandler>(); services.AddTransient <IExecuteTelegramCommand, EchoCommandExecutor>(); return(services); }
public void Init(TelegramBotConfiguration telegramBotConfiguration) { IBot bot = new TelegramBot(telegramBotConfiguration); _bots.Add(bot); }
public InitialBotService(TelegramBotConfiguration telegramBotConfiguration) { TelegramBotConfiguration = telegramBotConfiguration; TelegramBotClient = new TelegramBotClient(TelegramBotConfiguration.Token); }