Wednesday, 30 December 2015

WebHooks in Asp.Net : A Visual Studio Extension

Before couple of weeks Asp.Net team has announced to support Web Hooks with Visual Studio.
First of all let us see What is WebHooks?

WebHooks in web development is a method of augmenting or altering the behavior of a web page, or web application, with custom callbacks. These callbacks may be maintained, modified, and managed by third-party users and developers who may not necessarily be affiliated with the originating website or application.
In simple words WebHooks is a HTTP callback(user-defined HTTP Callback) which providing a simple pub/sub model for wiring together Web APIs and SaaS services.
Ok, above explanation is a kind of theoretical, can you tell some real time example?
Suppose you have a company account in Instagram! Now a new photo is tagged with tags of your choosing and you want to do some instant event like saying “Thank you!” or to answer if someone has asked anything or deletion of comments in case of some spamming!
WebHooks does it for you!
That is awesome! How to get WebHooks in Asp .Net?
You can install the extension directly in Visual Studio using the Tools -> “Extensions and Updates” dialog, as shown below. Simply search for “WebHooks” and you will see below screen:
After getting this Visual Studio extension you will see some wizard as shown below:
The above wizard is used to select which client you want to use.
Above wizard is used to enter secret number of client. It captures the secrets for each of them individually.
Now Let us go step by step with the example of Instagram I explained above. Let us code it out!!
We will take Instagram sample in which we will connect WebHooks to get the notification generated in Instagram account.
Instagram WebHooks support four kinds of subscriptions:
  • Users: receive notifications when users registered with your application post new photos.
  • Tags: receive notifications when a new photo is tagged with specific tags.
  • Locations: receive notifications when new photos are posted and tagged with a specific location.
  • Geographies: receive notifications when new photos are posted in a given geo location defined by a center point and radius.
First of all get secret number for your Instagram account as shown below:
which will be stored in your web.config file as below:
   <add key="MS_WebHookReceiverSecret_InstagramId" value="Instagram Client ID" /> 
   <add key="MS_WebHookReceiverSecret_Instagram" value="Instagram Client Secret" />
After installation you will see a WebHookConfig.cs class in your App_Code folder which would be created automatically. It is used to initialize the process as shown below:
namespace WebHookReceivers
   public static class WebHookConfig
      public static void Register(HttpConfiguration config)
           // Load receivers
Once you select the receiver as shown in figure, a class is added to the project that inherits from the WebHookHandler class, the base class for all WebHook handler customization you intend on doing.
Each receiver is identified by name, with the name being used in the route wire-up, so each generated handler checks to see if it’s the one who should be handling the incoming requests.
Instagram generated handler is below:
public class InstagramWebHookHandler : WebHookHandler
   public override Task ExecuteAsync(string receiver, WebHookHandlerContext context)
      string action = context.Actions.First();
      JObject data = context.GetDataOrDefault<JObject>();

      return Task.FromResult(true);

Now let us make some changes in above code as below:
public class InstagramWebHookHandler : WebHookHandler
   public InstagramWebHookHandler()
       this.Receiver = "instagram";
   public override async Task ExecuteAsync(string generator, WebHookHandlerContext context)
       // Get the WebHook client
       InstagramWebHookClient client = Dependencies.Client;
       // Convert the incoming data to a collection of InstagramNotifications
      var instagramNotifications = context.GetDataOrDefault<IEnumerable<InstagramNotification>>();
      foreach (var notification in instagramNotifications)
          var entries = await client.GetRecentGeoMedia(context.Id, notification.ObjectId);
          foreach (JToken entry in entries)
              // Different sizes of images
              var thumbnail = entry["images"]["thumbnail"].ToObject<InstagramMedia>(); 
In the case above we extract information about different image resolutions (i.e. thumbnail here). However, the information provide by Instagram is huge and we can extract much other information.
Here we have used InstagramWebHookClient to retrieve additional data and then extract information about the images posted.
In above code it gets the notification generated in Instagram for you and you can do different code as per your requirement.
I will update if something would be updated by .Net team.
In my next post I will write demo code for Github handler of WebHooks.(Post is available now)
Stay tuned for more updates.

No comments:

Post a Comment