Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Intercom Webhook Support #304

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/WebHooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ASP.NET Core WebHooks provide support for receiving WebHooks. The packages depen
- [Dropbox](./samples/DropboxCoreReceiver)
- [DynamicCRM](./samples/DynamicsCRMCoreReceiver)
- [GitHub](./samples/GitHubCoreReceiver)
- [Intercom](./samples/IntercomCoreReceiver)
- [Kudu](./samples/KuduCoreReceiver)
- [MailChimp](./samples/MailChimpCoreReceiver)
- [Pusher](./samples/PusherCoreReceiver)
Expand Down
14 changes: 14 additions & 0 deletions src/WebHooks/WebHooks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrelloCoreReceiver", "sampl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.WebHooks.FunctionalTest", "test\Microsoft.AspNetCore.WebHooks.FunctionalTest\Microsoft.AspNetCore.WebHooks.FunctionalTest.csproj", "{F42E56B7-60D4-481A-8585-04015B2B501E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.WebHooks.Receivers.Intercom", "src\Microsoft.AspNetCore.WebHooks.Receivers.Intercom\Microsoft.AspNetCore.WebHooks.Receivers.Intercom.csproj", "{920467E5-A7BD-4425-967E-32D9F411DE6C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntercomCoreReceiver", "samples\IntercomCoreReceiver\IntercomCoreReceiver.csproj", "{0F90B202-3D68-4EE2-B310-6D81A9440E22}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -249,6 +253,14 @@ Global
{F42E56B7-60D4-481A-8585-04015B2B501E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F42E56B7-60D4-481A-8585-04015B2B501E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F42E56B7-60D4-481A-8585-04015B2B501E}.Release|Any CPU.Build.0 = Release|Any CPU
{920467E5-A7BD-4425-967E-32D9F411DE6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{920467E5-A7BD-4425-967E-32D9F411DE6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{920467E5-A7BD-4425-967E-32D9F411DE6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{920467E5-A7BD-4425-967E-32D9F411DE6C}.Release|Any CPU.Build.0 = Release|Any CPU
{0F90B202-3D68-4EE2-B310-6D81A9440E22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0F90B202-3D68-4EE2-B310-6D81A9440E22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F90B202-3D68-4EE2-B310-6D81A9440E22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F90B202-3D68-4EE2-B310-6D81A9440E22}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -290,6 +302,8 @@ Global
{91E02D64-BA6E-473C-A250-D1051A50DDAB} = {9575CB90-BC4B-43BB-8AEA-82C53FDA4187}
{E9E37253-3FE1-44A4-A9E7-A2B6C54EECFE} = {E957C8D9-B4A0-488B-838F-BAB4DE080A76}
{F42E56B7-60D4-481A-8585-04015B2B501E} = {9575CB90-BC4B-43BB-8AEA-82C53FDA4187}
{920467E5-A7BD-4425-967E-32D9F411DE6C} = {929F44D0-A040-4DC3-A22F-4C5829C05D44}
{0F90B202-3D68-4EE2-B310-6D81A9440E22} = {E957C8D9-B4A0-488B-838F-BAB4DE080A76}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {12CE6238-F847-4984-8622-1ED46072150A}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebHooks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace IntercomCoreReceiver.Controllers
{
public class IntercomController : ControllerBase
{
private readonly ILogger _logger;

public IntercomController(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<IntercomController>();
}

[IntercomWebHook(Id = "It")]
public IActionResult IntercomForIt(string @event, string notificationId, JObject data)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

return Ok();
}

[IntercomWebHook]
public IActionResult Intercom(string id, string @event, string notificationId, JObject dataObj)
{

var data = dataObj.ToObject<IntercomNotification>();

if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

_logger.LogInformation(
0,
$"{nameof(IntercomController)} / '{{ReceiverId}}' received a '{{EventType}}' notification (event " +
"'{EventName}').",
id,
data.Type,
@event);

_logger.LogInformation(
1,
"Data created at '{Created}' and contains Notification ID '{Id}' / '{NotificationId}', Topic " +
"'{Topic}'.",
data.CreatedAt,
data.Id,
notificationId,
data.Topic);

var details = data.Data.Item;

return Ok();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFrameworks>$(StandardTestWebsiteTfms)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="$(MicrosoftAspNetCorePackageVersion)" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.WebHooks.Receivers.Intercom\Microsoft.AspNetCore.WebHooks.Receivers.Intercom.csproj" />
</ItemGroup>
</Project>
17 changes: 17 additions & 0 deletions src/WebHooks/samples/IntercomCoreReceiver/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace IntercomCoreReceiver
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
24 changes: 24 additions & 0 deletions src/WebHooks/samples/IntercomCoreReceiver/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace IntercomCoreReceiver
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvcCore()
.AddIntercomWebHooks();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc();
}
}
}
3 changes: 3 additions & 0 deletions src/WebHooks/samples/IntercomCoreReceiver/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"WebHooks:Intercom:SecretKey:default": "0123456789012345"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.ComponentModel;

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for setting up Intercom WebHooks in an <see cref="IMvcBuilder" />.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class IntercomMvcBuilderExtensions
{
/// <summary>
/// <para>
/// Add Intercom WebHook configuration and services to the specified <paramref name="builder"/>. See
/// <see href="https://developers.intercom.com/building-apps/docs/webhooks/"/> for additional details about Intercom WebHook requests.
/// </para>
/// <para>
/// The '<c>WebHooks:Intercom:SecretKey:default</c>' configuration value contains the secret key for Intercom
/// WebHook URIs of the form '<c>https://{host}/api/webhooks/incoming/intercom</c>'.
/// '<c>WebHooks:Intercom:SecretKey:{id}</c>' configuration values contain secret keys for Intercom WebHook URIs of
/// the form '<c>https://{host}/api/webhooks/incoming/intercom/{id}</c>'.
/// </para>
/// </summary>
/// <param name="builder">The <see cref="IMvcBuilder" /> to configure.</param>
/// <returns>The <paramref name="builder"/>.</returns>
public static IMvcBuilder AddIntercomWebHooks(this IMvcBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

IntercomServiceCollectionSetup.AddIntercomServices(builder.Services);

return builder.AddWebHooks();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.ComponentModel;

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for setting up Intercom WebHooks in an <see cref="IMvcCoreBuilder" />.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class IntercomMvcCoreBuilderExtensions
{
/// <summary>
/// <para>
/// Add Intercom WebHook configuration and services to the specified <paramref name="builder"/>. See
/// <see href="https://developers.intercom.com/building-apps/docs/webhooks"/> for additional details about Intercom WebHook requests.
/// </para>
/// <para>
/// The '<c>WebHooks:Intercom:SecretKey:default</c>' configuration value contains the secret key for Intercom
/// WebHook URIs of the form '<c>https://{host}/api/webhooks/incoming/intercom</c>'.
/// '<c>WebHooks:Intercom:SecretKey:{id}</c>' configuration values contain secret keys for Intercom WebHook URIs of
/// the form '<c>https://{host}/api/webhooks/incoming/intercom/{id}</c>'.
/// </para>
/// </summary>
/// <param name="builder">The <see cref="IMvcCoreBuilder" /> to configure.</param>
/// <returns>The <paramref name="builder"/>.</returns>
public static IMvcCoreBuilder AddIntercomWebHooks(this IMvcCoreBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

IntercomServiceCollectionSetup.AddIntercomServices(builder.Services);

return builder
.AddJsonFormatters()
.AddWebHooks();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.WebHooks.Filters;
using Microsoft.AspNetCore.WebHooks.Metadata;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Methods to add services for the GitHub receiver.
/// </summary>
internal static class IntercomServiceCollectionSetup
{
/// <summary>
/// Add services for the Intercom receiver.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to update.</param>
public static void AddIntercomServices(IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

WebHookMetadata.Register<IntercomMetadata>(services);
services.TryAddSingleton<IntercomVerifySignatureFilter>();
}
}
}
Loading