Ejemplo n.º 1
0
        /// <summary>
        /// Passes an update to the chain for handling
        /// </summary>
        /// <param name="args">Data structure containing relevant references for update handling</param>
        public async Task Handle(UpdateHandlerArgs args)
        {
            // Catch exceptions so they don't cause the entire service to crash
            try
            {
                if ((null == HandlesUpdateType) || (args.Update.Type == HandlesUpdateType))
                {
                    await HandlerLogicImpl(args);
                }
            }
            catch (Exception e)
            {
                await Say.Error($"Internal error while handling update {args.Update.Id}: {e.Message}");
            }

            // End of the chain has reached as soon as the update has been marked as handled
            if (args.Handled)
            {
                return;
            }

            // Invoke next handler in chain, otherwise throw Exception that update was not handled
            if (null != _nextHandler)
            {
                await _nextHandler.Handle(args);
            }
            else
            {
                throw new UpdateNotHandledException(args.Update);
            }
        }
Ejemplo n.º 2
0
        static async Task Main(string[] args)
        {
            // ------------------------------------------------------------------------------------
            // Step 1: Verify and/ or set up environement
            if (String.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("BOT_TOKEN")))
            {
                // Environment.SetEnvironmentVariable("BOT_TOKEN", "");
                await Say.Error("Fatal: Environment Variable BOT_TOKEN empty");

                return;
            }

            // ------------------------------------------------------------------------------------
            // Step 2 - Setting up the bot object
            _UpdateHandlerChain = new DevHandler();
            _UpdateHandlerChain.Add(new StartHandler());

            _Bot = new TelegramBotClient(Environment.GetEnvironmentVariable("BOT_TOKEN"));

            _BotUpdatesChannel = Channel.CreateUnbounded <Update>(
                new UnboundedChannelOptions()
            {
                SingleWriter = true,
                SingleReader = true
            });

            var updateHandler = Task.Run(async() => await _HandleUpdates());
            var updateFetcher = Task.Run(async() => await _FetchUpdates(_CancelWorkerTasks.Token));

            // Setting up Event Handler for Smooth Termination ( Improved Thread.Sleep(forever) )
            Console.CancelKeyPress += async(object sender, ConsoleCancelEventArgs e) =>
            {
                e.Cancel = true;                                        // Intercept the CTRL+C
                _TerminateProcessEvent.Set();                           // Signal the main thread

                await Say.Warning("Cancellation request sent");
            };

            _TerminateProcessEvent.WaitOne();                           // Suspend main thread until signaled

            // ------------------------------------------------------------------------------------
            // Step 3 - Tidy up: Release ressources, flush buffers, etc
            _CancelWorkerTasks.Cancel();
            await Task.WhenAll(updateFetcher, updateHandler);

            await Say.Success("Thank you very much. You made a simple bot very happy");
        }