Example #1
0
 private ServiceProvider BuildQueryControllerProvider(
     IBotInputQuery inputQuery, IBotInputChat inputChat, RouteValueDictionary matchedData
     ) => CollectBaseServiceCollection()
 .AddSingleton(inputQuery)
 .AddSingleton(matchedData)
 .AddSingleton(inputChat)
 .BuildServiceProvider();
Example #2
0
        private void HandleView(object viewModel, IBotInputChat chat)
        {
            var provider   = BuildViewProvider(chat, viewModel);
            var viewAction = FindViewAction(viewModel, chat);
            var controller = provider.GetRequiredService(viewAction.Method.DeclaringType);

            viewAction.Method.InvokeWithInjection <object>(controller, provider);
        }
Example #3
0
 public void RepresentList(
     MenuViewModel menuView,
     TelegramMessageService messageService,
     IBotInputChat chat
     ) => messageService.SendText(
     chat,
     menuView.Header,
     CreateInlineKeyBoard(menuView.Buttons)
     );
Example #4
0
 private IBotInputQuery ConvertMessageToQuery(
     IServiceProvider provider, MessageAction action, IBotInputChat chat
     ) => new FromMessageQuery(
     chat.Id,
     action.Method.InvokeWithInjection <string>(
         provider.GetRequiredService(action.Method.DeclaringType),
         provider
         )
     );
Example #5
0
 private void RemoveMessage(IBotInputChat chat, BotMessage message)
 {
     try {
         RemoveMessagesInPlatform(chat, new[] { message });
         RemoveByPattern(m => m.Id == message.Id && m.PlatformId == chat.PlatformId);
     }
     catch (Exception e) {
         Console.WriteLine(e);
     }
 }
Example #6
0
        public void SaveOrder(IBotInputChat inputChat)
        {
            var chat     = _chatService.FindChat(inputChat);
            var orderDto = new Order {
                ChatId    = chat.Id,
                CreatedAt = DateTime.Now,
                AccountId = _account.Id
            };
            var order = AddEntity(orderDto);

            _orderItemService.SaveOrderItems(inputChat, order.Id);
        }
Example #7
0
 public void SaveMessage(IBotInputChat chat, IBotInputMessage message)
 {
     if (ById(message.Id) == null)
     {
         AddEntity(
             new BotMessage {
             Id         = message.Id,
             ChatId     = chat.Id,
             PlatformId = chat.PlatformId
         }
             );
     }
 }
Example #8
0
 public CartController(
     ProductService productService,
     CartService cartService,
     II18NService i18NService,
     ViewModelService viewModelService,
     RouteValueDictionary routeValues,
     IBotInputChat chat
     )
 {
     _productService   = productService;
     _routeValues      = routeValues;
     _cartService      = cartService;
     _chat             = chat;
     _i18NService      = i18NService;
     _viewModelService = viewModelService;
 }
Example #9
0
        private void RootQueryHandler(IBotInputQuery inputQuery, IBotInputChat inputChat)
        {
            var queryAction = FindQueryAction(inputQuery.Payload);
            var matchedData = queryAction.Metadata.Template.MatchRoute(inputQuery.Payload);

            using var provider = BuildQueryControllerProvider(inputQuery, inputChat, matchedData);

            Log(inputQuery);
            Log(inputChat);
            Log(queryAction.Metadata);
            Log(matchedData);
            EnsureChatSaved(provider, inputChat);
            CheckDisplayClearing(provider, queryAction, inputChat);
            HandleView(HandleQuery(provider, queryAction.Method), inputChat);
            UpdateLastExecutedQuery(provider, queryAction, inputChat);
        }
Example #10
0
 public OrderController(
     CartService cartService,
     RouteValueDictionary routeValues,
     ChatService chatService,
     OrderService orderService,
     ViewModelService viewModelService,
     II18NService i18NService,
     IBotInputChat chat
     )
 {
     _cartService      = cartService;
     _routeValues      = routeValues;
     _chatService      = chatService;
     _orderService     = orderService;
     _viewModelService = viewModelService;
     _i18NService      = i18NService;
     _chat             = chat;
 }
Example #11
0
 public void RepresentString(
     StringViewModel stringViewModel,
     TelegramMessageService messageService,
     IBotInputChat chat
     ) => messageService.SendText(
     chat,
     stringViewModel.Value,
     new ReplyKeyboardMarkup {
     Keyboard = new [] {
         new [] {
             new KeyboardButton {
                 Text = stringViewModel.Tip
             },
         }
     },
     OneTimeKeyboard = true,
 }
     );
