Exemple #1
0
        private static async Task <long> SaveUploadedFileAsDoc(string fileDesc)
        {
            var url =
                "https://api.vk.com/method/docs.save" +
                "?v=5.124" +
                "&file=" + fileDesc +
                "&access_token=" + AuthorizationManager.Token;

            using var client = new ProxiedWebClient();
            var saveDocResponse = await HttpHelpers.RetryIfEmptyResponse <JsonDto <SaveDocResponseDto> >(
                () => client.GetAsync(new Uri(url)), e => e?.response != null);

            return(saveDocResponse.response.audio_message !.id);
        }
        public static async Task <LongPollingInitResponseDto> GetLongPollServer(CancellationToken cancellationToken)
        {
            var url =
                "https://api.vk.com/method/messages.getLongPollServer" +
                "?v=5.124" +
                "&lp_version=3" +
                "&access_token=" + AuthorizationManager.Token;

            using var client = new ProxiedWebClient();
            var json = await HttpHelpers.RetryIfEmptyResponse <JsonDto <LongPollingInitResponseDto> >(
                () => client.GetAsync(new Uri(url), cancellationToken), e => e?.response != null);

            return(json.response);
        }
Exemple #3
0
        private static async Task <Uri> GetUploadLink(string type)
        {
            var url =
                "https://api.vk.com/method/docs.getUploadServer" +
                "?v=5.124" +
                "&type=" + type +
                "&access_token=" + AuthorizationManager.Token;

            using var client = new ProxiedWebClient();
            var uploadLinkResponse = await HttpHelpers.RetryIfEmptyResponse <JsonDto <UploadLinkResponseDto> >(
                () => client.GetAsync(new Uri(url)), e => e?.response != null);

            return(uploadLinkResponse.response.upload_url);
        }
        // ReSharper disable once UnusedMember.Global
        public static async Task SendAsync(string text)
        {
            try
            {
                const string url = "https://vkmessenger.azurewebsites.net/api/crash";

                using var client = new ProxiedWebClient();
                await client.PostAsync(new Uri(url), text).ConfigureAwait(false);
            }
            catch
            {
                // ignored
            }
        }
        private static async Task <MessagesResponseDto> GetMessagesJsonByIds(IReadOnlyCollection <int> messagesIds)
        {
            var url =
                "https://api.vk.com/method/messages.getById" +
                "?v=5.124" +
                "&extended=1" +
                "&message_ids=" + messagesIds.Aggregate(string.Empty, (seed, item) => seed + "," + item).Substring(1) +
                "&access_token=" + AuthorizationManager.Token;

            using var client = new ProxiedWebClient();
            var json = await HttpHelpers.RetryIfEmptyResponse <JsonDto <MessagesResponseDto> >(
                () => client.GetAsync(new Uri(url)), e => e?.response != null);

            return(json.response);
        }
        public static async Task <LongPollingUpdatesJsonDto> SendLongRequest(string server, string key, int ts, CancellationToken cancellationToken)
        {
            using var client = new ProxiedWebClient();

            var url = "https://" + server +
                      "?act=a_check" +
                      "&key=" + key +
                      "&ts=" + ts +
                      "&wait=" + LongPoolingWaitTime +
                      "&version=3";

            var json = await HttpHelpers.RetryIfEmptyResponse <LongPollingUpdatesJsonDto>(
                () => client.GetAsync(new Uri(url), cancellationToken), e => e != null);

            return(json);
        }
        private static async Task <MessagesResponseDto> GetMessagesJson(int dialogId, int?offset)
        {
            var url =
                "https://api.vk.com/method/messages.getHistory" +
                "?v=5.124" +
                "&extended=1" +
                "&offset=" + (offset ?? 0) +
                "&peer_id=" + dialogId +
                "&access_token=" + AuthorizationManager.Token;

            using var client = new ProxiedWebClient();
            var json = await HttpHelpers.RetryIfEmptyResponse <JsonDto <MessagesResponseDto> >(
                () => client.GetAsync(new Uri(url)), e => e?.response != null);

            return(json.response);
        }
