Example #1
0
        public Updates CallbackMessageHandler(Updates updates, VkCoreApiBase vkApi)
        {
            var msg = Message.FromJson(new VkResponse(updates.Object));

            vkApi.Core.Log.Debug($"message from Group:{updates.GroupId} and Chat:{msg.ChatId} by {msg.FromId} Action:{msg.Action?.Type?.ToString() ?? "text"}  \"{msg.Text}\"");
            return(updates);
        }
Example #2
0
        private async Task <UpdateContext?> CreateUpdateContext(Update update)
        {
            switch (update.Type)
            {
            case "message_new":
                var originalMessage = VkMessage.FromJson(new VkResponse(update.Object));
                var message         = VkConverter.ToInMessage(originalMessage);
                var chat            = await _messenger.GetChat(originalMessage.PeerId !.Value).ConfigureAwait(false);

                var sender = await _messenger.GetUser(originalMessage.FromId !.Value).ConfigureAwait(false);

                return(new UpdateContext(_messenger, chat, sender, message));

            default:
                return(null);
            }
        }
Example #3
0
        public IActionResult Callback([FromBody] Updates updates)
        {
            try
            {
                if (updates.SecretKey != _secretKey)
                {
                    return(BadRequest("Secret key is incorrect!"));
                }

                if (updates.Type == CallbackReceive.Confirmation)
                {
                    return(Ok(Core.Configuration.GetValue($"Config:Groups:{updates.GroupId}:Confirmation", Core.Configuration["Config:Confirmation"])));
                }

                new Thread(() =>
                {
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    try
                    {
                        VkCoreApiBase vkApi = Core.VkApi.Get(updates.GroupId);
                        Core.PluginManager.PluginCallbackHandler(ref updates, vkApi);
                        if (updates == null)
                        {
                            return;
                        }

                        var response = new VkResponse(updates.Object);

                        var update = GroupUpdate.FromJson(response);

                        switch (updates.Type)
                        {
                        case CallbackReceive.Message.New:
                            {
                                var msg = Message.FromJson(response);

                                if (msg.Date.HasValue && (DateTime.UtcNow - msg.Date.Value).TotalSeconds > _messageResendBlockTime)
                                {
                                    return;
                                }

                                IUser user    = vkApi.GetUser(msg.FromId.Value);
                                BaseChat chat = vkApi.GetChat(msg.PeerId.Value);

                                lock (chat)
                                {
                                    vkApi.MessageHandler.OnMessage(user, msg.Text, chat, msg);
                                }
                                break;
                            }

                        case CallbackReceive.Message.Event:
                            {
                                var msgEvent = MessageEvent.FromJson(response);

                                IUser user = vkApi.GetUser(msgEvent.UserId.Value);

                                if (user is User _user)
                                {
                                    BaseChat chat = vkApi.GetChat(msgEvent.PeerId.Value);

                                    lock (chat)
                                    {
                                        vkApi.MessageHandler.ClickButton(chat, _user, new UI.EventId(msgEvent.EventId), msgEvent.Payload);
                                    }
                                }
                                break;
                            }

                        case CallbackReceive.Group.OfficersEdit:
                            {
                                var msgEvent = GroupOfficersEdit.FromJson(response);

                                var userId = msgEvent.UserId.Value;

                                vkApi.Group.Managers.Remove(userId);

                                var level = (int)msgEvent.LevelNew.Value;

                                if (level > 0)
                                {
                                    var permissionLevel = (int)Math.Pow(10, level + 1);
                                    vkApi.Group.Managers.Add(userId, (UserPermission)level);
                                }
                                break;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Core.Log.Error(e.ToString());
                    }
                    Core.Log.Debug(stopwatch.ElapsedMilliseconds.ToString());
                    stopwatch.Stop();
                })
                {
                    IsBackground = true
                }.Start();
            }