public static IFramework AddBotForm <TModel>(this IFramework framework, string[] routes)
            where TModel : class
        {
            var modelTypeInfo     = typeof(TModel).GetTypeInfo();
            var completionActions = modelTypeInfo.GetCustomAttributes().Select(x => x as CompletionActionAttribute).Where(x => x?.Type != null && !registeredCompletionActions.Contains(x.Type));

            foreach (var completionAction in completionActions)
            {
                if (!registeredCompletionActions.Contains(completionAction.Type))
                {
                    framework.AddService(s => s.Service <IFormReplyCompletionAction>().As(completionAction.Type).Keyed(completionAction.TypeKey));
                    registeredCompletionActions.Add(completionAction.Type);
                }
            }

            var propertiesAttributes = modelTypeInfo.GetProperties().SelectMany(x => x.GetCustomAttributes());
            var validators           = propertiesAttributes.Select(x => x as PropertyValidatorAttribute).Where(x => x?.Type != null && !registeredValidators.Contains(x.Type));

            foreach (var validator in validators)
            {
                if (!registeredValidators.Contains(validator.Type))
                {
                    framework.AddService(s => s.Service <IFormReplyPropertyValidator>().As(validator.Type).Keyed(validator.TypeKey));
                    registeredValidators.Add(validator.Type);
                }
            }

            var actions = propertiesAttributes.Select(x => x as PropertyReplyAttribute).Where(x => x?.Type != null && !registeredActions.Contains(x.Type));

            foreach (var action in actions)
            {
                if (!registeredActions.Contains(action.Type))
                {
                    AddBotAction(framework, action.Type, action.TypeKey);
                    registeredActions.Add(action.Type);
                }
            }

            return(AddBotAction <FormReplyAction <TModel> >(framework, routes));
        }
        private static IFramework AddBotAction(this IFramework framework, Type replyActionType, string[] routes, bool isDefault, bool isGlobalCommand)
        {
            var replyId   = replyActionType.FullName;
            var replyItem = new ReplyItem
            {
                Routes          = routes,
                ReplyId         = replyId,
                ReplyType       = replyId,
                IsDefault       = isDefault,
                IsGlobalCommand = isGlobalCommand
            };

            framework.AddService(s => s.Service(typeof(ReplyItem)).As(replyItem).Keyed(replyId).Lifetime(Lifetime.Singleton));
            return(AddBotAction(framework, replyActionType, replyId));
        }
Beispiel #3
0
 public static IFramework AddConsoleLogger(this IFramework framework)
 {
     return(framework.AddService(s => s.Service <ILogTarget>().As(new ConsoleLogger()).Lifetime(Lifetime.Singleton)));
 }