Exemple #8
0
        private static async Task <string> UploadFileUsingLink(Uri link, string filePath)
        {
            var fileData = await File.ReadAllBytesAsync(filePath).ConfigureAwait(false);

            var fileName    = Path.GetFileName(filePath);
            var fileExt     = Path.GetExtension(filePath);
            var contentType = fileExt switch
            {
                ".3gp" => "video/3gpp",
                _ => throw new NotSupportedException("Content type is not supported")
            };

            using var client = new ProxiedWebClient();
            var deserialized = await HttpHelpers.RetryIfEmptyResponse <UploadFileResponseDto>(
                () => client.UploadFileAsync(fileData, fileName, contentType, link), e => e?.file != null);

            return(deserialized.file);
        }
        public static async Task <int> Send(int dialogId, string?text, string?voiceMessagePath)
        {
            try
            {
                Logger.Info($"Sending message in dialog {dialogId}");

                var url =
                    "https://api.vk.com/method/messages.send" +
                    "?v=5.124" +
                    "&peer_id=" + dialogId +
                    "&access_token=" + AuthorizationManager.Token;

                if (text != null)
                {
                    url += "&message=" + text;
                    url += "&random_id=" + BitConverter.ToInt32(Md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(text)), 0);
                }
                else if (voiceMessagePath != null)
                {
                    var id = await DocumentsClient.UploadAudioFile(voiceMessagePath);

                    url += "&attachment=audio_message" + AuthorizationManager.UserId + "_" + id;
                    url += "&random_id=" + BitConverter.ToInt32(Md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(voiceMessagePath)), 0);
                }
                else
                {
                    throw new ArgumentNullException(nameof(voiceMessagePath));
                }

                using var client = new ProxiedWebClient();
                var result = await HttpHelpers.RetryIfEmptyResponse <JsonDto <int> >(
                    () => client.GetAsync(new Uri(url)),
                    e => e?.response != null);

                return(result.response);
            }
            catch (Exception e)
            {
                Logger.Error(e);
                throw;
            }
        }
Exemple #10
0
        public static async Task <string> DownloadDocumentToTempFile(Uri source)
        {
            try
            {
                var fileName     = source.Segments.Last();
                var tempFileName = Path.Combine(Path.GetTempPath(), fileName);
                var client       = new ProxiedWebClient();
                if (!File.Exists(tempFileName))
                {
                    await client.DownloadFileAsync(source, tempFileName).ConfigureAwait(false);
                }

                return(tempFileName);
            }
            catch (Exception e)
            {
                Logger.Error(e);
                throw;
            }
        }
Exemple #11
0
        public static async Task <Uri> GetPhoto(string token, int userId)
        {
            try
            {
                var url =
                    "https://api.vk.com/method/users.get" +
                    "?user_ids=" + userId +
                    "&v=5.124" +
                    "&fields=photo_50" +
                    "&access_token=" + token;

                using var client = new ProxiedWebClient();
                var json = await HttpHelpers.RetryIfEmptyResponse <JsonDto <UserDto[]> >(
                    () => client.GetAsync(new Uri(url)), e => e?.response != null);

                return(json.response.First().photo_50);
            }
            catch (Exception e)
            {
                Logger.Error(e);
                throw;
            }
        }
        public static async Task DeleteMessage(int messageId)
        {
            try
            {
                Logger.Info($"Removing message {messageId}");

                var url =
                    "https://api.vk.com/method/messages.delete" +
                    "?v=5.124" +
                    "&message_ids=" + messageId +
                    "&access_token=" + AuthorizationManager.Token +
                    "&delete_for_all=1";

                using var client = new ProxiedWebClient();
                await HttpHelpers.RetryIfEmptyResponse <JsonDto <object> >(
                    () => client.GetAsync(new Uri(url)),
                    e => e?.response != null || e?.error?.error_code == 924); // Already deleted
            }
            catch (Exception e)
            {
                Logger.Error(e);
                throw;
            }
        }