Skip to content

Commit

Permalink
Added WooWa client implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pushnitsa committed Jun 11, 2019
1 parent 0a5a0b6 commit 3c7ea50
Show file tree
Hide file tree
Showing 14 changed files with 358 additions and 7 deletions.
103 changes: 103 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using VirtoCommerce.Platform.Core.Notifications;

namespace VirtoCommerce.WhatsAppNotification.Core.Gateway
{
public interface IWhatsAppNotificationSendingGateway : INotificationSendingGateway
{
}
}
Original file line number Diff line number Diff line change
@@ -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)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,35 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable, Version=1.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Immutable.1.4.0\lib\netstandard2.0\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="VirtoCommerce.Platform.Core, Version=2.13.48.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\VirtoCommerce.Platform.Core.2.13.48\lib\net461\VirtoCommerce.Platform.Core.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Class1.cs" />
<Compile Include="Gateway\IWhatsAppNotificationSendingGateway.cs" />
<Compile Include="Notifications\WhatsAppNotification.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
6 changes: 6 additions & 0 deletions VirtoCommerce.WhatsAppNotification.Core/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net461" />
<package id="System.Collections.Immutable" version="1.4.0" targetFramework="net461" />
<package id="VirtoCommerce.Platform.Core" version="2.13.48" targetFramework="net461" />
</packages>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using System.Reflection;

[assembly: AssemblyTitle("VirtoCommerce.WhatsAppNotification.Data")]
[assembly: AssemblyDescription("")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{924E7BA3-9FE9-4020-B14C-E002A6D40083}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>VirtoCommerce.WhatsAppNotification.Data</RootNamespace>
<AssemblyName>VirtoCommerce.WhatsAppNotification.Data</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable, Version=1.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Immutable.1.4.0\lib\netstandard2.0\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="VirtoCommerce.Platform.Core, Version=2.13.48.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\VirtoCommerce.Platform.Core.2.13.48\lib\net461\VirtoCommerce.Platform.Core.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WhatsAppClient\WooWaClient.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
6 changes: 6 additions & 0 deletions VirtoCommerce.WhatsAppNotification.Data/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net461" />
<package id="System.Collections.Immutable" version="1.4.0" targetFramework="net461" />
<package id="VirtoCommerce.Platform.Core" version="2.13.48" targetFramework="net461" />
</packages>
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
23 changes: 21 additions & 2 deletions VirtoCommerce.WhatsAppNotification.Web/Module.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -15,6 +19,21 @@ public Module(IUnityContainer container)
public override void Initialize()
{
base.Initialize();

_container.RegisterType<IWhatsAppNotificationSendingGateway, WhatsAppNotificationSendingGateway>();
_container.RegisterType<WooWaClient>();

var notificationManager = _container.Resolve<INotificationManager>();

notificationManager.RegisterNotificationType(() => new Core.Notifications.WhatsAppNotification(_container.Resolve<IWhatsAppNotificationSendingGateway>())
{
DisplayName = "WhatsApp notification",
Description = "",
NotificationTemplate = new NotificationTemplate
{
Body = "Sample notification body."
}
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@
<Compile Include="..\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Gateway\WhatsAppNotificationSendingGateway.cs" />
<Compile Include="Module.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="module.manifest" />
<Content Include="module.manifest">
<SubType>Designer</SubType>
</Content>
<Content Include="module.ignore" />
<None Include="packages.config" />
<None Include="Web.Debug.config">
Expand All @@ -109,6 +112,16 @@
<DependentUpon>Web.config</DependentUpon>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\VirtoCommerce.WhatsAppNotification.Core\VirtoCommerce.WhatsAppNotification.Core.csproj">
<Project>{9f1b05f5-c50a-412e-abd9-92163bd1d640}</Project>
<Name>VirtoCommerce.WhatsAppNotification.Core</Name>
</ProjectReference>
<ProjectReference Include="..\VirtoCommerce.WhatsAppNotification.Data\VirtoCommerce.WhatsAppNotification.Data.csproj">
<Project>{924e7ba3-9fe9-4020-b14c-e002a6d40083}</Project>
<Name>VirtoCommerce.WhatsAppNotification.Data</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Expand Down
Loading

0 comments on commit 3c7ea50

Please sign in to comment.