Instantly share code, notes, and snippets.

Embed
What would you like to do?
Webhooks with ASP .NET Core at the example of Infusionsoft
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace Tools.Infusionsoft.Hook
{
[Route("api/[controller]")]
public class InfusionsoftController : Controller
{
public const string HeaderSecretName = "X-Hook-Secret";
[HttpPost("[action]")]
public async Task<ActionResult> HookCallback([FromQuery] string account)
{
var body = await new StreamReader(Request.Body).ReadToEndAsync();
var infEvent = JsonConvert.DeserializeObject<InfusionsoftEvent>(body);
if (Request.Headers.ContainsKey(HeaderSecretName))
{
//this is the verification call from Infusionsoft
var secret = Request.Headers[HeaderSecretName];
Reponse.Headers.Append(HeaderSecretName, secret);
Debug.WriteLine($"Hook verification for Account: {account}, Event: {infEvent.EventKey}, Secret: {secret}");
}
else
{
//the event (hook) is actually happening
Debug.WriteLine(
$"Hook event '{infEvent.EventKey}' for Account: {account}, Object: {infEvent.ObjectType}, Object keys: {JsonConvert.SerializeObject(infEvent.ObjectKeys)}");
}
return Ok();
}
}
}
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Tools.Infusionsoft.Hook
{
public class InfusionsoftEvent
{
[JsonProperty("event_Key")]
public string EventKey { get; set; }
[JsonProperty("object_type")]
public string ObjectType { get; set; }
[JsonProperty("object_keys")]
public List<Dictionary<string, string>> ObjectKeys { get; set; }
}
}
using Newtonsoft.Json;
namespace Tools.Infusionsoft.Hook
{
public class InfusionsoftHook
{
[JsonProperty("eventKey")]
public string EventKey { get; set; }
[JsonProperty("hookUrl")]
public string HookUrl { get; set; }
[JsonProperty("key")]
public int Key { get; set; }
/// <summary>
/// Can be Unverified, Verified or Inactive
/// </summary>
[JsonProperty("status")]
public string Status { get; set; }
}
}
using Newtonsoft.Json;
namespace Tools.Infusionsoft.Hook
{
public class InfusionsoftHookRequest
{
[JsonProperty("eventKey")]
public string EventKey { get; set; }
[JsonProperty("hookUrl")]
public string HookUrl { get; set; }
}
}
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using ServiceStack;
namespace Tools.Infusionsoft.Hook
{
public class WebhookCreator
{
private string _account = "bx000.infusionsoft.com";
private string _accessToken = "abcdefg";
private string _infusionsoftUrl = "https://api.infusionsoft.com/crm/rest/v1/hooks";
private string _callbackUrl = "http://www.mypage.com/api/infusionsoft/hookcallback";
public void CreateWebhook()
{
var hookObj = new InfusionsoftHookRequest
{
EventKey = "contact.edit",
HookUrl = $"{_callbackUrl}?account={_account}"
};
var data = JsonConvert.SerializeObject(hookObj);
var request = (HttpWebRequest)WebRequest.Create(_infusionsoftUrl);
request.Method = "POST";
request.KeepAlive = true;
request.ContentType = "application/json";
var dataBytes = Encoding.UTF8.GetBytes(data);
using (var reqStream = request.GetRequestStream())
{
reqStream.Write(dataBytes, 0, dataBytes.Length);
}
request.Headers[HttpRequestHeader.Authorization] = $"Bearer {_accessToken}";
try
{
string resultJson;
using (var response = request.GetResponse())
{
var sr = new StreamReader(response.GetResponseStream());
resultJson = response.ReadToEnd();
sr.Close();
}
var hookResult = JsonConvert.DeserializeObject<InfusionsoftHook>(resultJson);
Debug.WriteLine($"Created Hook {hookResult.EventKey}, Status: {hookResult.Status}");
}
catch (Exception e)
{
Debug.WriteLine($"Error creating webhook: {e.Message}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment