diff --git a/NetTelegramBotApi/NetTelegramBotApi.csproj b/NetTelegramBotApi/NetTelegramBotApi.csproj index 82238e9..a78982c 100644 --- a/NetTelegramBotApi/NetTelegramBotApi.csproj +++ b/NetTelegramBotApi/NetTelegramBotApi.csproj @@ -31,7 +31,7 @@ - ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll + ..\..\..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll True diff --git a/NetTelegramBotApi/TelegramBot.cs b/NetTelegramBotApi/TelegramBot.cs index f4afd37..49a1e19 100644 --- a/NetTelegramBotApi/TelegramBot.cs +++ b/NetTelegramBotApi/TelegramBot.cs @@ -1,4 +1,5 @@ using System; +using System.Net; using System.Net.Http; using System.Threading.Tasks; using NetTelegramBotApi.Requests; @@ -10,6 +11,8 @@ namespace NetTelegramBotApi { public class TelegramBot { + private readonly string _proxyUrl; + public static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings { ContractResolver = new Util.JsonLowerCaseUnderscoreContractResolver() @@ -23,19 +26,26 @@ static TelegramBot() JsonSettings.Converters.Add(new UnixDateTimeConverter()); } - public TelegramBot(string accessToken) + /// + /// Initialisiert eine neue Instanz des + /// + /// + /// optional: URL like + public TelegramBot(string accessToken, string proxyUrl = null) { + _proxyUrl = proxyUrl; if (string.IsNullOrWhiteSpace(accessToken)) { throw new ArgumentNullException("accessToken"); } + this.baseAddress = new Uri("https://api.telegram.org/bot" + accessToken + "/"); } public async Task MakeRequestAsync(RequestBase request) { - using (var client = new HttpClient()) + using (var client = new HttpClient(getHttpDefaultHandler())) { client.BaseAddress = baseAddress; using (var httpMessage = new HttpRequestMessage(HttpMethod.Get, request.MethodName)) @@ -64,6 +74,20 @@ public async Task MakeRequestAsync(RequestBase request) } } + private HttpClientHandler getHttpDefaultHandler() + { + if (_proxyUrl != null) + { + return new HttpClientHandler + { + Proxy = new WebProxy(_proxyUrl, true), + UseProxy = true + }; + } + + return null; + } + /// /// Use this method to deserialize Update object, sent to your webhook by Telegram server. ///