Example #1
0
        public static VkWallEntry FromJson(JToken json)
        {
            if (json == null)
            {
                throw new ArgumentException("Json can not be null.");
            }

            var result = new VkWallEntry();

            result.Id = json["id"].Value <double>();

            if (json["text"] != null)
            {
                result.Text = WebUtility.HtmlDecode(json["text"].Value <string>());
                result.Text = result.Text.Replace("<br>", Environment.NewLine);
            }

            if (json["from_id"] != null)
            {
                result.SourceId = json["from_id"].Value <long>();
            }

            if (json["date"] != null)
            {
                result.Date = DateTimeExtensions.UnixTimeStampToDateTime(json["date"].Value <long>());
            }

            if (json["attachments"] != null)
            {
                result.Attachments = VkAttachment.FromJson(json["attachments"]);
            }

            if (json["copy_history"] != null)
            {
                result.CopyHistory = new List <VkWallEntry>();

                foreach (var p in json["copy_history"])
                {
                    try
                    {
                        var post = VkWallEntry.FromJson(p);
                        if (post != null)
                        {
                            result.CopyHistory.Add(post);
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// <para>Send message</para>
        /// <para>See also: <seealso cref="http://vk.com/dev/messages.send"/></para>
        /// </summary>
        /// <returns>Id of new message</returns>
        public async Task <long> Send(long userId, long chatId, string message, VkAttachment attachment = null)
        {
            if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired)
            {
                throw new Exception("Access token is not valid.");
            }

            var parametres = new Dictionary <string, string>();

            if (userId != 0)
            {
                parametres.Add("user_id", userId.ToString());
            }
            else
            {
                if (chatId != 0)
                {
                    parametres.Add("chat_id", chatId.ToString());
                }
                else
                {
                    throw new Exception("User id or chat id must be specified.");
                }
            }

            if (!string.IsNullOrEmpty(message))
            {
                parametres.Add("message", message);
            }

            if (attachment != null)
            {
                string type = null;
                if (attachment is VkAudioAttachment)
                {
                    type = "audio";
                }
                else if (attachment is VkPhotoAttachment)
                {
                    type = "photo";
                }
                if (type != null)
                {
                    parametres.Add("attachment", string.Format("{0}{1}_{2}", type, attachment.OwnerId, attachment.Id));
                }
            }

            _vkontakte.SignMethod(parametres);

            var response = await new VkRequest(new Uri(VkConst.MethodBase + "messages.send"), parametres).Execute();

            VkErrorProcessor.ProcessError(response);

            return(response["response"].Value <long>());
        }
Example #3
0
        public static VkMessage FromJson(JToken json, string apiVersion = null)
        {
            if (json == null)
            {
                throw new Exception("Json can't be null");
            }

            var result = new VkMessage();

            if (json["id"] != null)
            {
                result.Id = (long)json["id"];
            }

            if (json["user_id"] != null)
            {
                result.UserId = (long)json["user_id"];
            }
            result.Date = DateTimeExtensions.UnixTimeStampToDateTime((long)json["date"]);

            if (json["read_state"] != null)
            {
                result.IsRead = (int)json["read_state"] == 1;
            }

            if (json["out"] != null)
            {
                result.IsOut = (int)json["out"] == 1;
            }

            result.Title = (string)json["title"];
            result.Body  = (string)json["body"];

            if (json["deleted"] != null)
            {
                result.IsDeleted = (int)json["deleted"] == 1;
            }

            if (json["attachments"] != null)
            {
                result.Attachments = VkAttachment.FromJson(json["attachments"]);
            }

            if (json["geo"] != null)
            {
                result.Geo = VkGeo.FromJson(json["geo"]);
            }

            if (json["fwd_messages"] != null)
            {
                result.ForwardMessages = new List <VkMessage>();
                foreach (var fwdMessage in json["fwd_messages"])
                {
                    var message = VkMessage.FromJson(fwdMessage);
                    result.ForwardMessages.Add(message);
                }
            }

            if (json["emoji"] != null)
            {
                result.ContainsEmoji = (int)json["emoji"] == 1;
            }

            if (json["chat_id"] != null)
            {
                result.ChatId = (long)json["chat_id"];
            }

            if (json["chat_active"] != null)
            {
                result.ChatUsers = json["chat_active"].Select(t => t.Value <long>()).ToList();
            }

            if (json["users_count"] != null)
            {
                result.UsersCount = (int)json["users_count"];
            }

            if (json["admin_id"] != null)
            {
                result.AdminId = (long)json["admin_id"];
            }

            if (json["action"] != null)
            {
                result.Action = (string)json["action"];
            }

            if (json["action_text"] != null)
            {
                result.ActionText = (string)json["action_text"];
            }

            if (json["photo_50"] != null)
            {
                result.Photo50 = (string)json["photo_50"];
            }

            if (json["photo_100"] != null)
            {
                result.Photo100 = (string)json["photo_100"];
            }

            if (json["photo_200"] != null)
            {
                result.Photo200 = (string)json["photo_200"];
            }

            return(result);
        }
        public static VkMessage FromJson(JToken json, string apiVersion = null)
        {
            if (json == null)
            {
                throw new Exception("Json can't be null");
            }

            var result = new VkMessage();

            result.Id     = (long)json["id"];
            result.UserId = (long)json["user_id"];
            result.Date   = DateTimeExtensions.UnixTimeStampToDateTime((long)json["date"]);

            if (json["read_state"] != null)
            {
                result.IsRead = (int)json["read_state"] == 1;
            }

            if (json["out"] != null)
            {
                result.IsOut = (int)json["out"] == 1;
            }

            result.Title = (string)json["title"];
            result.Body  = (string)json["body"];

            if (json["deleted"] != null)
            {
                result.IsDeleted = (int)json["deleted"] == 1;
            }

            if (json["attachments"] != null)
            {
                result.Attachments = VkAttachment.FromJson(json["attachments"], apiVersion);
            }

            //TODO forward messages

            if (json["emoji"] != null)
            {
                result.ContainsEmoji = (int)json["emoji"] == 1;
            }

            if (json["chat_id"] != null)
            {
                result.ChatId = (long)json["chat_id"];
            }

            if (json["chat_active"] != null)
            {
                result.ChatUsers = json["chat_active"].Select(t => t.Value <long>()).ToList();
            }

            if (json["users_count"] != null)
            {
                result.UsersCount = (int)json["users_count"];
            }

            if (json["admin_id"] != null)
            {
                result.AdminId = (long)json["admin_id"];
            }

            return(result);
        }