-
-
Notifications
You must be signed in to change notification settings - Fork 703
feat(notification): add support for Ntfy #1332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
app/src/views/preference/components/ExternalNotify/ntfy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// This file is auto-generated by notification generator. DO NOT EDIT. | ||
import type { ExternalNotifyConfig } from './types' | ||
|
||
const NtfyConfig: ExternalNotifyConfig = { | ||
name: () => $gettext('Ntfy'), | ||
config: [ | ||
{ | ||
key: 'server_url', | ||
label: 'Server URL', | ||
}, | ||
{ | ||
key: 'topic', | ||
label: 'Topic', | ||
}, | ||
{ | ||
key: 'priority', | ||
label: 'Priority(int, one of: 1, 2, 3, 4, 5)', | ||
}, | ||
{ | ||
key: 'tags', | ||
label: 'Tags(string array)', | ||
}, | ||
{ | ||
key: 'click', | ||
label: 'Click URL', | ||
}, | ||
{ | ||
key: 'actions', | ||
label: 'Actions(JSON array)', | ||
}, | ||
{ | ||
key: 'username', | ||
label: 'Username', | ||
}, | ||
{ | ||
key: 'password', | ||
label: 'Password', | ||
}, | ||
{ | ||
key: 'token', | ||
label: 'Token', | ||
0xJacky marked this conversation as resolved.
Show resolved
Hide resolved
0xJacky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
], | ||
} | ||
|
||
export default NtfyConfig |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package notification | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/0xJacky/Nginx-UI/model" | ||
"github.com/uozi-tech/cosy/map2struct" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
const ( | ||
DEFAULT_NTFY_PRIORITY = 3 | ||
DEFAULT_NTFY_ICON = "https://nginxui.com/assets/logo.svg" | ||
) | ||
|
||
// @external_notifier(Ntfy) | ||
type Ntfy struct { | ||
ServerURL string `json:"server_url" title:"Server URL"` | ||
Topic string `json:"topic" title:"Topic"` | ||
Priority string `json:"priority" title:"Priority"` | ||
Tags string `json:"tags" title:"Tags"` | ||
Click string `json:"click" title:"Click URL"` | ||
Actions string `json:"actions" title:"Actions"` | ||
Username string `json:"username" title:"Username"` | ||
Password string `json:"password" title:"Password"` | ||
Token string `json:"token" title:"Token"` | ||
} | ||
|
||
type NtfyMessage struct { | ||
Topic string `json:"topic,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
Title string `json:"title,omitempty"` | ||
Priority int `json:"priority,omitempty"` | ||
Tags []string `json:"tags,omitempty"` | ||
Click string `json:"click,omitempty"` | ||
Actions []interface{} `json:"actions,omitempty"` | ||
Icon string `json:"icon,omitempty"` | ||
} | ||
|
||
func init() { | ||
RegisterExternalNotifier("ntfy", func(ctx context.Context, n *model.ExternalNotify, msg *ExternalMessage) error { | ||
ntfyConfig := &Ntfy{} | ||
err := map2struct.WeakDecode(n.Config, ntfyConfig) | ||
if err != nil { | ||
return err | ||
} | ||
if ntfyConfig.ServerURL == "" || ntfyConfig.Topic == "" { | ||
return ErrInvalidNotifierConfig | ||
} | ||
|
||
// Convert priority string to int | ||
priority := DEFAULT_NTFY_PRIORITY | ||
if ntfyConfig.Priority != "" { | ||
p, err := strconv.Atoi(ntfyConfig.Priority) | ||
if err != nil { | ||
return fmt.Errorf("invalid priority: %w", err) | ||
} | ||
if p < 1 || p > 5 { | ||
return fmt.Errorf("invalid priority: must be between 1 and 5") | ||
} | ||
priority = p | ||
} | ||
|
||
// Prepare the message | ||
ntfyMsg := NtfyMessage{ | ||
Topic: ntfyConfig.Topic, | ||
Message: msg.GetContent(n.Language), | ||
Title: msg.GetTitle(n.Language), | ||
Priority: priority, | ||
Icon: DEFAULT_NTFY_ICON, | ||
Click: ntfyConfig.Click, | ||
} | ||
|
||
// Add tags if provided | ||
if ntfyConfig.Tags != "" { | ||
var tags []string | ||
if err := json.Unmarshal([]byte(ntfyConfig.Tags), &tags); err != nil { | ||
return fmt.Errorf("invalid tags: %w", err) | ||
} | ||
ntfyMsg.Tags = tags | ||
} | ||
|
||
// Add actions if provided | ||
if ntfyConfig.Actions != "" { | ||
var actions []interface{} | ||
if err := json.Unmarshal([]byte(ntfyConfig.Actions), &actions); err != nil { | ||
return fmt.Errorf("invalid actions: %w", err) | ||
} | ||
ntfyMsg.Actions = actions | ||
} | ||
|
||
// Create HTTP request | ||
jsonData, err := json.Marshal(ntfyMsg) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal ntfy message: %w", err) | ||
} | ||
req, err := http.NewRequestWithContext(ctx, "POST", ntfyConfig.ServerURL, bytes.NewBuffer(jsonData)) | ||
if err != nil { | ||
return fmt.Errorf("failed to create HTTP request: %w", err) | ||
} | ||
|
||
// Set headers | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("User-Agent", "Nginx-UI") | ||
if ntfyConfig.Token != "" { | ||
req.Header.Set("Authorization", "Bearer "+ntfyConfig.Token) | ||
} else if ntfyConfig.Username != "" && ntfyConfig.Password != "" { | ||
req.SetBasicAuth(ntfyConfig.Username, ntfyConfig.Password) | ||
} | ||
|
||
// Send request | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to send ntfy request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
// Check response status | ||
if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
return fmt.Errorf("ntfy request failed with status: %d", resp.StatusCode) | ||
} | ||
|
||
return nil | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.