NTFY integration

This commit is contained in:
Jan Zípek 2026-04-11 20:02:08 +02:00
parent 8a52975adb
commit 7a659cc044
Signed by: kamen
GPG Key ID: A17882625B33AC31
6 changed files with 225 additions and 3 deletions

View File

@ -20,6 +20,8 @@ type FormValues = {
type: string type: string
telegramApiKey?: string telegramApiKey?: string
telegramTargetChannel?: number telegramTargetChannel?: number
ntfyEndpoint?: string
ntfyAuthToken?: string
} }
export const ContactPointFormModal = ({ export const ContactPointFormModal = ({
@ -45,6 +47,13 @@ export const ContactPointFormModal = ({
} }
} }
if (v.type === 'ntfy') {
return {
endpoint: v.ntfyEndpoint,
authToken: v.ntfyAuthToken,
}
}
return {} return {}
} }
@ -56,6 +65,10 @@ export const ContactPointFormModal = ({
telegramApiKey: parsedTypeConfig.apiKey ?? '', telegramApiKey: parsedTypeConfig.apiKey ?? '',
telegramTargetChannel: parsedTypeConfig.targetChannel, telegramTargetChannel: parsedTypeConfig.targetChannel,
}), }),
...(parsedTypeConfig?.type === 'ntfy' && {
ntfyEndpoint: parsedTypeConfig.endpoint ?? '',
ntfyAuthToken: parsedTypeConfig.authToken ?? '',
}),
}), }),
onSubmit: async (v) => { onSubmit: async (v) => {
if (isLoading) { if (isLoading) {
@ -105,6 +118,7 @@ export const ContactPointFormModal = ({
<label>Type</label> <label>Type</label>
<select required {...register('type')}> <select required {...register('type')}>
<option value="telegram">Telegram</option> <option value="telegram">Telegram</option>
<option value="ntfy">Ntfy</option>
</select> </select>
</div> </div>
@ -132,6 +146,26 @@ export const ContactPointFormModal = ({
</> </>
)} )}
{type === 'ntfy' && (
<>
<div className="input">
<label>Endpoint</label>
<input
type="text"
minLength={1}
required
placeholder="https://ntfy.sh"
{...register('ntfyEndpoint')}
/>
</div>
<div className="input">
<label>Auth Token (optional)</label>
<input type="text" {...register('ntfyAuthToken')} />
</div>
</>
)}
<div className="actions"> <div className="actions">
<button <button
className="test" className="test"

View File

@ -7,7 +7,15 @@ type ContactPointTelegramConfig = {
targetChannel: number targetChannel: number
} }
export type ParsedContactPointConfig = ContactPointTelegramConfig type ContactPointNtfyConfig = {
type: 'ntfy'
endpoint: string
authToken?: string
}
export type ParsedContactPointConfig =
| ContactPointTelegramConfig
| ContactPointNtfyConfig
export const tryParseContactPointConfig = ( export const tryParseContactPointConfig = (
contactPoint: ContactPointInfo contactPoint: ContactPointInfo
@ -26,5 +34,13 @@ export const tryParseContactPointConfig = (
} }
} }
if (contactPoint.type === 'ntfy') {
return {
type: 'ntfy',
endpoint: data.endpoint,
authToken: data.authToken,
}
}
return null return null
} }

158
server/integrations/ntfy.go Normal file
View File

