/// <summary>
        ///
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        protected virtual bool InitializeNameWithType(TelegramBotCommandInfo info)
        {
            var attrs = info.ClrType.GetCustomAttributes(typeof(TelegramBotCommandNameAttribute), false);

            if (attrs.Length != 1)
            {
                return(false);
            }

            var attr = (TelegramBotCommandNameAttribute)attrs.FirstOrDefault();

            if (attr.CommandNameType == null)
            {
                return(false);
            }

            var commandName = TelegramBotServicesContainer.CreateWithServices(attr.CommandNameType) as ITelegramBotCommandName;

            if (commandName == null)
            {
                return(false);
            }

            info.CommandName = commandName;
            commandName.Initialize(info);
            return(true);
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="command"></param>
        /// <param name="servicesProvider"></param>
        public TelegramBotCommandService(ITelegramBotCommand command, ITelegramBotServicesProvider servicesProvider)
        {
            Command = command;
            TelegramBotServicesProvider = servicesProvider;
            TelegramBotCommandFactory   = servicesProvider.GetService <ITelegramBotCommandFactory>();

            Info           = TelegramBotCommandFactory.GetCommandInfo(command.GetType());
            MessageService = TelegramBotServicesProvider.GetService <ITelegramBotMessageService>();
        }
        /// <summary>
        ///
        /// </summary>
        protected virtual void InitializeName(TelegramBotCommandInfo info)
        {
            if (InitializeNameWithType(info))
            {
                return;
            }

            InitializeNameWithService(info);
        }
示例#4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="info"></param>
        public virtual void Initialize(TelegramBotCommandInfo info)
        {
            Info = info;
            if (InitializeWithAttributes())
            {
                return;
            }

            InitializeWithResource();
        }
示例#5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="info"></param>
        private void InitializeHighestCommand(TelegramBotCommandInfo info)
        {
            var attrs = info.ClrType.GetCustomAttribute <TelegramBotHighestCommandAttribute>();

            if (attrs == null)
            {
                return;
            }

            info.IsHighestCommand = true;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        protected virtual void InitializeRole(TelegramBotCommandInfo info)
        {
            var commandrole = TelegramBotServicesContainer.GetService <ITelegramBotCommandRole>();

            if (commandrole == null)
            {
                return;
            }

            info.CommandRole = commandrole;
            commandrole.Initialize(info);
        }
示例#7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="info"></param>
        private void InitializeNearCommands(TelegramBotCommandInfo info)
        {
            var attrs = info.ClrType.GetCustomAttributes <TelegramBotNearCommandAttribute>();

            if (attrs == null)
            {
                return;
            }

            var nearCommands = attrs.Select(s => s.NearCommandType).ToList();

            info.NearCommands = Infos.Where(w => nearCommands.Contains(w.ClrType)).ToList();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="info"></param>
        protected virtual void InitializeIdentifier(TelegramBotCommandInfo info)
        {
            var dbCommand = _dbCommands.FirstOrDefault(f => f.ClrName == info.ClrType.Name);

            if (dbCommand == null)
            {
                dbCommand         = new TelegramBotDbCommand();
                dbCommand.ClrName = info.ClrType.Name;
                TelegramBotDb.Commands.Insert(dbCommand);
            }

            info.CommandId = dbCommand.Id;
        }
示例#9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public ITelegramBotCommand GetCommandInstance(TelegramBotCommandInfo info)
        {
            if (info == null)
            {
                return(null);
            }

            var activator = TelegramBotServicesProvider.GetService <ITelegramBotCommandActivator>();
            var command   = activator.ActivateCommand(info.ClrType);

            command.Initialize(TelegramBotServicesProvider);

            return(command);
        }
示例#10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="commandClrType"></param>
        private void InitializeInfo(Type commandClrType)
        {
            if (commandClrType.IsAbstract || commandClrType.IsInterface)
            {
                return;
            }

            var attr = commandClrType.GetCustomAttribute <TelegramBotCommandAttribute>();

            if (attr == null)
            {
                return;
            }

            var info = new TelegramBotCommandInfo();

            info.ClrType        = commandClrType;
            info.CommandClrName = commandClrType.Name;
            info.CommandType    = attr.Type;

            Infos.Add(info);
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="info"></param>
 public virtual void Initialize(TelegramBotCommandInfo info)
 {
     Info = info;
     InitializeWithAttribute();
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="info"></param>
 protected virtual void InitializeInfo(TelegramBotCommandInfo info)
 {
     InitializeIdentifier(info);
     InitializeName(info);
     InitializeRole(info);
 }