private void AddFileDataContent(MultipartFormDataContent form, string fullFileName, [CallerMemberName] string callerName = "") { fullFileName.NullInspect(nameof(fullFileName)); var fileType = TelegramMethodAttribute.GetFileTypeValue(this.GetType(), callerName); Debug.Assert(!string.IsNullOrEmpty(fileType), $"Use {nameof(TelegramMethodAttribute)} to avoid error."); if (!Path.HasExtension(fullFileName)) { form.Add(new StringContent(fullFileName, Encoding.UTF8), fileType); return; } try { //can't use using here, because these streams are necessary open var fileStream = new FileStream(fullFileName, FileMode.Open, FileAccess.Read); form.Add(new StreamContent(fileStream), fileType, Path.GetFileName(fullFileName)); } catch (Exception ex) { throw new TelegramMethodsException($"Something wrong with the file {fullFileName}", ex); } }
private async Task <string> UploadMultipartFormDataContent(HttpContent content, [CallerMemberName] string callerName = "") { var telegramMethodName = TelegramMethodAttribute.GetMethodNameValue(this.GetType(), callerName); Debug.Assert(!string.IsNullOrEmpty(telegramMethodName), $"Use {nameof(TelegramMethodAttribute)} to avoid error."); try { using (var httpClient = new HttpClient()) { var response = await httpClient.PostAsync(TelegramBotUrl + Token + "/" + telegramMethodName, content).ConfigureAwait(false); return(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); } } catch (HttpRequestException ex) { throw new TelegramMethodsException( "Bad http request, wrong parameters or something. See inner exception.", ex); } }