question

Ivie O avatar image
Ivie O asked Ivie O commented

Webhook notification callback C#

Hi Phong, my webhook is setup and working, I can see event notifications in the webhook server console. My question is how do I retrieve the notification payload on the client so I can handle it? SubcriptionInfo (type returned by Subscription.Post(new CreateNew SubscriptionRequest{...})) does not have a Response property. See code snippet from client below:

rest api
1570621022651.png (29.2 KiB)
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image
Phong Vu answered

The code you posted above is just for subscribing for the notification. The Webhook callback is running on the server code below and you can parse the body as shown in bold text:

using System;
using System.IO;
using System.Text;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;

namespace webhook_server
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment()) app.UseDeveloperExceptionPage();

            app.Run( async (context) =>
            {
                context.Request.Headers.TryGetValue("Validation-Token", out StringValues validationToken);
                context.Response.Headers.Add("Validation-Token", validationToken);
                if (context.Request.Path == "/webhook" && context.Request.Method == "POST")
                {
                    using (StreamReader reader = new StreamReader(context.Request.Body, Encoding.UTF8))
                    {
                        var str = reader.ReadToEnd();
                        Console.WriteLine(str);
                    }
                }
            });
        }
    }
}


1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Ivie O avatar image
Ivie O answered Ivie O commented

My understanding is that the server code you show above gets called once, when the subscription request is created. From what I can see (through breakpoints), the server reads the request object from the client. Are you saying the notifications are also stored in context.response on the webhook server?

4 comments
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image Phong Vu ♦♦ commented ·

Correct, the server gets called immediately after you subscribed to validate the token. You must keep the server running so it will get called again when there is a notification.

0 Likes 0 ·
Ivie O avatar image Ivie O Phong Vu ♦♦ commented ·

So is there a way to return context,request.body (the notification payload) back to my client so that I can handle the event there, (in other words, where the subscription was requested)? It seems to me that the server should not be concerned with specific event handling or how the payload will be used.

0 Likes 0 ·
Phong Vu avatar image Phong Vu ♦♦ Ivie O commented ·

Webhooks cannot push anything directly to your browser client app. That is a common implementation for client/server communications for different Web frameworks. You can check out WebSockets. Read the answers of these threads from stackoverflow

https://stackoverflow.com/questions/34586733/sending-a-value-from-server-to-client-with-sockets

https://stackoverflow.com/questions/12304960/implementing-a-client-side-webhook-handler

Or, you can turn to use the PubNub notification.

0 Likes 0 ·
Show more comments

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys