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(); }
/// <summary> /// Разобрать из json. /// </summary> /// <param name="response"> Ответ сервера. </param> /// <returns> </returns> public static GroupUpdate FromJson(VkResponse response) { var fromJson = JsonConvert.DeserializeObject <GroupUpdate>(response.ToString()); var resObj = response["object"]; switch (fromJson.Type) { case GroupLongPollUpdateType.MessageNew: case GroupLongPollUpdateType.MessageEdit: case GroupLongPollUpdateType.MessageReply: fromJson.Message = resObj; fromJson.UserId = fromJson.Message.FromId; break; case GroupLongPollUpdateType.MessageAllow: fromJson.MessageAllow = MessageAllow.FromJson(resObj); fromJson.UserId = fromJson.MessageAllow.UserId; break; case GroupLongPollUpdateType.MessageDeny: fromJson.MessageDeny = MessageDeny.FromJson(resObj); fromJson.UserId = fromJson.MessageDeny.UserId; break; case GroupLongPollUpdateType.PhotoNew: fromJson.Photo = resObj; break; case GroupLongPollUpdateType.PhotoCommentNew: case GroupLongPollUpdateType.PhotoCommentEdit: case GroupLongPollUpdateType.PhotoCommentRestore: fromJson.PhotoComment = PhotoComment.FromJson(resObj); fromJson.UserId = fromJson.PhotoComment.FromId; break; case GroupLongPollUpdateType.PhotoCommentDelete: fromJson.PhotoCommentDelete = PhotoCommentDelete.FromJson(resObj); fromJson.UserId = fromJson.PhotoCommentDelete.DeleterId; break; case GroupLongPollUpdateType.AudioNew: fromJson.Audio = resObj; break; case GroupLongPollUpdateType.VideoNew: fromJson.Video = resObj; break; case GroupLongPollUpdateType.VideoCommentNew: case GroupLongPollUpdateType.VideoCommentEdit: case GroupLongPollUpdateType.VideoCommentRestore: fromJson.VideoComment = VideoComment.FromJson(resObj); fromJson.UserId = fromJson.VideoComment.FromId; break; case GroupLongPollUpdateType.VideoCommentDelete: fromJson.VideoCommentDelete = VideoCommentDelete.FromJson(resObj); fromJson.UserId = fromJson.VideoCommentDelete.DeleterId; break; case GroupLongPollUpdateType.WallPostNew: case GroupLongPollUpdateType.WallRepost: fromJson.WallPost = WallPost.FromJson(resObj); fromJson.UserId = fromJson.WallPost.FromId > 0 ? fromJson.WallPost.FromId : null; break; case GroupLongPollUpdateType.WallReplyNew: case GroupLongPollUpdateType.WallReplyEdit: case GroupLongPollUpdateType.WallReplyRestore: fromJson.WallReply = WallReply.FromJson(resObj); fromJson.UserId = fromJson.WallReply.FromId; break; case GroupLongPollUpdateType.WallReplyDelete: fromJson.WallReplyDelete = WallReplyDelete.FromJson(resObj); fromJson.UserId = fromJson.WallReplyDelete.DeleterId; break; case GroupLongPollUpdateType.BoardPostNew: case GroupLongPollUpdateType.BoardPostEdit: case GroupLongPollUpdateType.BoardPostRestore: fromJson.BoardPost = BoardPost.FromJson(resObj); fromJson.UserId = fromJson.BoardPost.FromId > 0 ? fromJson.BoardPost.FromId : (long?)null; break; case GroupLongPollUpdateType.BoardPostDelete: fromJson.BoardPostDelete = BoardPostDelete.FromJson(resObj); break; case GroupLongPollUpdateType.MarketCommentNew: case GroupLongPollUpdateType.MarketCommentEdit: case GroupLongPollUpdateType.MarketCommentRestore: fromJson.MarketComment = MarketComment.FromJson(resObj); fromJson.UserId = fromJson.MarketComment.FromId; break; case GroupLongPollUpdateType.MarketCommentDelete: fromJson.MarketCommentDelete = MarketCommentDelete.FromJson(resObj); fromJson.UserId = fromJson.MarketCommentDelete.DeleterId; break; case GroupLongPollUpdateType.GroupLeave: fromJson.GroupLeave = GroupLeave.FromJson(resObj); fromJson.UserId = fromJson.GroupLeave.IsSelf == true ? fromJson.GroupLeave.UserId : null; break; case GroupLongPollUpdateType.GroupJoin: fromJson.GroupJoin = GroupJoin.FromJson(resObj); fromJson.UserId = fromJson.GroupJoin.UserId; break; case GroupLongPollUpdateType.UserBlock: fromJson.UserBlock = UserBlock.FromJson(resObj); fromJson.UserId = fromJson.UserBlock.AdminId; break; case GroupLongPollUpdateType.UserUnblock: fromJson.UserUnblock = UserUnblock.FromJson(resObj); fromJson.UserId = fromJson.UserUnblock.ByEndDate == true ? null : fromJson.UserUnblock.AdminId; break; case GroupLongPollUpdateType.PollVoteNew: fromJson.PollVoteNew = PollVoteNew.FromJson(resObj); fromJson.UserId = fromJson.PollVoteNew.UserId; break; case GroupLongPollUpdateType.GroupChangePhoto: fromJson.GroupChangePhoto = GroupChangePhoto.FromJson(resObj); fromJson.UserId = fromJson.GroupChangePhoto.UserId; break; case GroupLongPollUpdateType.GroupOfficersEdit: fromJson.GroupOfficersEdit = GroupOfficersEdit.FromJson(resObj); fromJson.UserId = fromJson.GroupOfficersEdit.AdminId; break; } return(fromJson); }