Example #12
0
        private void RootMessageHandler(IBotInputMessage inputMessage, IBotInputChat inputChat)
        {
            using var provider = BuildMessageControllerProvider(inputMessage, inputChat);
            EnsureChatSaved(provider, inputChat);
            var chat          = FindChat(provider, inputChat);
            var messageAction = FindMessageAction(chat, inputMessage);
            var query         = ConvertMessageToQuery(provider, messageAction, inputChat);

            Log(inputChat);
            Log(chat);
            Log(inputMessage);
            SaveMessage(provider, inputChat, inputMessage);

            if (messageAction == null)
            {
                throw new ArgumentOutOfRangeException("No message handler for this message");
            }

            RootQueryHandler(query, inputChat);
        }
Example #13
0
        public void RepresentCardPage(
            CardPageView cardPage,
            TelegramMessageService messageService,
            IBotInputChat chat
            )
        {
            messageService.SendText(chat, cardPage.Header);
            messageService.SendMediaBulk(chat, cardPage.Cards.SkipLast(1).Select(CardToMediaDto));
            var lastCard = cardPage.Cards.Last();

            messageService.SendMedia(
                chat,
                lastCard.MediaUrl,
                lastCard.Content,
                CreateInlineKeyBoard(
                    ArrayFrom(
                        lastCard.Actions,
                        cardPage.Actions
                        )
                    )
                );
        }
Example #14
0
 public List <CartItem> GetCartItems(IBotInputChat chat) => ByPatternMany(
     item => item.ChatId == chat.Id,
     item => item.Product
     );
Example #15
0
 public void AddToCart(IBotInputChat chat, int productId) => AddEntity(new CartItem {
     ChatId    = chat.Id,
     ProductId = productId
 });
Example #16
0
 private ViewAction FindViewAction(object viewModel, IBotInputChat chat) => _viewActions.FirstOrDefault(
     action => action.Metadata.ViewModelType == viewModel.GetType() &&
     action.Metadata.PlatformId == chat.PlatformId
     );
Example #17
0
 private void CheckDisplayClearing(IServiceProvider provider, QueryAction action, IBotInputChat chat)
 {
     if (action.Metadata.ClearDisplayBeforeHandle)
     {
         provider.GetRequiredService <IBotMessageService>().ClearMessages(chat);
     }
 }
Example #18
0
 protected abstract void RemoveMessagesInPlatform(IBotInputChat chat, IEnumerable <BotMessage> messages);
Example #19
0
 public void ClearCart(IBotInputChat chat) => RemoveManyByPattern(item => item.ChatId == chat.Id);
Example #20
0
 public void ClearMessages(IBotInputChat chat) => ByPatternMany(
     message => message.ChatId == chat.Id && message.PlatformId == chat.PlatformId
     ).ForEach(messageModel => RemoveMessage(chat, messageModel));
Example #21
0
 private void HandleMessage(IBotInputMessage inputMessage, IBotInputChat inputChat)
 => TryToExecute(() => RootMessageHandler(inputMessage, inputChat));
Example #22
0
 private ServiceProvider BuildMessageControllerProvider(
     IBotInputMessage message, IBotInputChat inputChat
     ) => CollectBaseServiceCollection()
 .AddSingleton(message)
 .AddSingleton(inputChat)
 .BuildServiceProvider();
Example #23
0
 private IBotChat FindChat(IServiceProvider provider, IBotInputChat chat) => provider
 .GetRequiredService <IBotChatService>().FindChat(chat);
Example #24
0
 private ServiceProvider BuildViewProvider(
     IBotInputChat inputChat, object viewModel
     ) => CollectBaseServiceCollection()
 .AddSingleton(inputChat)
 .AddSingleton(viewModel.GetType(), viewModel)
 .BuildServiceProvider();
Example #25
0
 private void SaveMessage(
     IServiceProvider provider, IBotInputChat chat, IBotInputMessage message
     ) => provider.GetRequiredService <IBotMessageService>().SaveMessage(chat, message);
Example #26
0
 private void HandleQuery(IBotInputQuery inputQuery, IBotInputChat inputChat)
 => TryToExecute(() => RootQueryHandler(inputQuery, inputChat));
Example #27
0
 private void EnsureChatSaved(IServiceProvider provider, IBotInputChat chat) => provider
 .GetRequiredService <IBotChatService>().EnsureChatSaved(chat);
Example #28
0
 private void UpdateLastExecutedQuery(
     IServiceProvider provider, QueryAction action, IBotInputChat chat
     ) => provider.GetRequiredService <IBotChatService>().UpdateLastExecutedQuery(chat, action.Metadata.Template);
Example #29
0
 public bool IsCartEmpty(IBotInputChat chat) => Count(item => item.ChatId == chat.Id) == 0;