/// <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>()); }
public async Task <VkItemsResponse <VkGroup> > Get(long userId, string fields, string filter, int count, int offset, bool extended = true) { if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired) { throw new Exception("Access token is not valid."); } var parameters = new Dictionary <string, string>(); if (userId != 0) { parameters.Add("user_id", userId.ToString()); } if (!string.IsNullOrWhiteSpace(fields)) { parameters.Add("fields", fields); } if (!string.IsNullOrWhiteSpace(filter)) { parameters.Add("filter", filter); } if (count > 0) { parameters.Add("count", count.ToString()); } if (offset > 0) { parameters.Add("offset", offset.ToString()); } if (extended) { parameters.Add("extended", "1"); } _vkontakte.SignMethod(parameters); var response = await new VkRequest(new Uri(VkConst.MethodBase + "groups.get"), parameters).Execute(); VkErrorProcessor.ProcessError(response); if (response.SelectToken("response.items") != null) { return(new VkItemsResponse <VkGroup>(response["response"]["items"].Select(VkGroup.FromJson).ToList(), (int)response["response"]["count"])); } return(VkItemsResponse <VkGroup> .Empty); }
public async Task Start(Action <List <VkLongPollMessage> > onMessage = 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>(); _vkontakte.SignMethod(parametres); var response = await new VkRequest(new Uri(VkConst.MethodBase + "messages.getLongPollServer"), parametres).Execute(); VkErrorProcessor.ProcessError(response); if (response["response"] != null) { var key = (string)response["response"]["key"]; var server = (string)response["response"]["server"]; var ts = (string)response["response"]["ts"]; Debug.WriteLine("Long poll service started: " + response); await Connect(key, server, ts, onMessage); } }
public async Task <VkItemsResponse <VkProfile> > Get() { if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired) { throw new Exception("Access token is not valid."); } var parameters = new Dictionary <string, string>(); _vkontakte.SignMethod(parameters); var response = await new VkRequest(new Uri(VkConst.MethodBase + "subscriptions.get"), parameters).Execute(); VkErrorProcessor.ProcessError(response); if (response.SelectToken("response.items") != null) { return(new VkItemsResponse <VkProfile>((from id in response["response"]["items"] select new VkProfile() { Id = (long)id }).ToList(), response["response"]["count"].Value <int>())); } return(VkItemsResponse <VkProfile> .Empty); }
public async Task <string> GetWallUploadServer(long albumId = 0, long groupId = 0) { if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired) { throw new Exception("Access token is not valid."); } var parameters = new Dictionary <string, string>(); if (albumId > 0) { parameters.Add("aid", albumId.ToString()); } if (groupId > 0) { parameters.Add("group_id", groupId.ToString()); } _vkontakte.SignMethod(parameters); var response = await new VkRequest(new Uri(VkConst.MethodBase + "photos.getWallUploadServer"), parameters).Execute(); VkErrorProcessor.ProcessError(response); if (response["response"]["upload_url"] != null) { return(response["response"]["upload_url"].Value <string>()); } return(null); }
public async Task <long> Post(long ownerId = 0, string message = null, IEnumerable <VkAttachment> attachments = null, bool fromGroup = false, bool signed = false) { if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired) { throw new Exception("Access token is not valid."); } var parameters = new Dictionary <string, string>(); if (ownerId != 0) { parameters.Add("owner_id", ownerId.ToString()); } if (!string.IsNullOrEmpty(message)) { parameters.Add("message", message); } if (attachments != null) { parameters.Add("attachments", string.Join(",", attachments)); } if (fromGroup) { parameters.Add("from_group", "1"); } if (signed) { parameters.Add("signed", "1"); } _vkontakte.SignMethod(parameters); var response = await new VkRequest(new Uri(VkConst.MethodBase + "wall.post"), parameters).Execute(); VkErrorProcessor.ProcessError(response); if (response["response"] != null) { return((long)response["response"]["post_id"]); } return(0); }
public async Task <VkItemsResponse <VkWallEntry> > GetPosts(int count = 0, int offset = 0, bool extended = true) { if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired) { throw new Exception("Access token is not valid."); } var parameters = new Dictionary <string, string>(); if (count > 0) { parameters.Add("count", count.ToString()); } if (offset > 0) { parameters.Add("offset", offset.ToString()); } if (extended) { parameters.Add("extended", "1"); } _vkontakte.SignMethod(parameters); var response = await new VkRequest(new Uri(VkConst.MethodBase + "fave.getPosts"), parameters).Execute(); VkErrorProcessor.ProcessError(response); if (response.SelectToken("response.items") != null) { var result = new VkItemsResponse <VkWallEntry>((from n in response["response"]["items"] select VkWallEntry.FromJson(n)).ToList()); if (response["response"]["profiles"] != null) { var users = (from n in response["response"]["profiles"] select VkProfile.FromJson(n)).ToList(); foreach (var entry in result.Items) { entry.Author = users.FirstOrDefault(u => u.Id == entry.SourceId); } } if (response["response"]["groups"] != null) { var groups = (from n in response["response"]["groups"] select VkGroup.FromJson(n)).ToList(); foreach (var entry in result.Items.Where(e => e.Author == null)) { entry.Author = groups.FirstOrDefault(g => g.Id == Math.Abs(entry.SourceId)); } } return(result); } return(null); }
//this method used by vk to mark access token as "true" (obtained from official app) public async Task GetBaseData(Dictionary <string, string> parameters) { if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired) { throw new Exception("Access token is not valid."); } _vkontakte.SignMethod(parameters); var response = await new VkRequest(new Uri(VkConst.MethodBase + "execute.getBaseData"), parameters).Execute(); }
/// <summary> /// Tracks user in stats /// </summary> /// <returns></returns> public async Task <bool> TrackVisitor() { if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired) { throw new Exception("Access token is not valid."); } var parameters = new Dictionary <string, string>(); _vkontakte.SignMethod(parameters); var response = await new VkRequest(new Uri(VkConst.MethodBase + "stats.trackVisitor"), parameters).Execute(); if (response != null && response["response"] != null) { return(true); } return(false); }
public async Task <bool> RegisterDevice(string token, string deviceModel = null, string systemVersion = null, bool noText = false, string subscribe = 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>(); parametres.Add("token", token); if (!string.IsNullOrEmpty(deviceModel)) { parametres.Add("device_model", deviceModel); } if (!string.IsNullOrEmpty(systemVersion)) { parametres.Add("system_version", systemVersion); } if (noText) { parametres.Add("no_text", "1"); } _vkontakte.SignMethod(parametres); var response = await new VkRequest(new Uri(VkConst.MethodBase + "account.registerDevice"), parametres).Execute(); VkErrorProcessor.ProcessError(response); if (response["response"] != null) { return(response["response"].Value <int>() == 1); } return(false); }
public async Task <VkItemsResponse <VkProfile> > Get(IEnumerable <string> userIds, string fields = null, string nameCase = null) { if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired) { throw new Exception("Access token is not valid."); } var parameters = new Dictionary <string, string>(); if (userIds != null) { parameters.Add("user_ids", string.Join(",", userIds)); } if (!string.IsNullOrWhiteSpace(fields)) { parameters.Add("fields", fields); } if (!string.IsNullOrWhiteSpace(nameCase)) { parameters.Add("name_case", nameCase); } _vkontakte.SignMethod(parameters); var response = await new VkRequest(new Uri(VkConst.MethodBase + "users.get"), parameters).Execute(); VkErrorProcessor.ProcessError(response); if (response.SelectToken("response") != null) { return(new VkItemsResponse <VkProfile>((from u in response["response"] select VkProfile.FromJson(u)).ToList())); } return(VkItemsResponse <VkProfile> .Empty); }
/// <summary> /// Get audios of user or society. /// See also <see cref="http://vk.com/dev/audio.get"/> /// </summary> /// <param name="ownerId">Owner id. For society must be negative.</param> /// <param name="albumId">Album id</param> /// <param name="count"></param> /// <param name="offset"></param> /// <returns></returns> public async Task <VkItemsResponse <VkAudio> > Get(long ownerId, long albumId = 0, int count = 0, int offset = 0) { if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired) { throw new VkInvalidTokenException(); } var parameters = new Dictionary <string, string>(); if (ownerId != 0) { parameters.Add("owner_id", ownerId.ToString(CultureInfo.InvariantCulture)); } if (albumId != 0) { parameters.Add("album_id", albumId.ToString(CultureInfo.InvariantCulture)); } if (count > 0) { parameters.Add("count", count.ToString(CultureInfo.InvariantCulture)); } if (offset > 0) { parameters.Add("offset", offset.ToString(CultureInfo.InvariantCulture)); } _vkontakte.SignMethod(parameters); var response = await new VkRequest(new Uri(VkConst.MethodBase + "audio.get"), parameters).Execute(); VkErrorProcessor.ProcessError(response); if (response.SelectToken("response.items") != null) { return(new VkItemsResponse <VkAudio>(response["response"]["items"].Select(VkAudio.FromJson).ToList(), (int)response["response"]["count"])); } return(VkItemsResponse <VkAudio> .Empty); }
public async Task <VkItemsResponse <VkProfile> > Get(long userId, string fields, string nameCase, int count, int offset, FriendsOrder order = FriendsOrder.ByName) { if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired) { throw new Exception("Access token is not valid."); } var parameters = new Dictionary <string, string>(); if (userId > 0) { parameters.Add("user_id", userId.ToString()); } if (!string.IsNullOrWhiteSpace(fields)) { parameters.Add("fields", fields); } if (!string.IsNullOrWhiteSpace(nameCase)) { parameters.Add("name_case", nameCase); } if (count > 0) { parameters.Add("count", count.ToString()); } if (offset > 0) { parameters.Add("offset", offset.ToString()); } switch (order) { case FriendsOrder.ByName: parameters.Add("order", "name"); break; case FriendsOrder.ByRating: parameters.Add("order", "hints"); break; case FriendsOrder.Random: parameters.Add("order", "random"); break; } _vkontakte.SignMethod(parameters); var response = await new VkRequest(new Uri(VkConst.MethodBase + "friends.get"), parameters).Execute(); VkErrorProcessor.ProcessError(response); if (response.SelectToken("response.items") != null) { return(new VkItemsResponse <VkProfile>(response["response"]["items"].Select(VkProfile.FromJson).ToList(), (int)response["response"]["count"])); } return(VkItemsResponse <VkProfile> .Empty); }
public async Task <VkNewsResponse> Get(string sourceIds = null, string filters = null, int count = 0, string startFrom = null) { if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired) { throw new Exception("Access token is not valid."); } var parameters = new Dictionary <string, string>(); if (!string.IsNullOrEmpty(sourceIds)) { parameters.Add("source_ids", sourceIds); } if (!string.IsNullOrEmpty(filters)) { parameters.Add("filters", filters); } if (count > 0) { parameters.Add("count", count.ToString()); } if (!string.IsNullOrEmpty(startFrom)) { parameters.Add("start_from", startFrom); } _vkontakte.SignMethod(parameters); var response = await new VkRequest(new Uri(VkConst.MethodBase + "newsfeed.get"), parameters).Execute(); VkErrorProcessor.ProcessError(response); if (response.SelectToken("response.items") != null) { var result = new VkNewsResponse((from n in response["response"]["items"] select VkNewsEntry.FromJson(n)).ToList()); if (response["response"]["profiles"] != null) { var users = (from n in response["response"]["profiles"] select VkProfile.FromJson(n)).ToList(); foreach (var entry in result.Items) { entry.Author = users.FirstOrDefault(u => u.Id == entry.SourceId); } } if (response["response"]["groups"] != null) { var groups = (from n in response["response"]["groups"] select VkGroup.FromJson(n)).ToList(); foreach (var entry in result.Items.Where(e => e.Author == null)) { entry.Author = groups.FirstOrDefault(g => g.Id == Math.Abs(entry.SourceId)); } } if (response["response"]["next_from"] != null) { result.NextFrom = response["response"]["next_from"].Value <string>(); } return(result); } return(null); }