Ejemplo n.º 1
0
        private async Task UpdateReceiveLoop()
        {
            await Task.Yield();

            try
            {
                var me = await _api.GetMe();

                if (me != null)
                {
                    logger.Info("Bot self identification: {0}", me.ToJsonString());
                }

                long?nextOffset = null;

                for (; ;)
                {
                    try
                    {
                        var updates = await _api.GetUpdates(
                            offset : nextOffset,
                            timeout : CallTimeout,
                            limit : CallNumUpdatesLimit
                            );

                        if (updates != null && updates.Count > 0)
                        {
                            logger.Info($"Got {updates.Count} updates from the network");

                            foreach (var update in updates)
                            {
                                nextOffset = update.UpdateId + 1;
                                if (update.Message == null)
                                {
                                    continue;
                                }

                                User   from   = update.Message.From;
                                long   userId = update.Message.From.Id;
                                string text   = update.Message.Text ?? "";

                                logger.Info($"{from.ToString()}: {userId} {text}");
                                logger.Debug("full update: {0}", update.ToJsonString());

                                bool added = false;
                                lock (_queueLock)
                                {
                                    if (_queue.Count < QueueMaxSize)
                                    {
                                        _queue.Enqueue(update);
                                        _queueCount.Release(1);
                                        added = true;
                                    }
                                }

                                if (!added)
                                {
                                    logger.Error($"Queue overflow, update dropped");
                                    var msg = await _api.RespondToUpdate(update, "Bot internal queue overflow");
                                }
                            }
                        }
                        else
                        {
                            logger.Info("No updates");
                        }

                        await Task.Delay(100);
                    }
                    catch (TaskCanceledException ex)
                    {
                        logger.Error(ex, "Read task cancelled");
                        await Task.Delay(10 * 1000);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Telegram update loop got an exception");
                throw;
            }
        }
Ejemplo n.º 2
0
        public async Task RunReceiveLoop()
        {
            var me = await _api.GetMe();

            if (me != null)
            {
                Console.WriteLine("me: {0}", me.ToJsonString());
            }

            long?nextOffset = null;

            for (; ;)
            {
                try
                {
                    var updates = await _api.GetUpdates(
                        offset : nextOffset,
                        timeout : CallTimeout,
                        limit : CallNumUpdatesLimit
                        );

                    if (updates != null && updates.Count > 0)
                    {
                        foreach (var update in updates)
                        {
                            nextOffset = update.UpdateId + 1;
                            if (update.Message == null)
                            {
                                continue;
                            }

                            if (VerboseLogging)
                            {
                                Telegram.User from   = update.Message.From;
                                long          userId = update.Message.From.Id;
                                string        text   = update.Message.Text ?? "";
                                Console.WriteLine($"{from.ToString()}: {userId} {text}");
                            }

                            bool added = false;
                            lock (_queueLock)
                            {
                                if (_queue.Count < QueueMaxSize)
                                {
                                    _queue.Enqueue(update);
                                    Monitor.PulseAll(_queueLock);
                                    added = true;
                                }
                            }

                            if (!added)
                            {
                                var msg = await _api.RespondToUpdate(update, "Bot internal queue overflow");
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("No Updates");
                    }

                    await Task.Delay(100);
                }
                catch (TaskCanceledException ex)
                {
                    await Task.Delay(10 * 1000);
                }
            }
        }