@ -0,0 +1,158 @@
package integrations
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
)
type NtfyIntegration struct{}
var ntfyHTTPClient = &http.Client{
Timeout: 15 * time.Second,
}
type NtfyNotificationChannelConfig struct {
Endpoint string `json:"endpoint"`
AuthToken string `json:"authToken"`
}
func (s NtfyIntegration) ProcessEvent(evt *ContactPointEvent, rawConfig string) error {
config := NtfyNotificationChannelConfig{}
err := json.Unmarshal([]byte(rawConfig), &config)
if err != nil {
return fmt.Errorf("failed to parse ntfy integration config - %w", err)
}
if err := s.validate(config); err != nil {
return err
}
switch evt.Type {
case ContactPointEventAlertTriggered:
data := evt.AlertTriggeredEvent
if data.SensorValueCondition != nil {
text := fmt.Sprintf("%s is at {value}", data.Sensor.Name)
if data.Alert.CustomMessage != "" {
text = data.Alert.CustomMessage
}
text = strings.Replace(text, "{value}", strconv.FormatFloat(data.LastValue, 'f', -1, 64), -1)
return s.send(config, "Alert Triggered", text)
}
if data.SensorLastContactCondition != nil {
text := fmt.Sprintf(
"%s has not reported in for last %s %s",
data.Sensor.Name,
strconv.FormatFloat(data.SensorLastContactCondition.Value, 'f', -1, 64),
data.SensorLastContactCondition.ValueUnit,
)
if data.Alert.CustomMessage != "" {
text = data.Alert.CustomMessage
}
return s.send(config, "Alert Triggered", text)
}
return nil
case ContactPointEventAlertResolved:
data := evt.AlertResolvedEvent
if data.SensorValueCondition != nil {
text := fmt.Sprintf("%s is at {value}", data.Sensor.Name)
if data.Alert.CustomResolvedMessage != "" {
text = data.Alert.CustomResolvedMessage
}
text = strings.Replace(text, "{value}", strconv.FormatFloat(data.LastValue, 'f', -1, 64), -1)
return s.send(config, "Alert Resolved", text)
}
if data.SensorLastContactCondition != nil {
text := fmt.Sprintf(
"%s has reported in last %s %s",
data.Sensor.Name,
strconv.FormatFloat(data.SensorLastContactCondition.Value, 'f', -1, 64),
data.SensorLastContactCondition.ValueUnit,
)
if data.Alert.CustomResolvedMessage != "" {
text = data.Alert.CustomResolvedMessage
}
return s.send(config, "Alert Resolved", text)
}
return nil
case ContactPointEventTest:
return s.send(config, "Test", "Test message from Basic Sensor Receiver")
}
return nil
}
func (s NtfyIntegration) ValidateConfig(rawConfig string) error {
config := NtfyNotificationChannelConfig{}
err := json.Unmarshal([]byte(rawConfig), &config)
if err != nil {
return err
}
return s.validate(config)
}
func (s NtfyIntegration) validate(config NtfyNotificationChannelConfig) error {
if strings.TrimSpace(config.Endpoint) == "" {
return fmt.Errorf("ntfy endpoint is required")
}
return nil
}
func (s NtfyIntegration) send(config NtfyNotificationChannelConfig, title string, text string) error {
url := strings.TrimSpace(config.Endpoint)
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(text))
if err != nil {
return fmt.Errorf("failed to create ntfy request - %w", err)
}
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
req.Header.Set("Title", title)
if config.AuthToken != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", config.AuthToken))
}
resp, err := ntfyHTTPClient.Do(req)
if err != nil {
return fmt.Errorf("failed to send ntfy notification - %w", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
body, readErr := io.ReadAll(resp.Body)
if readErr != nil {
return fmt.Errorf("ntfy returned status %d", resp.StatusCode)
}
return fmt.Errorf("ntfy returned status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
}
return nil
}

View File

@ -9,12 +9,12 @@ import (
type postOrPutContactPointsBody struct { type postOrPutContactPointsBody struct {
Name string `json:"name" binding:"required"` Name string `json:"name" binding:"required"`
Type string `json:"type" binding:"required,oneof=telegram"` Type string `json:"type" binding:"required,oneof=telegram ntfy"`
TypeConfig string `json:"typeConfig" binding:"required"` TypeConfig string `json:"typeConfig" binding:"required"`
} }
type testContactPointBody struct { type testContactPointBody struct {
Type string `json:"type" binding:"required,oneof=telegram"` Type string `json:"type" binding:"required,oneof=telegram ntfy"`
TypeConfig string `json:"typeConfig" binding:"required"` TypeConfig string `json:"typeConfig" binding:"required"`
} }

View File

@ -62,6 +62,14 @@ func (s *ContactPointsService) Create(name string, contactType string, contactTy
return nil, fmt.Errorf("failed to validate telegram config - %w", err) return nil, fmt.Errorf("failed to validate telegram config - %w", err)
} }
} }
case "ntfy":
{
err := s.ctx.Integrations.Ntfy.ValidateConfig(contactTypeConfig)
if err != nil {
return nil, fmt.Errorf("failed to validate ntfy config - %w", err)
}
}
} }
item := ContactPointItem{ item := ContactPointItem{
@ -144,6 +152,8 @@ func (channel *ContactPointItem) getService(ctx *Context) (integrations.ContactP
switch channel.Type { switch channel.Type {
case "telegram": case "telegram":
return ctx.Integrations.Telegram, nil return ctx.Integrations.Telegram, nil
case "ntfy":
return ctx.Integrations.Ntfy, nil
} }
return nil, errors.New("unknown channel type") return nil, errors.New("unknown channel type")
@ -153,6 +163,8 @@ func (channel *ContactPointItem) getTestService(ctx *Context) (integrations.Cont
switch channel.Type { switch channel.Type {
case "telegram": case "telegram":
return ctx.Integrations.Telegram, nil return ctx.Integrations.Telegram, nil
case "ntfy":
return ctx.Integrations.Ntfy, nil
} }
return nil, errors.New("unknown channel type") return nil, errors.New("unknown channel type")

View File

@ -22,6 +22,7 @@ type Services struct {
type Integrations struct { type Integrations struct {
Telegram *integrations.TelegramIntegration Telegram *integrations.TelegramIntegration
Ntfy *integrations.NtfyIntegration
MQTT *integrations.MQTTIntegration MQTT *integrations.MQTTIntegration
} }
@ -50,6 +51,7 @@ func InitializeServices(ctx *Context) *Services {
ctx.Integrations = &Integrations{} ctx.Integrations = &Integrations{}
ctx.Integrations.Telegram = &integrations.TelegramIntegration{} ctx.Integrations.Telegram = &integrations.TelegramIntegration{}
ctx.Integrations.Ntfy = &integrations.NtfyIntegration{}
ctx.Integrations.MQTT = &integrations.MQTTIntegration{} ctx.Integrations.MQTT = &integrations.MQTTIntegration{}
return &services return &services