From bbf90deccc8e0275129ff0ba8b9c9ffb29e77a3c Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Wed, 20 Mar 2019 11:14:38 +0700 Subject: [PATCH] - Added easy to use Proxy functionality into constructor - improved DeviceSession - added IsGroup property to DeviceSession --- TelegramBotBase/Base/MessageClient.cs | 39 ++++++++++++++ TelegramBotBase/BotBase.cs | 60 ++++++++++++++++++++-- TelegramBotBase/Properties/AssemblyInfo.cs | 4 +- TelegramBotBase/Sessions/DeviceSession.cs | 45 +++++++++++++--- 4 files changed, 136 insertions(+), 12 deletions(-) diff --git a/TelegramBotBase/Base/MessageClient.cs b/TelegramBotBase/Base/MessageClient.cs index 91af2c8..69e7974 100644 --- a/TelegramBotBase/Base/MessageClient.cs +++ b/TelegramBotBase/Base/MessageClient.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.ComponentModel; using System.Linq; +using System.Net; +using System.Net.Http; using System.Text; using System.Threading.Tasks; @@ -32,6 +34,43 @@ namespace TelegramBotBase.Base Prepare(); } + public MessageClient(String APIKey, HttpClient proxy) + { + this.APIKey = APIKey; + this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, proxy); + + + Prepare(); + } + + public MessageClient(String APIKey, Uri proxyUrl) + { + this.APIKey = APIKey; + + var proxy = new WebProxy(proxyUrl); + + this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, proxy); + + Prepare(); + } + + /// + /// Initializes the client with a proxy + /// + /// + /// i.e. 127.0.0.1 + /// i.e. 10000 + public MessageClient(String APIKey, String proxyHost, int proxyPort) + { + this.APIKey = APIKey; + + var proxy = new WebProxy(proxyHost, proxyPort); + + this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, proxy); + + Prepare(); + } + public MessageClient(String APIKey, Telegram.Bot.TelegramBotClient Client) { this.APIKey = APIKey; diff --git a/TelegramBotBase/BotBase.cs b/TelegramBotBase/BotBase.cs index 7a941e4..a1e80fb 100644 --- a/TelegramBotBase/BotBase.cs +++ b/TelegramBotBase/BotBase.cs @@ -78,6 +78,60 @@ namespace TelegramBotBase this.Sessions.Client = this.Client; } + /// + /// Simple start of your Bot with the APIKey and a proxyAdress + /// + /// + /// i.e. https://127.0.0.1:10000 + public BotBase(String apiKey, System.Net.Http.HttpClient proxy) + { + this.APIKey = apiKey; + + this.Client = new Base.MessageClient(this.APIKey, proxy); + + this.SystemCalls = new List(); + + this.Sessions = new SessionBase(); + this.Sessions.Client = this.Client; + } + + /// + /// Simple start of your Bot with the APIKey and a proxyAdress + /// + /// + /// i.e. https://127.0.0.1:10000 + public BotBase(String apiKey, String proxyBaseAddress) + { + this.APIKey = apiKey; + + var url = new Uri(proxyBaseAddress); + + this.Client = new Base.MessageClient(this.APIKey, url); + + this.SystemCalls = new List(); + + this.Sessions = new SessionBase(); + this.Sessions.Client = this.Client; + } + + /// + /// Simple start of your Bot with the APIKey and a proxyAdress + /// + /// + /// i.e. 127.0.0.1 + /// i.e. 10000 + public BotBase(String apiKey, String proxyHost, int proxyPort) + { + this.APIKey = apiKey; + + this.Client = new Base.MessageClient(this.APIKey, proxyHost, proxyPort); + + this.SystemCalls = new List(); + + this.Sessions = new SessionBase(); + this.Sessions.Client = this.Client; + } + /// /// Start your Bot /// @@ -154,13 +208,13 @@ namespace TelegramBotBase { ds = await this.Sessions.StartSession(e.DeviceId); - ds.LastMessage = e.MessageId; + ds.LastMessage = e.Message; OnSessionBegins(new SessionBeginResult(e.DeviceId, ds)); } ds.LastAction = DateTime.Now; - ds.LastMessage = e.MessageId; + ds.LastMessage = e.Message; //Is this a systemcall ? if (e.IsSystemCall && this.SystemCalls.Contains(e.SystemCommand)) @@ -223,7 +277,7 @@ namespace TelegramBotBase ds.LastAction = DateTime.Now; - ds.LastMessage = e.MessageId; + ds.LastMessage = e.Message; FormBase activeForm = null; diff --git a/TelegramBotBase/Properties/AssemblyInfo.cs b/TelegramBotBase/Properties/AssemblyInfo.cs index 23d83d6..454c5df 100644 --- a/TelegramBotBase/Properties/AssemblyInfo.cs +++ b/TelegramBotBase/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern // übernehmen, indem Sie "*" eingeben: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.3.0.0")] -[assembly: AssemblyFileVersion("1.3.0.0")] +[assembly: AssemblyVersion("1.3.1.0")] +[assembly: AssemblyFileVersion("1.3.1.0")] diff --git a/TelegramBotBase/Sessions/DeviceSession.cs b/TelegramBotBase/Sessions/DeviceSession.cs index 308191d..a72c1cb 100644 --- a/TelegramBotBase/Sessions/DeviceSession.cs +++ b/TelegramBotBase/Sessions/DeviceSession.cs @@ -27,11 +27,31 @@ namespace TelegramBotBase.Sessions /// public long DeviceId { get; set; } + /// + /// When did any last action happend (message received or button clicked) + /// public DateTime LastAction { get; set; } + /// + /// Returns the form where the user/group is at the moment. + /// public FormBase ActiveForm { get; set; } - public int LastMessage { get; set; } + /// + /// Returns the ID of the last received message. + /// + public int LastMessageId + { + get + { + return this.LastMessage?.MessageId ?? -1; + } + } + + /// + /// Returns the last received message. + /// + public Message LastMessage { get; set; } private MessageClient Client { @@ -41,13 +61,24 @@ namespace TelegramBotBase.Sessions } } + /// + /// Returns if the messages is posted within a group. + /// + public bool IsGroup + { + get + { + return this.LastMessage != null && this.LastMessage.Chat.Id != this.LastMessage.From.Id; + } + } + public EventHandlerList __Events = new EventHandlerList(); private static object __evMessageSent = new object(); public DeviceSession() { - this.LastMessage = 0; + } public DeviceSession(long DeviceId) @@ -87,7 +118,7 @@ namespace TelegramBotBase.Sessions } catch { - + } @@ -119,7 +150,7 @@ namespace TelegramBotBase.Sessions { m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); } - catch (Telegram.Bot.Exceptions.ApiRequestException ex) + catch (ApiRequestException ex) { return null; } @@ -153,7 +184,7 @@ namespace TelegramBotBase.Sessions { m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); } - catch (Telegram.Bot.Exceptions.ApiRequestException ex) + catch (ApiRequestException ex) { return null; } @@ -192,7 +223,7 @@ namespace TelegramBotBase.Sessions { m = await this.Client.TelegramClient.SendPhotoAsync(this.DeviceId, file, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification); } - catch (Telegram.Bot.Exceptions.ApiRequestException ex) + catch (ApiRequestException ex) { return null; } @@ -316,7 +347,7 @@ namespace TelegramBotBase.Sessions return true; } - catch(ApiRequestException ex) + catch (ApiRequestException ex) { }