-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds (unauthenticated) support for ntfy (https://ntfy.sh/). It should also support alternative ntfy servers (set using the NOTI_NTFY_URL environment variable) but I don't have an instance to test with. Topic to send notifications to is set with NOTI_NTFY_TOPIC.
- Loading branch information
Showing
7 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,76 @@ | ||
package ntfy | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
type apiResponse struct { | ||
// Message identifier | ||
Id string `json:"id"` | ||
|
||
// Message date time as Unix time stamp | ||
Time uint64 `json:"time"` | ||
|
||
// Unix time stamp for when the message will be deleted | ||
Expires uint64 `json:"expires"` | ||
|
||
// Message type | ||
Event string `json:"event"` | ||
|
||
// Topic ID | ||
Topic string `json:"topic"` | ||
|
||
// Message title | ||
Title string `json:"title"` | ||
|
||
// Message body | ||
Message string `json:"message"` | ||
} | ||
|
||
type Notification struct { | ||
// Base Ntfy URL | ||
URL string | ||
|
||
// Ntfy topic to publish to | ||
Topic string `json:"topic"` | ||
|
||
// Message body | ||
Message string `json:"message"` | ||
|
||
// Message title | ||
Title string `json:"title"` | ||
|
||
Client *http.Client `json:"-"` | ||
} | ||
|
||
func (n *Notification) Send() error { | ||
if n.URL == "" { | ||
return errors.New("missing Ntfy url") | ||
} | ||
|
||
if n.Topic == "" { | ||
return errors.New("missing topic id") | ||
} | ||
|
||
payload, err := json.Marshal(n) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := n.Client.Post(n.URL, "application/json", bytes.NewReader(payload)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
var r apiResponse | ||
if err := json.NewDecoder(resp.Body).Decode(&r); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains 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,58 @@ | ||
package ntfy | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestSend(t *testing.T) { | ||
n := Notification{ | ||
URL: "https://ntfy.sh/", | ||
Topic: "topic", | ||
Message: "Message body test", | ||
Title: "Test Message", | ||
Client: &http.Client{Timeout: 3 * time.Second}, | ||
} | ||
|
||
var mockResp apiResponse | ||
var hitServer bool | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
hitServer = true | ||
|
||
if r.Method != "POST" { | ||
t.Error("HTTP method should be POST") | ||
} | ||
|
||
var req Notification | ||
json.NewDecoder(r.Body).Decode(&req) | ||
|
||
if req.Topic == "" { | ||
t.Error("missing topic") | ||
} | ||
|
||
if req.Message == "" { | ||
t.Error("missing message") | ||
} | ||
|
||
if req.Title == "" { | ||
t.Error("missing title") | ||
} | ||
|
||
json.NewEncoder(rw).Encode(mockResp) | ||
})) | ||
defer ts.Close() | ||
|
||
n.URL = ts.URL | ||
|
||
if err := n.Send(); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if !hitServer { | ||
t.Error("didn't reach server") | ||
} | ||
} |