Exemplo n.º 1
0
        private static async Task MessageNew(IServiceProvider serviceProvider, DatabaseContext context, CallbackMessage message)
        {
            if (await IsPassedCallbackMessage(context, message))
            {
                return;
            }
            var innerMessage = VkNet.Model.Message.FromJson(new VkNet.Utils.VkResponse(message.Object));

            var vkPoolService = serviceProvider.GetService <VkPoolService>();

            var subscriber = await VkHelper.CreateSubscriber(context, vkPoolService, message.IdGroup, innerMessage.UserId);

            if (subscriber == null)
            {
                return;
            }

            await context.History_Messages.AddAsync(new History_Messages()
            {
                Dt = DateTime.UtcNow,
                IsOutgoingMessage = false,
                IdSubscriber      = subscriber.Id,
                Text = innerMessage.Body
            });

            bool isCancelMessaging = innerMessage.Body?.ToLower() == "стоп";

            if (!isCancelMessaging && (!subscriber.IsChatAllowed.HasValue || !subscriber.IsChatAllowed.Value))
            {
                await context.History_GroupActions.AddAsync(new History_GroupActions()
                {
                    ActionType   = (int)Models.Database.Common.GroupActionTypes.AcceptMessaging,
                    IdGroup      = message.IdGroup,
                    IdSubscriber = subscriber.Id,
                    Dt           = DateTime.UtcNow
                });

                subscriber.IsChatAllowed = true;
            }

            await context.SaveChangesAsync();

            var vkApi = await vkPoolService.GetGroupVkApi(message.IdGroup);

            if (isCancelMessaging)
            {
                await context.History_GroupActions.AddAsync(new History_GroupActions()
                {
                    ActionType   = (int)Models.Database.Common.GroupActionTypes.CancelMessaging,
                    IdGroup      = message.IdGroup,
                    IdSubscriber = subscriber.Id,
                    Dt           = DateTime.UtcNow
                });

                if (!subscriber.IsChatAllowed.HasValue || subscriber.IsChatAllowed.Value)
                {
                    subscriber.IsChatAllowed = false;
                    if (vkApi != null)
                    {
                        await context.History_Messages.AddAsync(new History_Messages()
                        {
                            Dt = DateTime.UtcNow,
                            IsOutgoingMessage = true,
                            IdSubscriber      = subscriber.Id,
                            Text = "Вы успешно отписаны от сообщений группы"
                        });

                        await vkApi.Messages.SendAsync(new VkNet.Model.RequestParams.MessagesSendParams()
                        {
                            Message = "Вы успешно отписаны от сообщений группы",
                            UserId  = innerMessage.UserId
                        });
                    }
                }

                await context.SaveChangesAsync();

                return;
            }

            if (vkApi == null)
            {
                return;
            }

            var replyToMessageResult = await CallbackHelper.ReplyToMessage(context, message.IdGroup, subscriber.Id, innerMessage);

            if (replyToMessageResult == null)
            {
                return;
            }

            if (replyToMessageResult.Item1.HasValue)
            {
                var _configuration = serviceProvider.GetService <IConfiguration>();

                var messageHelper = new MessageHelper(_configuration, context);
                await messageHelper.SendMessages(vkApi, message.IdGroup, replyToMessageResult.Item1.Value, innerMessage.UserId.Value);

                await context.History_Messages.AddAsync(new History_Messages()
                {
                    Dt = DateTime.UtcNow,
                    IsOutgoingMessage = true,
                    IdSubscriber      = subscriber.Id,
                    IdMessage         = replyToMessageResult.Item1
                });

                await context.SaveChangesAsync();
            }
            else if (replyToMessageResult.Item2)
            {
                await vkApi.Messages.MarkAsReadAsync(innerMessage.UserId.ToString(), groupId : message.IdGroup);
            }
        }