Notifications - gonace/Cronitor GitHub Wiki
Create, read, update and delete notification lists (Notifications API).
A notification list says where alerts go: email addresses, phone numbers, Slack, PagerDuty and other integrations. Monitors name the lists they alert through in their Notify property; without it, a monitor uses the list with the key default.
Getting a client
Notification lists are managed through an INotificationsClient: CronitorClient.Notifications on the static class, injected after .ConfigureCronitor(), or created with new NotificationsClient("apiKey"). See Setup for each. The examples below use notificationsClient.
Methods
Each method has a sync and an async (…Async) version. All of them throw when Cronitor answers with an error.
| Method | Returns | Description |
|---|---|---|
ListAsync(int page = 1) |
ListNotificationResponse |
One page of notification lists |
GetAsync(string key) |
Notification |
One notification list |
CreateAsync(CreateNotificationRequest request) |
Notification |
Creates a notification list |
UpdateAsync(UpdateNotificationRequest request) |
Notification |
Updates a notification list |
DeleteAsync(string key) |
Deletes a notification list |
Notification and Notifications are in Cronitor.Models, the requests in Cronitor.Requests.
Create
using Cronitor.Models;
using Cronitor.Requests;
var list = new Notification
{
Key = "devops",
Name = "DevOps",
Notifications = new Notifications
{
Emails = new[] { "[email protected]" },
Slack = new[] { "https://hooks.slack.com/services/…" }
},
Environments = new[]
{
new NotificationEnvironment { Key = "production" }
}
};
var created = await notificationsClient.CreateAsync(new CreateNotificationRequest(list));
CreateNotificationRequest also takes the name, key and channels directly:
using Cronitor.Models;
using Cronitor.Requests;
var created = await notificationsClient.CreateAsync(new CreateNotificationRequest(
"DevOps",
"devops",
new Notifications { Emails = new[] { "[email protected]" } }));
Properties
Properties you can set:
| Property | Description |
|---|---|
Key |
The list's unique key, used in a monitor's Notify. Letters, numbers, dashes and underscores. |
Name |
The display name. |
Notifications |
Where alerts are sent, see Channels. At least one channel must be set. |
Environments |
The environments the list alerts for, each a NotificationEnvironment with a Key. Leave it empty to alert for every environment. |
Properties Cronitor sets, read from a list you get or list: Monitors (the keys of the monitors using the list), MonitorDetails (their keys and names), Status and CreatedAt. They're never sent back to Cronitor.
Channels
Notifications has one property per channel, each a list of strings:
| Property | Contains |
|---|---|
Emails |
Email addresses |
Phones |
Phone numbers, for SMS |
Slack |
Slack webhook URLs |
Webhooks |
URLs to call, starting with http:// or https:// |
Pagerduty |
PagerDuty keys |
Opsgenie |
Opsgenie integration labels |
Victorops |
VictorOps integration labels |
MicrosoftTeams |
Microsoft Teams integration labels |
Discord |
Discord integration labels |
GoogleChat |
Google Chat integration labels |
Telegram |
Telegram integration labels |
Lark |
Lark integration labels |
DatadogOnCall |
Datadog On-Call integration labels |
Hipchat |
HipChat integration labels |
The integration labels are those of the integrations set up in your Cronitor account. A notification list can't include another list, but a monitor can use several lists.
Get
var list = await notificationsClient.GetAsync("devops");
Console.WriteLine($"{list.Name} ({list.Key})");
foreach (var monitor in list.MonitorDetails)
{
Console.WriteLine($"Used by {monitor.Name}");
}
List
ListAsync returns one page of notification lists, 50 to a page. Items holds the lists, and Total the number of lists in your account:
var response = await notificationsClient.ListAsync(page: 2);
foreach (var list in response.Items)
{
Console.WriteLine($"{list.Key}: {list.Name}");
}
Update
UpdateNotificationRequest takes the list's key and the list as it should be. Get the list first, change it, and send it back:
using Cronitor.Models;
using Cronitor.Requests;
var list = await notificationsClient.GetAsync("devops");
list.Notifications.Emails = new[] { "[email protected]", "[email protected]" };
var updated = await notificationsClient.UpdateAsync(new UpdateNotificationRequest("devops", list));
Delete
await notificationsClient.DeleteAsync("devops");