public async Task StartPolling(TimeSpan?timeout = null) { _isStopPolling = false; if (timeout.HasValue) { _botClient.Timeout = timeout.Value; } var updateReceiver = new QueuedUpdateReceiver(_botClient); updateReceiver.StartReceiving(); _logger.LogInformation("Polling started!"); await foreach (var update in updateReceiver.YieldUpdatesAsync()) { await HandleUpdate(update); if (_isStopPolling) { _logger.LogInformation("Polling stopped!"); break; } } }
public async Task StopsIfStoppedAndOutOfUpdates() { var mockClient = new MockTelegramBotClient("1", "2", "3"); var receiver = new QueuedUpdateReceiver(mockClient); receiver.StartReceiving(); Task stopTask = Task.Run(() => { Task.Delay(150).Wait(); receiver.StopReceiving(); }); int updateCount = 0; await foreach (var update in receiver.YieldUpdatesAsync()) { updateCount++; } Assert.Equal(3, updateCount); Assert.Equal(0, mockClient.MessageGroupsLeft); Assert.Equal(0, receiver.PendingUpdates); Assert.False(receiver.IsReceiving); Assert.True(stopTask.IsCompleted); }
public async Task ReturnsReceivedPendingUpdates() { var mockClient = new MockTelegramBotClient("foo-bar", "123"); var receiver = new QueuedUpdateReceiver(mockClient); Assert.Equal(2, mockClient.MessageGroupsLeft); Assert.Equal(0, receiver.PendingUpdates); receiver.StartReceiving(); await Task.Delay(200); Assert.Equal(0, mockClient.MessageGroupsLeft); Assert.Equal(3, receiver.PendingUpdates); await foreach (var update in receiver.YieldUpdatesAsync()) { Assert.Equal("foo", update.Message.Text); break; } Assert.Equal(2, receiver.PendingUpdates); await foreach (var update in receiver.YieldUpdatesAsync()) { Assert.Equal("bar", update.Message.Text); break; } Assert.Equal(1, receiver.PendingUpdates); await foreach (var update in receiver.YieldUpdatesAsync()) { Assert.Equal("123", update.Message.Text); break; } Assert.Equal(0, receiver.PendingUpdates); receiver.StopReceiving(); await foreach (var update in receiver.YieldUpdatesAsync()) { // No pending updates and we've called StopReceiving Assert.False(true); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var updateReceiver = new QueuedUpdateReceiver(_botClient); updateReceiver.StartReceiving(_botOptions.Value.AllowedUpdates, cancellationToken: stoppingToken); await foreach (var update in updateReceiver.YieldUpdatesAsync().WithCancellation(stoppingToken)) { _logger.LogDebug("Message with type {0} received from chat {1}", update.Message.Type, update.Message.Chat.Id); var photo = update.Message.Photo?[^ 1];
public async Task StartAsync(CancellationToken cancellationToken) { _queuedUpdateReceiver.StartReceiving(cancellationToken: cancellationToken); try { await foreach (Update update in _queuedUpdateReceiver.YieldUpdatesAsync().WithCancellation(cancellationToken)) { if (update.Message is Message message) { try { using var scope = _serviceProvider.CreateScope(); var messageContext = scope.ServiceProvider.GetRequiredService <MessageContext>(); var publishClient = scope.ServiceProvider.GetRequiredService <IPublishClient>(); var profiles = message.Text .ToTarget() .Validate() .MakeProfiles <DomainCollectorProfile>(messageContext .FillMessageContext(message.Chat.Id, message.Chat.Username)); var confirmations = await publishClient.Publish(profiles).Extract(); await _queuedUpdateReceiver.BotClient.SendTextMessageAsync( message.Chat, string.Join(Environment.NewLine, confirmations), cancellationToken : cancellationToken); } catch (Exception e) { await _queuedUpdateReceiver.BotClient.SendTextMessageAsync( message.Chat, $"sorry, input not recognized, {e.Message.ToLower()}", cancellationToken : cancellationToken); } } } } catch (Exception e) { _logger.LogError(e, "An error was thrown while updating the message receiver. '{ex.Message}'", e.Message); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { try { var botName = await _botClient.GetMeAsync(); _logger.Information($"Bot's name: {botName.FirstName}"); var updateReceiver = new QueuedUpdateReceiver(_botClient); updateReceiver.StartReceiving(new UpdateType[] { UpdateType.Message }); await foreach (Update update in updateReceiver.YieldUpdatesAsync()) { await _messageHandler.Handle(update.Message); } } catch (Exception e) { _logger.Error(e, "{nameof(Worker)}.{nameof(ExecuteAsync)} Some unhandled error occurred, stopping application."); _hostApplicationLifetime.StopApplication(); } }
public async Task ReceivesUpdatesInTheBackground() { var mockClient = new MockTelegramBotClient("1", "2", "3"); var receiver = new QueuedUpdateReceiver(mockClient); Assert.Equal(3, mockClient.MessageGroupsLeft); receiver.StartReceiving(); await foreach (var update in receiver.YieldUpdatesAsync()) { Assert.Equal("1", update.Message.Text); await Task.Delay(100); break; } Assert.Equal(0, mockClient.MessageGroupsLeft); Assert.Equal(2, receiver.PendingUpdates); receiver.StopReceiving(); }