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?
A 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:
<appSettings> <add key="MS_WebHookReceiverSecret_InstagramId" value="Instagram Client ID" /> <add key="MS_WebHookReceiverSecret_Instagram" value="Instagram Client Secret" /> </appSettings>
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 config.InitializeReceiveInstagramWebHooks(); } } }
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.
This is awesome. Thank you for taking the time to write the article 🙂 cheers!
LikeLike
Thank you so much 😊
LikeLike
Nice passionate work Neel!!
LikeLike
Thank you so much 🙂
LikeLike
I’m not seeing the Extension. I’ve downloaded all the Nuget packages. Is that preventing the Extension from showing up?
LikeLike
Hi Adamburns, Sorry but i did not get which all Nuget packages you downloaded. You require only which is shown in above post.
LikeLike
Lots of broken images, any chance for having these updated?
LikeLike
Nevermind… had to be logged in to see them :S
LikeLike
Hey! Do you use Twitter? I’d like to follow you if that would be ok. I’m undoubtedly enjoying your blog and look forward to new updates.
LikeLike
Hello.. Any idea on using webhooks with Helpscout using ASP.NET ?
LikeLike
Hey, maybe this will help: https://github.com/mathieukempe/HelpScoutNet
LikeLike
Thanks Neel, but this doesnts include any help on webhooks. It just has code to read entire conversation lists etc. We need to get event notifications for conversation created/updated etc in our custom app(based on ASP.NET)
LikeLike
Okay. Unfortunately, I have never played with HelpScout so I am not aware of what you are asking for. I will update here if I find anything useful.
LikeLike
Hi Neel , I’m not able to see the uploaded images
LikeLike
Hello Ajith, I guess there is something wrong with the post. I will check, thanks for pointing it.
LikeLike