Example #1
0
        private static void GetUpdates(ITelegramClient telegramClient)
        {
            UpdateType[] updateTypes =
            {
                UpdateType.All
            };
            Update[] updates = null;

            try
            {
                updates = telegramClient.GetUpdates(0, 100, 0, updateTypes);
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not get updates, make sure you don't have an active webHook since that will prevent from getting updates manually");
                return;
            }

            Console.WriteLine($"{updates.Length} update(s) found");

            foreach (var update in updates)
            {
                Console.WriteLine("---");
                Console.WriteLine($"Update: {update.Id}");
                Console.WriteLine($"ChatId: {update.Message.Chat.Id}");
                Console.WriteLine($"Message: {update.Message.Text}");
                Console.WriteLine($"Timestamp: {update.Message.Date.ToString()}");
            }
        }
Example #2
0
        private async Task  Pooling(CancellationToken cancellationToken)
        {
            await Task.Yield();

            try {
                _logger.LogDebug("Fetching last update id from history storage");
                _offset = (await _historyStorage.GetLastUpdateId()) + 1;
            }
            catch (Exception e) {
                _logger.LogError(0, e, "Cannot fetch last update id");
                throw;
            }

            while (!cancellationToken.IsCancellationRequested)
            {
                try {
                    var results = await _client.GetUpdates(_offset, _oneTimeLimit, _poolingTimeout, _fieldsFilter,
                                                           cancellationToken);

                    foreach (var update in results)
                    {
                        _logger.LogDebug($"Process update #{update.Id}: ${JsonConvert.SerializeObject(update)}");
                        OnUpdateReceived(new UpdateEventArgs(update));

                        _offset = update.Id + 1;
                        await _historyStorage.SaveLastUpdateId(update.Id);
                    }
                }
                catch (Exception e) {
                    _logger.LogError(0, e, "Error occurred while polling");
                }
            }
        }
Example #3
0
        private ResponseDto <UpdateDto[]> GetUpdates(long offset, CancellationToken token)
        {
            const int requestLimit  = 100;
            var       getUpdatesDto = new GetUpdatesDto(offset, requestLimit);

            return(_telegramClient.GetUpdates(getUpdatesDto, token));
        }