From 3c7ea504ef3278e380b544ed64a31730746f5411 Mon Sep 17 00:00:00 2001 From: Dmitry Pushnitsa Date: Tue, 11 Jun 2019 11:53:00 +0200 Subject: [PATCH] Added WooWa client implementation --- .editorconfig | 103 ++++++++++++++++++ .../IWhatsAppNotificationSendingGateway.cs | 8 ++ .../Notifications/WhatsAppNotification.cs | 13 +++ ...oCommerce.WhatsAppNotification.Core.csproj | 16 ++- .../packages.config | 6 + .../Properties/AssemblyInfo.cs | 4 + ...oCommerce.WhatsAppNotification.Data.csproj | 63 +++++++++++ .../WhatsAppClient/WooWaClient.cs | 42 +++++++ .../packages.config | 6 + .../WhatsAppNotificationSendingGateway.cs | 33 ++++++ .../Module.cs | 23 +++- ...toCommerce.WhatsAppNotification.Web.csproj | 15 ++- .../module.manifest | 27 ++++- VirtoCommerce.WhatsAppNotification.sln | 6 + 14 files changed, 358 insertions(+), 7 deletions(-) create mode 100644 .editorconfig create mode 100644 VirtoCommerce.WhatsAppNotification.Core/Gateway/IWhatsAppNotificationSendingGateway.cs create mode 100644 VirtoCommerce.WhatsAppNotification.Core/Notifications/WhatsAppNotification.cs create mode 100644 VirtoCommerce.WhatsAppNotification.Core/packages.config create mode 100644 VirtoCommerce.WhatsAppNotification.Data/Properties/AssemblyInfo.cs create mode 100644 VirtoCommerce.WhatsAppNotification.Data/VirtoCommerce.WhatsAppNotification.Data.csproj create mode 100644 VirtoCommerce.WhatsAppNotification.Data/WhatsAppClient/WooWaClient.cs create mode 100644 VirtoCommerce.WhatsAppNotification.Data/packages.config create mode 100644 VirtoCommerce.WhatsAppNotification.Web/Gateway/WhatsAppNotificationSendingGateway.cs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..be95936 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,103 @@ +root = true + +[*] +charset = utf-8 +indent_style = space +indent_size = 4 +end_of_line = crlf +trim_trailing_whitespace = true +insert_final_newline = true + +# Project files +[*.csproj] +indent_size = 2 +insert_final_newline = false + +# JSON files +[*.json] +indent_size = 2 + +# Configuration files +[*.config] +indent_size = 2 + +# Dotnet code style settings +[*.{cs,vb}] + +# Sort using and Import directives with System.* appearing first +dotnet_sort_system_directives_first = true + +# Avoid "this." and "Me." if not necessary +dotnet_style_qualification_for_field = false:suggestion +dotnet_style_qualification_for_property = false:suggestion +dotnet_style_qualification_for_method = false:suggestion +dotnet_style_qualification_for_event = false:suggestion + +# Use language keywords instead of framework type names for type references +dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion +dotnet_style_predefined_type_for_member_access = true:suggestion + +# Use explicit accessibility modifiers +dotnet_style_require_accessibility_modifiers = true:suggestion + +# Suggest more modern language features when available +dotnet_style_object_initializer = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_coalesce_expression = true:suggestion +dotnet_style_null_propagation = true:suggestion +dotnet_style_explicit_tuple_names = true:suggestion +dotnet_prefer_inferred_tuple_names = true:suggestion +dotnet_prefer_inferred_anonymous_type_member_names = true:suggestion + +# CSharp code style settings +[*.cs] + +# Prefer curly braces even for one line of code +csharp_prefer_braces = true:suggestion + +# Prefer "var" everywhere +csharp_style_var_for_built_in_types = true:suggestion +csharp_style_var_when_type_is_apparent = true:suggestion +csharp_style_var_elsewhere = true:suggestion + +# Prefer method-like constructs to have a block body +csharp_style_expression_bodied_methods = false:none +csharp_style_expression_bodied_constructors = false:none +csharp_style_expression_bodied_operators = false:none + +# Prefer property-like constructs to have an expression-body +csharp_style_expression_bodied_properties = true:none +csharp_style_expression_bodied_indexers = true:none +csharp_style_expression_bodied_accessors = true:none + +# Suggest more modern language features when available +csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion +csharp_style_pattern_matching_over_as_with_null_check = true:suggestion +csharp_style_inlined_variable_declaration = true:suggestion +csharp_style_throw_expression = true:suggestion +csharp_style_conditional_delegate_call = true:suggestion +csharp_prefer_simple_default_expression = true:suggestion +csharp_style_deconstructed_variable_declaration = true:suggestion +csharp_style_pattern_local_over_anonymous_function = true:suggestion + +# Newline settings +csharp_new_line_before_open_brace = all +csharp_new_line_before_else = true +csharp_new_line_before_catch = true +csharp_new_line_before_finally = true +csharp_new_line_before_members_in_object_initializers = true +csharp_new_line_before_members_in_anonymous_types = true +csharp_new_line_between_query_expression_clauses = true + +csharp_indent_case_contents = true +csharp_indent_switch_labels = true +csharp_indent_labels = flush_left + +csharp_space_after_cast = false +csharp_space_after_keywords_in_control_flow_statements = true +csharp_space_between_method_declaration_parameter_list_parentheses = false +csharp_space_between_method_call_parameter_list_parentheses = false +csharp_space_between_parentheses = false + +csharp_preserve_single_line_statements = false +csharp_preserve_single_line_blocks = true diff --git a/VirtoCommerce.WhatsAppNotification.Core/Gateway/IWhatsAppNotificationSendingGateway.cs b/VirtoCommerce.WhatsAppNotification.Core/Gateway/IWhatsAppNotificationSendingGateway.cs new file mode 100644 index 0000000..ca7b67b --- /dev/null +++ b/VirtoCommerce.WhatsAppNotification.Core/Gateway/IWhatsAppNotificationSendingGateway.cs @@ -0,0 +1,8 @@ +using VirtoCommerce.Platform.Core.Notifications; + +namespace VirtoCommerce.WhatsAppNotification.Core.Gateway +{ + public interface IWhatsAppNotificationSendingGateway : INotificationSendingGateway + { + } +} diff --git a/VirtoCommerce.WhatsAppNotification.Core/Notifications/WhatsAppNotification.cs b/VirtoCommerce.WhatsAppNotification.Core/Notifications/WhatsAppNotification.cs new file mode 100644 index 0000000..41b0d95 --- /dev/null +++ b/VirtoCommerce.WhatsAppNotification.Core/Notifications/WhatsAppNotification.cs @@ -0,0 +1,13 @@ +using VirtoCommerce.Platform.Core.Notifications; +using VirtoCommerce.WhatsAppNotification.Core.Gateway; + +namespace VirtoCommerce.WhatsAppNotification.Core.Notifications +{ + public class WhatsAppNotification : Notification + { + public WhatsAppNotification(IWhatsAppNotificationSendingGateway notificationSendingGateway) + : base(notificationSendingGateway) + { + } + } +} diff --git a/VirtoCommerce.WhatsAppNotification.Core/VirtoCommerce.WhatsAppNotification.Core.csproj b/VirtoCommerce.WhatsAppNotification.Core/VirtoCommerce.WhatsAppNotification.Core.csproj index f4778bb..ba9d2b4 100644 --- a/VirtoCommerce.WhatsAppNotification.Core/VirtoCommerce.WhatsAppNotification.Core.csproj +++ b/VirtoCommerce.WhatsAppNotification.Core/VirtoCommerce.WhatsAppNotification.Core.csproj @@ -31,7 +31,13 @@ 4 + + ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll + + + ..\packages\System.Collections.Immutable.1.4.0\lib\netstandard2.0\System.Collections.Immutable.dll + @@ -39,13 +45,21 @@ + + ..\packages\VirtoCommerce.Platform.Core.2.13.48\lib\net461\VirtoCommerce.Platform.Core.dll + Properties\CommonAssemblyInfo.cs - + + + + + + \ No newline at end of file diff --git a/VirtoCommerce.WhatsAppNotification.Core/packages.config b/VirtoCommerce.WhatsAppNotification.Core/packages.config new file mode 100644 index 0000000..6d3dda2 --- /dev/null +++ b/VirtoCommerce.WhatsAppNotification.Core/packages.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/VirtoCommerce.WhatsAppNotification.Data/Properties/AssemblyInfo.cs b/VirtoCommerce.WhatsAppNotification.Data/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..093d697 --- /dev/null +++ b/VirtoCommerce.WhatsAppNotification.Data/Properties/AssemblyInfo.cs @@ -0,0 +1,4 @@ +using System.Reflection; + +[assembly: AssemblyTitle("VirtoCommerce.WhatsAppNotification.Data")] +[assembly: AssemblyDescription("")] diff --git a/VirtoCommerce.WhatsAppNotification.Data/VirtoCommerce.WhatsAppNotification.Data.csproj b/VirtoCommerce.WhatsAppNotification.Data/VirtoCommerce.WhatsAppNotification.Data.csproj new file mode 100644 index 0000000..d55332b --- /dev/null +++ b/VirtoCommerce.WhatsAppNotification.Data/VirtoCommerce.WhatsAppNotification.Data.csproj @@ -0,0 +1,63 @@ + + + + + Debug + AnyCPU + {924E7BA3-9FE9-4020-B14C-E002A6D40083} + Library + Properties + VirtoCommerce.WhatsAppNotification.Data + VirtoCommerce.WhatsAppNotification.Data + v4.6.1 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll + + + + ..\packages\System.Collections.Immutable.1.4.0\lib\netstandard2.0\System.Collections.Immutable.dll + + + + + + + + + + ..\packages\VirtoCommerce.Platform.Core.2.13.48\lib\net461\VirtoCommerce.Platform.Core.dll + + + + + Properties\CommonAssemblyInfo.cs + + + + + + + + + \ No newline at end of file diff --git a/VirtoCommerce.WhatsAppNotification.Data/WhatsAppClient/WooWaClient.cs b/VirtoCommerce.WhatsAppNotification.Data/WhatsAppClient/WooWaClient.cs new file mode 100644 index 0000000..43d7956 --- /dev/null +++ b/VirtoCommerce.WhatsAppNotification.Data/WhatsAppClient/WooWaClient.cs @@ -0,0 +1,42 @@ +using System.Net; +using System.Text; +using VirtoCommerce.Platform.Core.Settings; + +namespace VirtoCommerce.WhatsAppNotification.Data.WhatsAppClient +{ + public class WooWaClient + { + private readonly ISettingsManager _settingsManager; + + public WooWaClient(ISettingsManager settingsManager) + { + _settingsManager = settingsManager; + } + + public void SendMessage(string recipient, string message) + { + var license = _settingsManager.GetValue("VirtoCommerce.WhatsApp.License", default(string)); + var domain = _settingsManager.GetValue("VirtoCommerce.WhatsApp.Domain", default(string)); + var apiEndpoint = _settingsManager.GetValue("VirtoCommerce.WhatsApp.APIEndpoint", default(string)); + + if (!string.IsNullOrEmpty(license) && !string.IsNullOrEmpty(domain) && !string.IsNullOrEmpty(apiEndpoint)) + { + var request = (HttpWebRequest)WebRequest.Create($"{apiEndpoint}/send-message"); + request.Method = "POST"; + request.ContentType = "application/x-www-form-urlencoded"; + + var data = $"license={license}&domain={domain}&wa_number={recipient}&text={message}"; + var byteArray = Encoding.UTF8.GetBytes(data); + + request.ContentLength = byteArray.Length; + + using (var dataStream = request.GetRequestStream()) + { + dataStream.Write(byteArray, 0, byteArray.Length); + } + + request.GetResponse(); + } + } + } +} diff --git a/VirtoCommerce.WhatsAppNotification.Data/packages.config b/VirtoCommerce.WhatsAppNotification.Data/packages.config new file mode 100644 index 0000000..6d3dda2 --- /dev/null +++ b/VirtoCommerce.WhatsAppNotification.Data/packages.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/VirtoCommerce.WhatsAppNotification.Web/Gateway/WhatsAppNotificationSendingGateway.cs b/VirtoCommerce.WhatsAppNotification.Web/Gateway/WhatsAppNotificationSendingGateway.cs new file mode 100644 index 0000000..509fef5 --- /dev/null +++ b/VirtoCommerce.WhatsAppNotification.Web/Gateway/WhatsAppNotificationSendingGateway.cs @@ -0,0 +1,33 @@ +using VirtoCommerce.Platform.Core.Notifications; +using VirtoCommerce.WhatsAppNotification.Core.Gateway; +using VirtoCommerce.WhatsAppNotification.Data.WhatsAppClient; + +namespace VirtoCommerce.WhatsAppNotification.Web.Gateway +{ + public class WhatsAppNotificationSendingGateway : IWhatsAppNotificationSendingGateway + { + private readonly WooWaClient _client; + + public WhatsAppNotificationSendingGateway(WooWaClient client) + { + _client = client; + } + + public SendNotificationResult SendNotification(Notification notification) + { + var result = new SendNotificationResult(); + + _client.SendMessage(notification.Recipient, notification.Body); + + result.IsSuccess = true; + + return result; + } + + public bool ValidateNotification(Notification notification) + { + return !string.IsNullOrEmpty(notification.Recipient) && + !string.IsNullOrEmpty(notification.Body); + } + } +} diff --git a/VirtoCommerce.WhatsAppNotification.Web/Module.cs b/VirtoCommerce.WhatsAppNotification.Web/Module.cs index 4d447d0..d5708eb 100644 --- a/VirtoCommerce.WhatsAppNotification.Web/Module.cs +++ b/VirtoCommerce.WhatsAppNotification.Web/Module.cs @@ -1,5 +1,9 @@ -using Microsoft.Practices.Unity; +using Microsoft.Practices.Unity; using VirtoCommerce.Platform.Core.Modularity; +using VirtoCommerce.Platform.Core.Notifications; +using VirtoCommerce.WhatsAppNotification.Core.Gateway; +using VirtoCommerce.WhatsAppNotification.Data.WhatsAppClient; +using VirtoCommerce.WhatsAppNotification.Web.Gateway; namespace VirtoCommerce.WhatsAppNotification.Web { @@ -15,6 +19,21 @@ public Module(IUnityContainer container) public override void Initialize() { base.Initialize(); + + _container.RegisterType(); + _container.RegisterType(); + + var notificationManager = _container.Resolve(); + + notificationManager.RegisterNotificationType(() => new Core.Notifications.WhatsAppNotification(_container.Resolve()) + { + DisplayName = "WhatsApp notification", + Description = "", + NotificationTemplate = new NotificationTemplate + { + Body = "Sample notification body." + } + }); } } -} \ No newline at end of file +} diff --git a/VirtoCommerce.WhatsAppNotification.Web/VirtoCommerce.WhatsAppNotification.Web.csproj b/VirtoCommerce.WhatsAppNotification.Web/VirtoCommerce.WhatsAppNotification.Web.csproj index b039218..74a2b78 100644 --- a/VirtoCommerce.WhatsAppNotification.Web/VirtoCommerce.WhatsAppNotification.Web.csproj +++ b/VirtoCommerce.WhatsAppNotification.Web/VirtoCommerce.WhatsAppNotification.Web.csproj @@ -95,11 +95,14 @@ Properties\CommonAssemblyInfo.cs + - + + Designer + @@ -109,6 +112,16 @@ Web.config + + + {9f1b05f5-c50a-412e-abd9-92163bd1d640} + VirtoCommerce.WhatsAppNotification.Core + + + {924e7ba3-9fe9-4020-b14c-e002a6d40083} + VirtoCommerce.WhatsAppNotification.Data + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/VirtoCommerce.WhatsAppNotification.Web/module.manifest b/VirtoCommerce.WhatsAppNotification.Web/module.manifest index c3bf038..b598b98 100644 --- a/VirtoCommerce.WhatsAppNotification.Web/module.manifest +++ b/VirtoCommerce.WhatsAppNotification.Web/module.manifest @@ -1,9 +1,9 @@ - + VirtoCommerce.WhatsAppNotification 1.0.0 - 2.13.48 + 2.13.47 @@ -19,4 +19,25 @@ VirtoCommerce.WhatsAppNotification.Web.dll VirtoCommerce.WhatsAppNotification.Web.Module, VirtoCommerce.WhatsAppNotification.Web - \ No newline at end of file + + + + + VirtoCommerce.WhatsApp.License + WooWa license + string + + + VirtoCommerce.WhatsApp.Domain + WooWa account domain + string + + + VirtoCommerce.WhatsApp.APIEndpoint + WooWa API endpoint + string + https://api.woo-wa.com/v2.0 + + + + diff --git a/VirtoCommerce.WhatsAppNotification.sln b/VirtoCommerce.WhatsAppNotification.sln index d3c528a..6989df6 100644 --- a/VirtoCommerce.WhatsAppNotification.sln +++ b/VirtoCommerce.WhatsAppNotification.sln @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VirtoCommerce.WhatsAppNotif EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VirtoCommerce.WhatsAppNotification.Core", "VirtoCommerce.WhatsAppNotification.Core\VirtoCommerce.WhatsAppNotification.Core.csproj", "{9F1B05F5-C50A-412E-ABD9-92163BD1D640}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VirtoCommerce.WhatsAppNotification.Data", "VirtoCommerce.WhatsAppNotification.Data\VirtoCommerce.WhatsAppNotification.Data.csproj", "{924E7BA3-9FE9-4020-B14C-E002A6D40083}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {9F1B05F5-C50A-412E-ABD9-92163BD1D640}.Debug|Any CPU.Build.0 = Debug|Any CPU {9F1B05F5-C50A-412E-ABD9-92163BD1D640}.Release|Any CPU.ActiveCfg = Release|Any CPU {9F1B05F5-C50A-412E-ABD9-92163BD1D640}.Release|Any CPU.Build.0 = Release|Any CPU + {924E7BA3-9FE9-4020-B14C-E002A6D40083}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {924E7BA3-9FE9-4020-B14C-E002A6D40083}.Debug|Any CPU.Build.0 = Debug|Any CPU + {924E7BA3-9FE9-4020-B14C-E002A6D40083}.Release|Any CPU.ActiveCfg = Release|Any CPU + {924E7BA3-9FE9-4020-B14C-E002A6D40083}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE