Skip to content

Commit

Permalink
Add Proxyability
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny Sotzny committed Sep 24, 2015
1 parent 8f0b6ac commit 2ffb63f
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions NetTelegramBotApi/TelegramBot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using NetTelegramBotApi.Requests;
Expand All @@ -10,6 +11,8 @@ namespace NetTelegramBotApi
{
public class TelegramBot
{
private readonly string _proxyUrl;

public static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings
{
ContractResolver = new Util.JsonLowerCaseUnderscoreContractResolver()
Expand All @@ -23,19 +26,26 @@ static TelegramBot()
JsonSettings.Converters.Add(new UnixDateTimeConverter());
}

public TelegramBot(string accessToken)
/// <summary>
/// Initialisiert eine neue Instanz des <see cref="TelegramBot"/>
/// </summary>
/// <param name="accessToken"></param>
/// <param name="proxyUrl">optional: URL like </param>
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<T> MakeRequestAsync<T>(RequestBase<T> request)
{
using (var client = new HttpClient())
using (var client = new HttpClient(getHttpDefaultHandler()))
{
client.BaseAddress = baseAddress;
using (var httpMessage = new HttpRequestMessage(HttpMethod.Get, request.MethodName))
Expand Down Expand Up @@ -64,6 +74,20 @@ public async Task<T> MakeRequestAsync<T>(RequestBase<T> request)
}
}

private HttpClientHandler getHttpDefaultHandler()
{
if (_proxyUrl != null)
{
return new HttpClientHandler
{
Proxy = new WebProxy(_proxyUrl, true),
UseProxy = true
};
}

return null;
}

/// <summary>
/// Use this method to deserialize <see cref="Update">Update</see> object, sent to <see cref="SetWebhook">your webhook</see> by Telegram server.
/// </summary>
Expand Down

0 comments on commit 2ffb63f

Please sign in to comment.