private async Task <Message> _extractMessage(VkNet.Model.Message msg, bool extractConversation = true) { Conversation conversation = null; Person person = null; IEnumerable <Message> forwarded = null; IEnumerable <Attachment> attachments = null; await Task.WhenAll( Task.Run(async() => { if (extractConversation) { conversation = await _extractConversation(msg); } }), Task.Run(async() => { person = await _extractPerson(msg); }), Task.Run(async() => { if (msg.ForwardedMessages != null && msg.ForwardedMessages.Count > 0) { forwarded = await Task.WhenAll( msg.ForwardedMessages.Select(fwd => _extractMessage(fwd, false))); } else if (msg.ReplyMessage != null) { forwarded = new[] { await _extractMessage(msg.ReplyMessage, false) }; } }), Task.Run(() => { attachments = _extractAttachments(msg); }) ); var body = msg.Text ?? ""; return(new Message(conversation, person, body, forwarded, attachments)); }
private async Task <Person> _extractPerson(VkNet.Model.Message msg) { try { if (msg.FromId == null || msg.FromId < 1) { throw new Exception("Invalid user id"); } var userId = (long)msg.FromId; string displayName = null; if (!DisplayNameCache.TryGetValue(userId, out displayName)) { var users = await ApiClient.Users.GetAsync(new[] { userId }); if (users.Any()) { displayName = $"{users.First().FirstName} {users.First().LastName}".Trim(); DisplayNameCache.Add(userId, displayName); } } return(new VkPerson(this, msg.FromId.ToString(), displayName)); } catch (Exception) { var id = msg.FromId?.ToString() ?? "0"; return(new VkPerson(this, id, $"[id{id}]")); } }
private async Task <Conversation> _extractConversation(VkNet.Model.Message msg) { if (msg.PeerId == null) { throw new NullReferenceException("Peer id can not be null"); } var peerId = (long)msg.PeerId; var vkConversation = await ApiClient.Messages.GetConversationsByIdAsync(new[] { peerId }, null, null, _groupId); var title = vkConversation.Items.Any() ? vkConversation.Items.First()?.ChatSettings?.Title ?? $"#{peerId}" : $"#{peerId}"; return(new Conversation(this, peerId.ToString(), title)); }
private List <Attachment> _extractAttachments(VkNet.Model.Message msg) { if (msg.Attachments.IsNullOrEmpty() && msg.Geo == null) { return(null); } var attachments = new List <Attachment>(); foreach (var at in msg.Attachments) { switch (at.Instance) { case Photo photo: { var maxPhoto = photo.Sizes.MaxBy(p => p.Width).First(); attachments.Add(new PhotoAttachment(maxPhoto.Url.ToString(), photo, photo.Text, mimeType: "image/jpeg")); break; } case Video video: attachments.Add(new LinkAttachment($"https://vk.com/video{video.OwnerId}_{video.Id}", video, $"📹{video.Title}")); break; case AudioMessage audioMessage: attachments.Add(new VoiceAttachment(audioMessage.LinkOgg.ToString(), audioMessage, fileName: null, duration: audioMessage.Duration)); break; case Audio audio: { var audioName = $"{audio.Title.Replace('+', ' ')} - {audio.Artist.Replace('+', ' ')}"; attachments.Add(new LinkAttachment( $"https://vk.com/audio?q={HttpUtility.UrlEncode(audioName)}", audio, $"🎵{audioName}")); break; } case Document doc when doc.Type == DocumentTypeEnum.Gif: attachments.Add(new AnimationAttachment(doc.Uri, doc, doc.Title, fileSize: doc.Size ?? 0)); break; case Document doc when doc.Type == DocumentTypeEnum.Video: attachments.Add(new VideoAttachment(doc.Uri, doc, doc.Title, fileSize: doc.Size ?? 0)); break; case Document doc: attachments.Add(new FileAttachment(doc.Uri, doc, doc.Title, fileSize: doc.Size)); break; case Link link: attachments.Add(new LinkAttachment(link.Uri.ToString(), link, $"🔗{link.Title}")); break; case Market market: attachments.Add(new LinkAttachment( $"https://vk.com/market{market.OwnerId}?w=product{market.OwnerId}_{market.Id}", market)); break; case MarketAlbum marketAlbum: attachments.Add(new LinkAttachment( $"https://vk.com/market{marketAlbum.OwnerId}?section=album_{marketAlbum.Id}", marketAlbum)); break; case Wall wall: attachments.Add(new LinkAttachment($"https://vk.com/wall{wall.OwnerId}_{wall.Id}", wall)); break; case Sticker sticker: { var maxSticker = sticker.Images.MaxBy(s => s.Width).First(); attachments.Add(new StickerAttachment(maxSticker.Url.ToString(), sticker)); break; } case Gift gift: { var giftUri = gift.Thumb256 ?? gift.Thumb96 ?? gift.Thumb48; attachments.Add(new PhotoAttachment(giftUri.ToString(), gift, "<gift>")); break; } } } if (msg.Geo != null) { attachments.Add(new PlaceAttachment(msg.Geo.Coordinates.Latitude, msg.Geo.Coordinates.Longitude, msg.Geo.Place.Title, msg.Geo.Place.Address)); } return(attachments); }