// The Ballerina WebSub Subscriber service, which could be used as a WebHook Listener for GitHub.
import ballerina/websub;
import ballerina/io;

// Annotation-based configurations specifying the subscription parameters.
@websub:SubscriberServiceConfig {
    target: [
        "https://api.github.com/hub", 
        "https://github.com/<YOUR_ORGANIZATION>/<REPOSITORY>/events/push.json"
    ],
    secret: "<YOUR_SECRET_KEY>",
    httpConfig: {
        auth: {
            token: "<YOUR_AUTH_TOKEN>"
        }
    }
}
service /subscriber on new websub:Listener(9090) {
    // Defines the remote function that accepts the event notification request for the WebHook.
    remote function onEventNotification(
                    websub:ContentDistributionMessage event) returns error? {
        var retrievedContent = event.content;
        if (retrievedContent is json) {
            if (retrievedContent.zen is string) {
                int hookId = check retrievedContent.hook_id;
                int senderId = check retrievedContent.sender.id;
                io:println(string`PingEvent received for webhook [${hookId}]`);
                io:println(string`Event sender [${senderId}]`);
            } else if (retrievedContent.ref is string) {
                string repositoryName = 
                            check retrievedContent.repository.name;
                string lastUpdatedTime = 
                            check retrievedContent.repository.updated_at;
                io:println(string`PushEvent received for [${repositoryName}]`);
                io:println(string`Last updated at ${lastUpdatedTime}`);
            }
        } else {
            io:println("Unrecognized content type, hence ignoring");
        }
    }
}

Websub Subscriber Service

Ballerina provides the capability to easily introduce subscriber services that are WebSub-compliant. Ballerina WebSub subscribers can specify the topic and hub to which they wish to subscribe

import ballerina/websub;
import ballerina/io;

The Ballerina WebSub Subscriber service, which could be used as a WebHook Listener for GitHub.

@websub:SubscriberServiceConfig {
    target: [
        "https://api.github.com/hub", 
        "https://github.com/<YOUR_ORGANIZATION>/<REPOSITORY>/events/push.json"
    ],
    secret: "<YOUR_SECRET_KEY>",
    httpConfig: {
        auth: {
            token: "<YOUR_AUTH_TOKEN>"
        }
    }
}
service /subscriber on new websub:Listener(9090) {

Annotation-based configurations specifying the subscription parameters.

    remote function onEventNotification(
                    websub:ContentDistributionMessage event) returns error? {
        var retrievedContent = event.content;
        if (retrievedContent is json) {
            if (retrievedContent.zen is string) {
                int hookId = check retrievedContent.hook_id;
                int senderId = check retrievedContent.sender.id;
                io:println(string`PingEvent received for webhook [${hookId}]`);
                io:println(string`Event sender [${senderId}]`);
            } else if (retrievedContent.ref is string) {
                string repositoryName = 
                            check retrievedContent.repository.name;
                string lastUpdatedTime = 
                            check retrievedContent.repository.updated_at;
                io:println(string`PushEvent received for [${repositoryName}]`);
                io:println(string`Last updated at ${lastUpdatedTime}`);
            }
        } else {
            io:println("Unrecognized content type, hence ignoring");
        }
    }
}

Defines the remote function that accepts the event notification request for the WebHook.

bal run websub_webhook_sample.bal
time = 2021-03-15 15:43:00,198 level = INFO  module = ballerina/websub message = "HTTPS is recommended but using HTTP" 
time = 2021-03-15 15:43:01,960 level = INFO  module = ballerina/websub message = "Subscription request considered successful for non 202 status code: 204" 
time = 2021-03-15 15:43:01,962 level = INFO  module = ballerina/websub message = "Subscription Request successfully sent to Hub[https://api.github.com/hub], for Topic[https://github.com/<your-org>/<your-repository/events/push.json], with Callback [<your-callback-url>]. Awaiting intent verification." 
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
PingEvent received for webhook [287075824]
Event sender [77491511]
PushEvent received for [Hello-World]
Last updated at 2021-03-15T15:43:01Z