diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25a3ef2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/packages +/.vs +/ConnectorUserTelegram/obj +/ConnectorUserTelegram/bin diff --git a/ConnectorUserTelegram.sln b/ConnectorUserTelegram.sln index 4d412cd..312bd00 100644 --- a/ConnectorUserTelegram.sln +++ b/ConnectorUserTelegram.sln @@ -1,31 +1,37 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.136 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectorUserTelegram", "ConnectorUserTelegram\ConnectorUserTelegram.csproj", "{E66A57DD-858A-40E4-8A2F-BEA5129C31F7}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils-IoT", "..\Utils-IoT\Utils-IoT\Utils-IoT.csproj", "{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Release|Any CPU.Build.0 = Release|Any CPU - {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {7C826771-09A9-44C6-BC5F-E7E8BABFD2A3} - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.136 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectorUserTelegram", "ConnectorUserTelegram\ConnectorUserTelegram.csproj", "{E66A57DD-858A-40E4-8A2F-BEA5129C31F7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils-IoT", "..\Utils-IoT\Utils-IoT\Utils-IoT.csproj", "{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "litjson_4.7.1", "..\..\Librarys\litjson\litjson\litjson_4.7.1.csproj", "{91A14CD2-2940-4500-8193-56D37EDDDBAA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Release|Any CPU.Build.0 = Release|Any CPU + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.Build.0 = Release|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7C826771-09A9-44C6-BC5F-E7E8BABFD2A3} + EndGlobalSection +EndGlobal diff --git a/ConnectorUserTelegram/ConnectorUserTelegram.csproj b/ConnectorUserTelegram/ConnectorUserTelegram.csproj index 34ecf6b..6a97cda 100644 --- a/ConnectorUserTelegram/ConnectorUserTelegram.csproj +++ b/ConnectorUserTelegram/ConnectorUserTelegram.csproj @@ -1,120 +1,76 @@ - - - - - Debug - AnyCPU - {E66A57DD-858A-40E4-8A2F-BEA5129C31F7} - Library - Properties - BlubbFish.Utils.IoT.Connector.User - ConnectorUserTelegram - v4.7.1 - 512 - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\..\..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - - - ..\..\..\..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll - - - - ..\..\..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - - ..\..\..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - - - - ..\..\..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - - - ..\..\..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - - - ..\..\..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - - ..\..\..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - - - ..\..\..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - - - ..\..\..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - - - ..\..\..\..\packages\System.Net.Http.4.3.0\lib\net46\System.Net.Http.dll - - - ..\..\..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - - - - ..\..\..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\..\..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll - - - ..\..\..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - - - ..\..\..\..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll - - - ..\..\..\..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - - - - - - - - ..\..\..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - - - ..\..\..\..\packages\Telegram.Bot.13.2.1\lib\netstandard1.1\Telegram.Bot.dll - - - - - - - - - {b870e4d5-6806-4a0b-b233-8907eedc5afc} - Utils-IoT - - - - - - - + + + + + Debug + AnyCPU + {E66A57DD-858A-40E4-8A2F-BEA5129C31F7} + Library + Properties + BlubbFish.Utils.IoT.Connector.User + ConnectorUserTelegram + v4.7.1 + 512 + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + ..\..\..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll + True + + + + + ..\..\..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll + True + + + + + + ..\..\..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll + True + + + + + + + + ..\packages\Telegram.Bot.13.2.1\lib\netstandard1.1\Telegram.Bot.dll + + + + + + + + + {b870e4d5-6806-4a0b-b233-8907eedc5afc} + Utils-IoT + + + + + + + \ No newline at end of file diff --git a/ConnectorUserTelegram/Telegram.cs b/ConnectorUserTelegram/Telegram.cs index 88fc3a7..4a2a556 100644 --- a/ConnectorUserTelegram/Telegram.cs +++ b/ConnectorUserTelegram/Telegram.cs @@ -1,69 +1,73 @@ -using System; -using System.Collections.Generic; -using Telegram.Bot; -using Telegram.Bot.Args; -using Telegram.Bot.Exceptions; -using Telegram.Bot.Types; -using Telegram.Bot.Types.Enums; -using Telegram.Bot.Types.ReplyMarkups; - -namespace BlubbFish.Utils.IoT.Connector.User { - public class Telegram : AUserBackend { - private TelegramBotClient bot; - private ChatId chat; - - public override event TelegramMessage MessageIncomming; - public override event TelegramMessage MessageSending; - - public Telegram(Dictionary settings) : base(settings) { - this.bot = new TelegramBotClient(settings["telegram-key"]); - this.bot.OnMessage += this.Bot_OnMessage; - this.Connect(); - } - - private void Bot_OnMessage(Object sender, MessageEventArgs e) { - this.MessageIncomming?.Invoke(this, new UserMessageEventArgs(e.Message.Text, e.Message.Chat.Id, e.Message.Date)); - } - - private void Connect() { - this.bot.StartReceiving(); - this.chat = new ChatId(this.settings["chatid"]); - } - - public async override void Send(String message) { - try { - Message x = await this.bot.SendTextMessageAsync(this.chat, message, ParseMode.Default, false, false, 0, new ReplyKeyboardRemove()); - this.MessageSending?.Invoke(this, new UserMessageEventArgs(x.Text, x.Chat.Id, x.Date)); - } catch(ApiRequestException e) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(e.Message+" "+e.ErrorCode+" "+e.Parameters); - Console.ForegroundColor = ConsoleColor.White; - } - - } - - public override void Dispose() { - this.bot.StopReceiving(); - this.bot = null; - } - - public async override void Send(String message, String[] buttons) { - try { - KeyboardButton[][] button = new KeyboardButton[(Int32)Math.Ceiling(((Double)buttons.Length)/2)][]; - for(Int32 i = 0; i < buttons.Length; i++) { - Int32 j = (Int32)Math.Floor(((Double)i) / 2); - if (i % 2 == 0) { - button[j] = new KeyboardButton[(j == button.Length - 1) ? ((buttons.Length % 2 == 0) ? 2 : buttons.Length % 2) : 2]; - } - button[j][i % 2] = new KeyboardButton(buttons[i]); - } - Message x = await this.bot.SendTextMessageAsync(this.chat, message, ParseMode.Default, false, false, 0, new ReplyKeyboardMarkup(button,true,true)); - this.MessageSending?.Invoke(this, new UserMessageEventArgs(x.Text, x.Chat.Id, x.Date)); - } catch (ApiRequestException e) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(e.Message + " " + e.ErrorCode + " " + e.Parameters); - Console.ForegroundColor = ConsoleColor.White; - } - } - } -} +using System; +using System.Collections.Generic; +using BlubbFish.Utils.IoT.Events; +using Telegram.Bot; +using Telegram.Bot.Args; +using Telegram.Bot.Exceptions; +using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; +using Telegram.Bot.Types.ReplyMarkups; + +namespace BlubbFish.Utils.IoT.Connector.User { + public class Telegram : AUserBackend { + private TelegramBotClient bot; + private ChatId chat; + + public override Boolean IsConnected { + get { + return true; + } + } + + public Telegram(Dictionary settings) : base(settings) { + this.bot = new TelegramBotClient(settings["telegram-key"]); + this.bot.OnMessage += this.Bot_OnMessage; + this.Connect(); + } + + private void Bot_OnMessage(Object sender, MessageEventArgs e) { + this.NotifyClientIncomming(new UserEvent(e.Message.Text, e.Message.Chat.Id, e.Message.Date)); + } + + private void Connect() { + this.bot.StartReceiving(); + this.chat = new ChatId(this.settings["chatid"]); + } + + public async override void Send(String message) { + try { + Message x = await this.bot.SendTextMessageAsync(this.chat, message, ParseMode.Default, false, false, 0, new ReplyKeyboardRemove()); + this.NotifyClientSending(new UserEvent(x.Text, x.Chat.Id, x.Date)); + } catch(ApiRequestException e) { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(e.Message+" "+e.ErrorCode+" "+e.Parameters); + Console.ForegroundColor = ConsoleColor.White; + } + + } + + public override void Dispose() { + this.bot.StopReceiving(); + this.bot = null; + } + + public async override void Send(String message, String[] buttons) { + try { + KeyboardButton[][] button = new KeyboardButton[(Int32)Math.Ceiling(((Double)buttons.Length)/2)][]; + for(Int32 i = 0; i < buttons.Length; i++) { + Int32 j = (Int32)Math.Floor(((Double)i) / 2); + if (i % 2 == 0) { + button[j] = new KeyboardButton[(j == button.Length - 1) ? ((buttons.Length % 2 == 0) ? 2 : buttons.Length % 2) : 2]; + } + button[j][i % 2] = new KeyboardButton(buttons[i]); + } + Message x = await this.bot.SendTextMessageAsync(this.chat, message, ParseMode.Default, false, false, 0, new ReplyKeyboardMarkup(button,true,true)); + this.NotifyClientSending(new UserEvent(x.Text, x.Chat.Id, x.Date)); + } catch (ApiRequestException e) { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(e.Message + " " + e.ErrorCode + " " + e.Parameters); + Console.ForegroundColor = ConsoleColor.White; + } + } + } +}