Example #1
0
        private async Task CallbackQueryInvoker(CallbackQuery callbackQuery)
        {
            //Log stats
            await StatsManager.AddStatAction(ActionType.CallbackQuery);

            //Get message data
            var user = await UserManager.AddOrUpdateUser(callbackQuery.From);

            //Get all handlers
            var handlers = ReflectiveEnumerator.GetEnumerableOfType <BaseCallbackQueryHandler>();

            foreach (var handlerType in handlers)
            {
                //Find method in handler
                MethodInfo[] handlerMethods = handlerType.GetMethods();

                foreach (var method in handlerMethods)
                {
                    //Pattern matching for message text
                    if (BaseHandler.MatchMethod(method, callbackQuery.Data))
                    {
                        //Check user access by role
                        if (!BaseHandler.ValidateAccess(method, user))
                        {
                            return;
                        }

                        try
                        {
                            //Get and send chatAction from attributes
                            var chatAction = BaseHandler.GetChatActionAttributes(method);
                            if (chatAction.HasValue)
                            {
                                await Bot.SendChatActionAsync(callbackQuery.Message.Chat.Id, chatAction.Value);
                            }

                            //Cast handler object
                            var handler = Activator.CreateInstance(handlerType);

                            //Set params
                            ((BaseHandler)handler).Init(Bot, user, callbackQuery: callbackQuery);

                            //Invoke method
                            await(Task) method.Invoke(handler, parameters: new object[] { });
                        }
                        catch (Exception ex)
                        {
                            await LogsManager.CreateLog($"Invoker error *{ex.Message}*", LogLevel.Critical);

                            //Log stats
                            await StatsManager.AddStatAction(ActionType.Error);
                        }
                    }
                    else
                    {
                        //Cant find method
                        //await LogsManager.CreateLog($"Can't find method for *{messageEventArgs.Message.Text}*", LogLevel.Warning);
                        //Console.WriteLine($"Can't find method for *{messageEventArgs.Message.Text}*");
                    }
                }
            }
        }