From cfe1d57d273d2a54268e0e399a8332800c13dee2 Mon Sep 17 00:00:00 2001 From: Julien Plateau Date: Fri, 6 Jul 2018 14:50:59 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20d'un=202eme=20bot=20pour=20d=C3=A9marr?= =?UTF-8?q?=C3=A9=20le=20premier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApolloWorldCup.Library.csproj | 89 ++++++++++++ ApolloWorldCup.Library/App.config | 18 +++ .../PronoApi.cs | 2 +- .../Properties/AssemblyInfo.cs | 36 +++++ .../SlackApi.cs | 2 +- .../SlackMessageApi.cs | 2 +- .../SlackResultApi.cs | 2 +- .../WorldCupApi.cs | 2 +- .../WorldCupMatch.cs | 2 +- ApolloWorldCup.Library/packages.config | 12 ++ ApolloWorldCup.sln | 12 ++ ApolloWorldCup/ApolloWorldCup.csproj | 12 +- ApolloWorldCup/Program.cs | 4 +- ApolloWorldCup/SlackBot.cs | 3 +- .../ApolloWorldCupLauncher.csproj | 81 +++++++++++ ApolloWorldCupLauncher/App.config | 18 +++ ApolloWorldCupLauncher/Program.cs | 67 +++++++++ .../Properties/AssemblyInfo.cs | 36 +++++ ApolloWorldCupLauncher/SlackBot.cs | 131 ++++++++++++++++++ ApolloWorldCupLauncher/packages.config | 9 ++ 20 files changed, 524 insertions(+), 16 deletions(-) create mode 100644 ApolloWorldCup.Library/ApolloWorldCup.Library.csproj create mode 100644 ApolloWorldCup.Library/App.config rename {ApolloWorldCup => ApolloWorldCup.Library}/PronoApi.cs (99%) create mode 100644 ApolloWorldCup.Library/Properties/AssemblyInfo.cs rename {ApolloWorldCup => ApolloWorldCup.Library}/SlackApi.cs (98%) rename {ApolloWorldCup => ApolloWorldCup.Library}/SlackMessageApi.cs (95%) rename {ApolloWorldCup => ApolloWorldCup.Library}/SlackResultApi.cs (92%) rename {ApolloWorldCup => ApolloWorldCup.Library}/WorldCupApi.cs (99%) rename {ApolloWorldCup => ApolloWorldCup.Library}/WorldCupMatch.cs (98%) create mode 100644 ApolloWorldCup.Library/packages.config create mode 100644 ApolloWorldCupLauncher/ApolloWorldCupLauncher.csproj create mode 100644 ApolloWorldCupLauncher/App.config create mode 100644 ApolloWorldCupLauncher/Program.cs create mode 100644 ApolloWorldCupLauncher/Properties/AssemblyInfo.cs create mode 100644 ApolloWorldCupLauncher/SlackBot.cs create mode 100644 ApolloWorldCupLauncher/packages.config diff --git a/ApolloWorldCup.Library/ApolloWorldCup.Library.csproj b/ApolloWorldCup.Library/ApolloWorldCup.Library.csproj new file mode 100644 index 0000000..f999e33 --- /dev/null +++ b/ApolloWorldCup.Library/ApolloWorldCup.Library.csproj @@ -0,0 +1,89 @@ + + + + + Debug + AnyCPU + {9884EB0B-BCDF-41DC-BC6D-A9CBABABCB0F} + Library + ApolloWorldCup.Library + ApolloWorldCup.Library + v4.6 + 512 + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + ..\packages\HtmlAgilityPack.1.8.4\lib\Net45\HtmlAgilityPack.dll + + + ..\packages\log4net.2.0.8\lib\net45-full\log4net.dll + + + ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll + + + ..\packages\Slack.Webhooks.1.0.5\lib\net45\Slack.Webhooks.dll + + + + + ..\packages\System.Net.Http.4.3.3\lib\net46\System.Net.Http.dll + + + + ..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net46\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\net46\System.Security.Cryptography.X509Certificates.dll + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ApolloWorldCup.Library/App.config b/ApolloWorldCup.Library/App.config new file mode 100644 index 0000000..29fbba0 --- /dev/null +++ b/ApolloWorldCup.Library/App.config @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/ApolloWorldCup/PronoApi.cs b/ApolloWorldCup.Library/PronoApi.cs similarity index 99% rename from ApolloWorldCup/PronoApi.cs rename to ApolloWorldCup.Library/PronoApi.cs index 423ca2d..8b69134 100644 --- a/ApolloWorldCup/PronoApi.cs +++ b/ApolloWorldCup.Library/PronoApi.cs @@ -11,7 +11,7 @@ using System.Xml; using System.Xml.Linq; -namespace ApolloWorldCup +namespace ApolloWorldCup.Library { public class PronoApi { diff --git a/ApolloWorldCup.Library/Properties/AssemblyInfo.cs b/ApolloWorldCup.Library/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..716790f --- /dev/null +++ b/ApolloWorldCup.Library/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Les informations générales relatives à un assembly dépendent de +// l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations +// associées à un assembly. +[assembly: AssemblyTitle("ApolloWorldCup.Library")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ApolloWorldCup.Library")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly +// aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de +// COM, affectez la valeur true à l'attribut ComVisible sur ce type. +[assembly: ComVisible(false)] + +// Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM +[assembly: Guid("9884eb0b-bcdf-41dc-bc6d-a9cbababcb0f")] + +// Les informations de version pour un assembly se composent des quatre valeurs suivantes : +// +// Version principale +// Version secondaire +// Numéro de build +// Révision +// +// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut +// en utilisant '*', comme indiqué ci-dessous : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ApolloWorldCup/SlackApi.cs b/ApolloWorldCup.Library/SlackApi.cs similarity index 98% rename from ApolloWorldCup/SlackApi.cs rename to ApolloWorldCup.Library/SlackApi.cs index a58311b..1de534f 100644 --- a/ApolloWorldCup/SlackApi.cs +++ b/ApolloWorldCup.Library/SlackApi.cs @@ -8,7 +8,7 @@ using System.Text; using System.Threading.Tasks; -namespace ApolloWorldCup +namespace ApolloWorldCup.Library { public class SlackApi diff --git a/ApolloWorldCup/SlackMessageApi.cs b/ApolloWorldCup.Library/SlackMessageApi.cs similarity index 95% rename from ApolloWorldCup/SlackMessageApi.cs rename to ApolloWorldCup.Library/SlackMessageApi.cs index 58e36d4..f5bc909 100644 --- a/ApolloWorldCup/SlackMessageApi.cs +++ b/ApolloWorldCup.Library/SlackMessageApi.cs @@ -3,7 +3,7 @@ using System.Runtime.Serialization; using System.Text; -namespace ApolloWorldCup +namespace ApolloWorldCup.Library { [DataContract] public class SlackMessageApi diff --git a/ApolloWorldCup/SlackResultApi.cs b/ApolloWorldCup.Library/SlackResultApi.cs similarity index 92% rename from ApolloWorldCup/SlackResultApi.cs rename to ApolloWorldCup.Library/SlackResultApi.cs index 897e458..99146a4 100644 --- a/ApolloWorldCup/SlackResultApi.cs +++ b/ApolloWorldCup.Library/SlackResultApi.cs @@ -3,7 +3,7 @@ using System.Runtime.Serialization; using System.Text; -namespace ApolloWorldCup +namespace ApolloWorldCup.Library { [DataContract] public class SlackResultApi diff --git a/ApolloWorldCup/WorldCupApi.cs b/ApolloWorldCup.Library/WorldCupApi.cs similarity index 99% rename from ApolloWorldCup/WorldCupApi.cs rename to ApolloWorldCup.Library/WorldCupApi.cs index 9b61ba1..1d648a6 100644 --- a/ApolloWorldCup/WorldCupApi.cs +++ b/ApolloWorldCup.Library/WorldCupApi.cs @@ -7,7 +7,7 @@ using System.Threading; using System.Threading.Tasks; -namespace ApolloWorldCup +namespace ApolloWorldCup.Library { public class WorldCupApi { diff --git a/ApolloWorldCup/WorldCupMatch.cs b/ApolloWorldCup.Library/WorldCupMatch.cs similarity index 98% rename from ApolloWorldCup/WorldCupMatch.cs rename to ApolloWorldCup.Library/WorldCupMatch.cs index 6d96796..6fc8294 100644 --- a/ApolloWorldCup/WorldCupMatch.cs +++ b/ApolloWorldCup.Library/WorldCupMatch.cs @@ -3,7 +3,7 @@ using System.Runtime.Serialization; using System.Text; -namespace ApolloWorldCup +namespace ApolloWorldCup.Library { [DataContract] public class WorldCupMatch diff --git a/ApolloWorldCup.Library/packages.config b/ApolloWorldCup.Library/packages.config new file mode 100644 index 0000000..a171f03 --- /dev/null +++ b/ApolloWorldCup.Library/packages.config @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/ApolloWorldCup.sln b/ApolloWorldCup.sln index ad59ddb..4d7fb30 100644 --- a/ApolloWorldCup.sln +++ b/ApolloWorldCup.sln @@ -5,6 +5,10 @@ VisualStudioVersion = 15.0.27130.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApolloWorldCup", "ApolloWorldCup\ApolloWorldCup.csproj", "{428B9E12-4861-45FF-BFA4-F2AA3F112548}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApolloWorldCupLauncher", "ApolloWorldCupLauncher\ApolloWorldCupLauncher.csproj", "{A42C4442-510F-4EDA-B927-C27C20F84B8D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApolloWorldCup.Library", "ApolloWorldCup.Library\ApolloWorldCup.Library.csproj", "{9884EB0B-BCDF-41DC-BC6D-A9CBABABCB0F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +19,14 @@ Global {428B9E12-4861-45FF-BFA4-F2AA3F112548}.Debug|Any CPU.Build.0 = Debug|Any CPU {428B9E12-4861-45FF-BFA4-F2AA3F112548}.Release|Any CPU.ActiveCfg = Release|Any CPU {428B9E12-4861-45FF-BFA4-F2AA3F112548}.Release|Any CPU.Build.0 = Release|Any CPU + {A42C4442-510F-4EDA-B927-C27C20F84B8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A42C4442-510F-4EDA-B927-C27C20F84B8D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A42C4442-510F-4EDA-B927-C27C20F84B8D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A42C4442-510F-4EDA-B927-C27C20F84B8D}.Release|Any CPU.Build.0 = Release|Any CPU + {9884EB0B-BCDF-41DC-BC6D-A9CBABABCB0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9884EB0B-BCDF-41DC-BC6D-A9CBABABCB0F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9884EB0B-BCDF-41DC-BC6D-A9CBABABCB0F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9884EB0B-BCDF-41DC-BC6D-A9CBABABCB0F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ApolloWorldCup/ApolloWorldCup.csproj b/ApolloWorldCup/ApolloWorldCup.csproj index 845aac2..00d3daa 100644 --- a/ApolloWorldCup/ApolloWorldCup.csproj +++ b/ApolloWorldCup/ApolloWorldCup.csproj @@ -101,13 +101,7 @@ - - - - - - @@ -130,5 +124,11 @@ false + + + {9884eb0b-bcdf-41dc-bc6d-a9cbababcb0f} + ApolloWorldCup.Library + + \ No newline at end of file diff --git a/ApolloWorldCup/Program.cs b/ApolloWorldCup/Program.cs index 95b9d54..ac9a43a 100644 --- a/ApolloWorldCup/Program.cs +++ b/ApolloWorldCup/Program.cs @@ -6,6 +6,7 @@ using System.Reflection; using System.IO; using System.Threading; +using ApolloWorldCup.Library; namespace ApolloWorldCup { @@ -24,9 +25,6 @@ class Program static void Main(string[] args) { - var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); - XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config")); - Console.ForegroundColor = ConsoleColor.White; try diff --git a/ApolloWorldCup/SlackBot.cs b/ApolloWorldCup/SlackBot.cs index b26c451..8bcf5f2 100644 --- a/ApolloWorldCup/SlackBot.cs +++ b/ApolloWorldCup/SlackBot.cs @@ -1,4 +1,5 @@ -using log4net; +using ApolloWorldCup.Library; +using log4net; using log4net.Core; using Slack.Webhooks; using System; diff --git a/ApolloWorldCupLauncher/ApolloWorldCupLauncher.csproj b/ApolloWorldCupLauncher/ApolloWorldCupLauncher.csproj new file mode 100644 index 0000000..e3b0110 --- /dev/null +++ b/ApolloWorldCupLauncher/ApolloWorldCupLauncher.csproj @@ -0,0 +1,81 @@ + + + + + Debug + AnyCPU + {A42C4442-510F-4EDA-B927-C27C20F84B8D} + Exe + ApolloWorldCupLauncher + ApolloWorldCupLauncher + v4.6 + 512 + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\TaskScheduler.2.8.1\lib\net452\Microsoft.Win32.TaskScheduler.dll + + + + + + + ..\packages\System.Net.Http.4.3.3\lib\net46\System.Net.Http.dll + + + + ..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net46\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\net46\System.Security.Cryptography.X509Certificates.dll + + + + + + + + + + + + + + + + + + + {9884eb0b-bcdf-41dc-bc6d-a9cbababcb0f} + ApolloWorldCup.Library + + + + \ No newline at end of file diff --git a/ApolloWorldCupLauncher/App.config b/ApolloWorldCupLauncher/App.config new file mode 100644 index 0000000..091393d --- /dev/null +++ b/ApolloWorldCupLauncher/App.config @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/ApolloWorldCupLauncher/Program.cs b/ApolloWorldCupLauncher/Program.cs new file mode 100644 index 0000000..de11eba --- /dev/null +++ b/ApolloWorldCupLauncher/Program.cs @@ -0,0 +1,67 @@ +using ApolloWorldCup; +using ApolloWorldCup.Library; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace ApolloWorldCupLauncher +{ + class Program + { + public static SlackApi _slackApi; + public static SlackBot _slackBot; + public static bool _enableSlackApi = false; + public static bool _running = true; + + public static string _channelId = "CAZAYAE1G"; + + static void Main(string[] args) + { + Console.ForegroundColor = ConsoleColor.White; + + try + { + string webhook = null; + string token = null; + string channelId = _channelId; + if (args != null) + { + if (args.Length > 0) + { + webhook = args[0]; + _enableSlackApi = true; + } + if (args.Length > 1) + { + token = args[1]; + } + if (args.Length > 2) + { + channelId = args[2]; + } + } + + _slackApi = new SlackApi(webhook); + + _slackBot = new SlackBot(_slackApi, channelId, token, () => _running = false); + _slackBot.Start(); + + + while (_running) + { + Thread.Sleep(1000); + } + + _slackBot.Stop(); + + } + catch (Exception e) + { + Console.WriteLine(e); + } + } + } +} diff --git a/ApolloWorldCupLauncher/Properties/AssemblyInfo.cs b/ApolloWorldCupLauncher/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..c3b8a76 --- /dev/null +++ b/ApolloWorldCupLauncher/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Les informations générales relatives à un assembly dépendent de +// l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations +// associées à un assembly. +[assembly: AssemblyTitle("ApolloWorldCupLauncher")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ApolloWorldCupLauncher")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly +// aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de +// COM, affectez la valeur true à l'attribut ComVisible sur ce type. +[assembly: ComVisible(false)] + +// Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM +[assembly: Guid("a42c4442-510f-4eda-b927-c27c20f84b8d")] + +// Les informations de version pour un assembly se composent des quatre valeurs suivantes : +// +// Version principale +// Version secondaire +// Numéro de build +// Révision +// +// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut +// en utilisant '*', comme indiqué ci-dessous : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ApolloWorldCupLauncher/SlackBot.cs b/ApolloWorldCupLauncher/SlackBot.cs new file mode 100644 index 0000000..6c06b08 --- /dev/null +++ b/ApolloWorldCupLauncher/SlackBot.cs @@ -0,0 +1,131 @@ +using ApolloWorldCup; +using ApolloWorldCup.Library; +using Microsoft.Win32.TaskScheduler; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading; + +namespace ApolloWorldCupLauncher +{ + public class SlackBot + { + private Dictionary _commands; + private string _channelId; + private SlackApi _api; + private string _token; + private Thread _threadBot; + private Thread _threadWc; + private bool _running; + private SlackMessageApi _firstMessage; + private SlackMessageApi _lastMessage; + private DateTime _start; + + private List _previousMatches; + private string _channel = "#sport"; + private CultureInfo[] _cultures; + private System.Action _onClose; + + public SlackBot(SlackApi api, string channelId, string token, System.Action onClose) + { + _channelId = channelId; + _token = token; + _start = DateTime.Now; + _onClose = onClose; + _api = api; + + _cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures); + + _commands = new Dictionary() + { + { "!start", () => { + // Create a new task definition and assign properties + var task = TaskService.Instance.FindTask("ApolloWorldCup"); + + if(task.State != TaskState.Running) + { + task.Run(); + } + } + } + }; + + } + + public void Start() + { + _running = true; + + _threadBot = new Thread(Run); + _threadBot.Start(); + } + + public void Stop() + { + _running = false; + } + + public void Run() + { + while (_running) + { + try + { + List messages = null; + + if (_lastMessage == null) + { + messages = _api.GetMessagesFromChannel(_token, _channelId, 100).Result.ToList(); + } + else + { + messages = _api.GetMessagesFromChannel(_token, _channelId, 100, _lastMessage.TimeStamp).Result.ToList(); + } + + if (_lastMessage == null) + { + _lastMessage = messages.Any() ? messages.FirstOrDefault() : null; + } + else + { + _lastMessage = messages.Any() ? messages.FirstOrDefault() : _lastMessage; + + if (messages.Any()) + { + foreach (var message in messages) + { + ExecuteCommand(message.Text); + } + } + } + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + + Thread.Sleep(1000); + } + } + + public void ExecuteCommand(string commandAsked) + { + if (string.IsNullOrEmpty(commandAsked)) + { + return; + } + + var parseLine = commandAsked.Split(' '); + var commandStr = parseLine[0].ToLowerInvariant(); + + var command = _commands.Keys.Where(c => c == commandStr).FirstOrDefault(); + + if (_commands.ContainsKey(commandStr)) + { + _commands[commandStr].Invoke(); + } + } + } +} diff --git a/ApolloWorldCupLauncher/packages.config b/ApolloWorldCupLauncher/packages.config new file mode 100644 index 0000000..ef4c904 --- /dev/null +++ b/ApolloWorldCupLauncher/packages.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file