Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a webhook in ASP.NET MVC?

I'm trying to create a simple webhook to receive a delivery receipt from Nexmo SMS service. The only documentation on their website was this.

During account set-up, you will be asked to supply Nexmo a CallBack URL for Delivery Receipt to which we will send a delivery receipt for each of your SMS submissions. This will confirm whether your message reached the recipient's handset. The request parameters are sent via a GET (default) to your Callback URL and Nexmo will be expecting response 200 OK response, or it will keep retrying until the Delivery Receipt expires (up to 72 hours).

I've been searching for ways to do this, and so far I have this method from an example I found online, although I'm not sure if this is correct. Anyways, this is being run on ASP.NET and on port 6563, so is this the port I'm supposed to be listening to? I downloaded an application called ngrok which should expose my local web server to the internet, so I ran the application and instructed it to listen onto port 6563, but no luck. I've been fiddling with it trying to find someway to post to this function.

[HttpPost]
public ActionResult CallbackURL()
{
    System.IO.StreamReader reader = new System.IO.StreamReader(HttpContext.Request.InputStream);
    string rawSendGridJSON = reader.ReadToEnd();
    return new HttpStatusCodeResult(200);
}

Usually I can call the function directly to return the view just by visiting http://localhost:6563/Home/Index/CallbackURL So I've inserted a breakpoint on the method signature, but it'll only get called if I remove the [HttpPost] from it. Any next steps that I should try?

like image 554
Dillon Drobena Avatar asked Oct 17 '14 03:10

Dillon Drobena


People also ask

Can I create my own webhook?

You can install webhooks on an organization or on a specific repository. To set up a webhook, go to the settings page of your repository or organization. From there, click Webhooks, then Add webhook. Alternatively, you can choose to build and manage a webhook through the Webhooks API.

How do you implement a webhook?

With webhooks, it's generally a three-step process: Get the webhook URL from the application you want to send data to. Use that URL in the webhook section of the application you want to receive data from. Choose the type of events you want the application to notify you about.


2 Answers

First you have to remove the [HttpPost] bit because it clearly states that "parameters are sent via a GET".

Then you should also remove the return HttpStatusCodeResult(200) as it will return the 200 OK status code anyway if no error occures.

Then you should simply read the values from querystring or using model binding. Here is a sample:

    public string CallbackURL()
    {
        string vals = "";

        // get all the sent data 
        foreach (String key in Request.QueryString.AllKeys)
            vals += key + ": " + Request.QueryString[key] + Environment.NewLine;

        // send all received data to email or use other logging mechanism
        // make sure you have the host correctly setup in web.config
        SmtpClient smptClient = new SmtpClient();
        MailMessage mailMessage = new MailMessage();
        mailMessage.To.Add("[email protected]");
        mailMessage.From = new MailAddress("[email protected]");
        mailMessage.Subject = "callback received";
        mailMessage.Body = "Received data: " + Environment.NewLine + vals;
        mailMessage.IsBodyHtml = false;
        smptClient.Send(mailMessage);

        // TODO: process data (save to database?)

        // disaplay the data (for degugging purposes only - to be removed)
        return vals.Replace(Environment.NewLine, "<br />");
    }
like image 96
SmartDev Avatar answered Sep 30 '22 11:09

SmartDev


Before couple of weeks Asp.Net team has announced to support Web Hooks with Visual Studio.

Please have a look here for more detailed information:

https://neelbhatt40.wordpress.com/2015/10/14/webhooks-in-asp-net-a-visual-studio-extension/

like image 28
Neel Avatar answered Sep 30 '22 12:09

Neel