Beispiel #4
0
        public void Bootstrapp(IFramework framework)
        {
            framework.AddService(s => s.Service <IConnector>().As <MessangerConnector>().Keyed(ConnectorType.Messanger));
            framework.AddTransientService <IMessangerSecurity, MessangerSecurity>();
            framework.AddService(s => s.Service <IDispatcher>().As <MessangerDispatcher>().Keyed(ConnectorType.Messanger));
            framework.AddService(s => s.Service <IUserManager>().As <MessangerUserManager>().Keyed(ConnectorType.Messanger));

            framework.AddService(s => s.Service <IHandler>().As <ContextHandler>().Keyed(HandlerType.Context));
            framework.AddService(s => s.Service <IHandler>().As <UserDataHandler>().Keyed(HandlerType.UserData));
            framework.AddService(s => s.Service <IHandler>().As <RouteHandler>().Keyed(HandlerType.Route));
            framework.AddService(s => s.Service <IHandler>().As <ReplyHandler>().Keyed(HandlerType.Reply));
            framework.AddService(s => s.Service <IHandler>().As <DispatchHandler>().Keyed(HandlerType.Dispatch));
            framework.AddService(s => s.Service <IHandler>().As <ContextKeeperHandler>().Keyed(HandlerType.ContextKeeper));

            framework.AddService(s => s.Service <IUserManager>().As <MessangerUserManager>().Keyed(ConnectorType.Messanger));

            framework.AddService(s => s.Service <IReplyBuilder>().As <RawReplyBuilder>().Keyed("raw"));
            framework.AddService(s => s.Service <IReplyBuilder>().As <TextReplyBuilder>().Keyed("text"));
            framework.AddService(s => s.Service <IReplyBuilder>().As <ImageReplyBuilder>().Keyed("image"));
            framework.AddService(s => s.Service <IReplyBuilder>().As <VideoReplyBuilder>().Keyed("video"));
            framework.AddService(s => s.Service <IReplyBuilder>().As <AudioReplyBuilder>().Keyed("audio"));
            framework.AddService(s => s.Service <IReplyBuilder>().As <FileReplyBuilder>().Keyed("file"));
            framework.AddService(s => s.Service <IReplyBuilder>().As <EnumReplyBuilder>().Keyed("enum"));
            framework.AddService(s => s.Service <IReplyBuilder>().As <LocationReplyBuilder>().Keyed("location"));
            framework.AddService(s => s.Service <IReplyBuilder>().As <FormReplyBuilder>().Keyed("form"));
            framework.AddService(s => s.Service <IReplyBuilder>().As <HttpReplyBuilder>().Keyed("http"));
            framework.AddService(s => s.Service <IReplyBuilder>().As <ButtonTemplateReplyBuilder>().Keyed("buttonTemplate"));
            framework.AddService(s => s.Service <IReplyBuilder>().As <CarouselReplyBuilder>().Keyed("carousel"));
            framework.AddService(s => s.Service <IReplyBuilder>().As <ListReplyBuilder>().Keyed("list"));
            framework.AddService(s => s.Service <IReplyBuilder>().As <CallReplyBuilder>().Keyed("call"));

            framework.AddService(s => s.Service <IReplyBuilder <RawReplyMessage> >().As <RawReplyBuilder>());
            framework.AddService(s => s.Service <IReplyBuilder <TextReplyMessage> >().As <TextReplyBuilder>());
            framework.AddService(s => s.Service <IReplyBuilder <ImageReplyMessage> >().As <ImageReplyBuilder>());
            framework.AddService(s => s.Service <IReplyBuilder <VideoReplyMessage> >().As <VideoReplyBuilder>());
            framework.AddService(s => s.Service <IReplyBuilder <AudioReplyMessage> >().As <AudioReplyBuilder>());
            framework.AddService(s => s.Service <IReplyBuilder <FileReplyMessage> >().As <FileReplyBuilder>());
            framework.AddService(s => s.Service <IReplyBuilder <EnumReplyMessage> >().As <EnumReplyBuilder>());
            framework.AddService(s => s.Service <IReplyBuilder <LocationReplyMessage> >().As <LocationReplyBuilder>());
            framework.AddService(s => s.Service <IReplyBuilder <FormReplyMessage> >().As <FormReplyBuilder>());
            framework.AddService(s => s.Service <IReplyBuilder <HttpReplyMessage> >().As <HttpReplyBuilder>());
            framework.AddService(s => s.Service <IReplyBuilder <ButtonTemplateReplyMessage> >().As <ButtonTemplateReplyBuilder>());
            framework.AddService(s => s.Service <IReplyBuilder <CarouselReplyMessage> >().As <CarouselReplyBuilder>());
            framework.AddService(s => s.Service <IReplyBuilder <ListReplyMessage> >().As <ListReplyBuilder>());
            framework.AddService(s => s.Service <IReplyBuilder <CallReplyMessage> >().As <CallReplyBuilder>());

            framework.AddService(s => s.Service <IFormReplyCompletionAction>().As <TextFormReplyCompletionAction>().Keyed("text"));
            framework.AddService(s => s.Service <IFormReplyCompletionAction>().As <HttpFormReplyCompletionAction>().Keyed("http"));

            framework.AddTransientService <IHandlerManager, HandlerManager>();
            framework.AddSingletonService <IReplyConfiguration, ReplyManager>();
            framework.AddSingletonService <IReplyFactory, ReplyFactory>();
            framework.AddSingletonService <IRoutingConfiguration, ReplyManager>();
            framework.AddSingletonService <IGenericExpressionFactory, GenericExpressionFactory>();

            framework.AddService(s => s.Service <IRouter>().As <DefaultRouter>().Keyed(nameof(DefaultRouter)));
            framework.AddService(s => s.Service <IRouter>().As <RegexRouter>().Keyed(nameof(RegexRouter)).Lifetime(Lifetime.Singleton));

            framework.AddTransientService <IMessageQueue, MemoryMessageQueue>();
            framework.AddTransientService <IBot, QBot>();
            framework.AddSingletonService <IBotConfig, BotConfig>();
            framework.AddTransientService <Func <object, IReplyBuilder> >(s => (Func <object, IReplyBuilder>)((key) => (IReplyBuilder)s.GetService(key, typeof(IReplyBuilder))));
            framework.AddTransientService <Func <object, IDispatcher> >(s => (Func <object, IDispatcher>)((key) => (IDispatcher)s.GetService(key, typeof(IDispatcher))));
            framework.AddTransientService <Func <object, IUserManager> >(s => (Func <object, IUserManager>)((key) => (IUserManager)s.GetService(key, typeof(IUserManager))));
            framework.AddTransientService <Func <object, IFormReplyCompletionAction> >(s => (Func <object, IFormReplyCompletionAction>)((key) => (IFormReplyCompletionAction)s.GetService(key, typeof(IFormReplyCompletionAction))));
            framework.AddTransientService <Func <object, IFormReplyPropertyActiveConstraint> >(s => (Func <object, IFormReplyPropertyActiveConstraint>)((key) => (IFormReplyPropertyActiveConstraint)s.GetService(key, typeof(IFormReplyPropertyActiveConstraint))));
            framework.AddTransientService <Func <object, IFormReplyPropertyValidator> >(s => (Func <object, IFormReplyPropertyValidator>)((key) => (IFormReplyPropertyValidator)s.GetService(key, typeof(IFormReplyPropertyValidator))));
            framework.AddTransientService <Func <object, IFormReplyPropertyConfirmation> >(s => (Func <object, IFormReplyPropertyConfirmation>)((key) => (IFormReplyPropertyConfirmation)s.GetService(key, typeof(IFormReplyPropertyConfirmation))));

            framework.AddService(s => s.Service <ReplyItem>().As(new ReplyItem()));
        }