Skip to content

.NET SDK for Novu - The open-source notification infrastructure for engineers. πŸš€

License

Notifications You must be signed in to change notification settings

Badabum/novu-dotnet

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

51 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

novu-dotnet

NuGet NuGet Deploy to Nuget

.NET SDK for Novu - The open-source notification infrastructure for engineers. πŸš€

novu-dotnet targets .NET Standard 2.0 and is compatible with .NET Core 2.0+ and .NET Framework 4.6.1+.

Installation

dotnet add package Novu
using Novu.DTO;
using Novu.Models;
using Novu;
...

 var novuConfiguration = new NovuClientConfiguration
{
    // Defaults to https://api.novu.co/v1
    Url = "https://novu-api.my-domain.com/v1",
    ApiKey = "12345",
};

var novu = new NovuClient(novuConfiguration);

var subscribers = await novu.Subscribers.GetSubscribers();

Examples

Novu Client

using Novu.DTO;
using Novu.Models;
using Novu;
...

var config = new NovuClientConfiguration
{
  ApiKey = "my-key",
};

var client = new NovuClient(config);

Subscribers

Get Subscribers

using Novu.DTO;
using Novu.Models;
using Novu;
...

var subscribers = await client.Subscriber.GetSubscribers()

Get Subscriber

using Novu.DTO;
using Novu.Models;
using Novu;
...

var subscriber = await client.Subscriber.GetSubscriber("subscriberId");

Create Subscriber

using Novu.DTO;
using Novu.Models;
using Novu;
...

var additionalData = new List<AdditionalDataDto>
{
  new AdditionalDataDto
  {
    Key = "External ID",
    Value = "1122334455"
  },
  {
    Key = "SMS_PREFERENCE",
    Value = "EMERGENT_ONLY"
  }
};

var newSubscriberDto = new CreateSubscriberDto
{
  SubscriberId = Guid.NewGuid().ToString(),
  FirstName = "John",
  LastName = "Doe",
  Avatar = "https://unslpash.com/random",
  Email = "jdoe@novu.co",
  Locale = "en-US",
  Phone = "+11112223333",
  Data = additionalData
};

var subscriber = await client.Subcriber.CreateSubscriber()

Update Subscriber

using Novu.DTO;
using Novu.Models;
using Novu;
...

var subscriber = client.Subscriber.GetSubscriber("subscriberId");

subscriber.FirstName = "Updated";
subscriber.LastName = "Subscriber";

var updatedSubscriber = await client.Subscriber.UpdateSubscriber(subscriber);

Delete Subscriber

using Novu.DTO;
using Novu.Models;
using Novu;
...

var deleted = await client.Subscriber.DeleteSubscriber("subscriberId");

Update Online Status

using Novu.DTO;
using Novu.Models;
using Novu;
...

var onlineDto = new SubscriberOnlineDto
{
  IsOnline = true
};

var online = await client.Subscriber.UpdateOnlineStatus("subscriberId", onlineDto);

Events

Trigger

using Novu.DTO; using Novu.Models; using Novu; ...

// OnboardEventPayload.cs
public class OnboardEventPayload
{
  [JsonProperty("username")]
  public string Username { get; set; }

  [JsonProperty("welcomeMessage")]
  public string WelcomeMessage { get; set; }
}

// MyFile.cs
var onboardingMessage = new OnboardEventPayload
{
  Username = "jdoe",
  WelcomeMessage = "Welcome to novu-dotnet"
};

var payload = new EventTriggerDataDto()
{
  EventName = "onboarding",
  To = { SubscriberId = "subscriberId" },
  Payload = onboardingMessage
};

var trigger = await client.Event.Trigger(payload);

if (trigger.TriggerResponsePayloadDto.Acknowledged)
{
  Console.WriteLine("Trigger has been created.");
}

Trigger Bulk

using Novu.DTO;
using Novu.Models;
using Novu;
...

var payload = new List<EventTriggerDataDto>()
{
    new()
    {
        EventName = "test",
        To = { SubscriberId = subscriber.SubscriberId},
        Payload = new TestRecord(){ Message = "Hello"}
    },
    new()
    {
        EventName = "test",
        To = { SubscriberId = subscriber.SubscriberId},
        Payload = new TestRecord(){ Message = "World"}
    },
};

var trigger = await client.Event.TriggerBulkAsync(payload);

Broadcast Trigger

using Novu.DTO;
using Novu.Models;
using Novu;
...

var testRecord = new TestRecord
{
    Message = "This is a test message"
};

var dto = new EventTriggerDataDto()
{
    EventName = "test",
    To =
    {
        SubscriberId = subscriber.SubscriberId
    },
    Payload = testRecord
};

var trigger = await client.Event.TriggerBroadcastAsync(dto);

Cancel Trigger

using Novu.DTO;
using Novu.Models;
using Novu;
...

var cancelled = await client.Event.TriggerCancelAsync("triggerTransactionId");

Topics

// Get Topic

var result = await client.Topic.GetTopicAsync("my-topic");

// Get Topics

var results = await client.Topic.GetTopicsAsync();

// Create Topic
var topicRequest = new TopicCreateDto
{
    Key = $"test:topic:{Guid.NewGuid().ToString()}",
    Name = "Test Topic",
    
};

var topic = await client.Topic.CreateTopicAsync(topicRequest);


// Add Subscriber to Topic
var subscriberList = new TopicSubscriberUpdateDto
{
    Keys = new List<string>
    {
        "test:subscriber:1",
    }
};

var result = await client.Topic.AddSubscriberAsync(topic.Data.Key, subscriberList);

// Remove Subscriber from Topic

var subscriberList = new TopicSubscriberUpdateDto
{
    Keys = new List<string>
    {
        "test:subscriber:1",
    }
};

var result = await client.Topic.RemoveSubscriberAsync(topic.Data.Key, subscriberList);

// Delete Topic

await client.Topic.DeleteTopicAsync("my-topic");

// Rename Topic

var topicRequest = new TopicRenameDto
{
    Name = "New Name"
};

var result = await client.Topic.RenameTopicAsync("my-topic", topicRequest);

Repository Overview

All code is located under the src directory and this will service as the project root.

Novu is the main SDK with Novu.Tests housing all unit tests.

novu-dotnet

  • Clients directory holds all clients that house the actual implementation of the various API calls.
  • DTO directory holds all Data Transfer Objects
  • Exceptions directory holds the custom exception creations
  • Interfaces directory holds all interfaces that are inteded to outline how a class should be structured.
  • Models directory holds various models
  • Utilities directory holds various utility functions used around the project.

About

.NET SDK for Novu - The open-source notification infrastructure for engineers. πŸš€

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%