-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add notification package with various notification providers
- Loading branch information
Showing
7 changed files
with
235 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package notification | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
type ApprisePayload struct { | ||
AppriseUrl string | ||
} | ||
|
||
type Apprise struct { | ||
Payload ApprisePayload | ||
} | ||
|
||
func (a *Apprise) Send(title string, message string) error { | ||
cmd := exec.Command("apprise", "-vv", "-b", message, a.Payload.AppriseUrl) | ||
if title != "" { | ||
cmd.Args = append(cmd.Args, "-t", title) | ||
} | ||
output, err := cmd.Output() | ||
if err != nil { | ||
return err | ||
} | ||
if string(output) == "ERROR: maybe apprise not found" { | ||
return fmt.Errorf("ERROR: maybe apprise not found") | ||
} | ||
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,37 @@ | ||
package notification | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type DingTalkPayload struct { | ||
Webhook string | ||
Message string | ||
} | ||
|
||
type DingTalk struct { | ||
Payload DingTalkPayload | ||
} | ||
|
||
func (d DingTalk) Send(title string, message string) error { | ||
webhook := d.Payload.Webhook | ||
|
||
sendMessageUrl := webhook | ||
sendMessageBody := map[string]interface{}{ | ||
"msgtype": "text", | ||
"text": map[string]string{ | ||
"content": fmt.Sprintf("%s\n%s", title, message), | ||
}, | ||
} | ||
jsonBody, _ := json.Marshal(sendMessageBody) | ||
resp, err := http.Post(sendMessageUrl, "application/json", bytes.NewBuffer(jsonBody)) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
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,43 @@ | ||
package notification | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type IFTTTPayload struct { | ||
Key string | ||
Event string | ||
Value1 string | ||
Value2 string | ||
Value3 string | ||
} | ||
|
||
type IFTTT struct { | ||
Payload IFTTTPayload | ||
} | ||
|
||
func (i IFTTT) Send(title string, message string) error { | ||
key := i.Payload.Key | ||
event := i.Payload.Event | ||
value1 := i.Payload.Value1 | ||
value2 := i.Payload.Value2 | ||
value3 := i.Payload.Value3 | ||
|
||
sendMessageUrl := fmt.Sprintf("https://maker.ifttt.com/trigger/%s/with/key/%s", event, key) | ||
sendMessageBody := map[string]string{ | ||
"value1": value1, | ||
"value2": value2, | ||
"value3": value3, | ||
} | ||
jsonBody, _ := json.Marshal(sendMessageBody) | ||
resp, err := http.Post(sendMessageUrl, "application/json", bytes.NewBuffer(jsonBody)) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
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,13 @@ | ||
package notification | ||
|
||
type NotificationProvider interface { | ||
Send(title string, message string) error | ||
} | ||
|
||
type Notification struct { | ||
Provider NotificationProvider | ||
} | ||
|
||
func (n *Notification) Send(title string, message string) error { | ||
return n.Provider.Send(title, message) | ||
} |
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,37 @@ | ||
package notification | ||
|
||
import ( | ||
"net/smtp" | ||
"strconv" | ||
) | ||
|
||
type SMTPPayload struct { | ||
Hostname string | ||
Port int | ||
Security bool | ||
IgnoreTLS bool | ||
Username string | ||
Password string | ||
From string | ||
To string | ||
Cc string | ||
Bcc string | ||
} | ||
|
||
type SMTP struct { | ||
Payload SMTPPayload | ||
} | ||
|
||
func (s *SMTP) Send(title string, message string) error { | ||
auth := smtp.PlainAuth("", s.Payload.Username, s.Payload.Password, s.Payload.Hostname) | ||
to := []string{s.Payload.To} | ||
msg := []byte("To: " + s.Payload.To + "\r\n" + | ||
"Subject: " + title + "\r\n" + | ||
"\r\n" + | ||
message + "\r\n") | ||
err := smtp.SendMail(s.Payload.Hostname+":"+strconv.Itoa(s.Payload.Port), auth, s.Payload.From, to, msg) | ||
if 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,39 @@ | ||
package notification | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type TelegramPayload struct { | ||
BotToken string | ||
ChatId string | ||
} | ||
|
||
type Telegram struct { | ||
Payload TelegramPayload | ||
} | ||
|
||
func (t Telegram) Send(title string, message string) error { | ||
botToken := t.Payload.BotToken | ||
chatId := t.Payload.ChatId | ||
|
||
text := fmt.Sprintf("%s\n%s", title, message) | ||
|
||
sendMessageUrl := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", botToken) | ||
sendMessageBody := map[string]string{ | ||
"chat_id": chatId, | ||
"text": text, | ||
"parse_mode": "HTML", | ||
} | ||
jsonBody, _ := json.Marshal(sendMessageBody) | ||
resp, err := http.Post(sendMessageUrl, "application/json", bytes.NewBuffer(jsonBody)) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
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,37 @@ | ||
package notification | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type WeComPayload struct { | ||
Key string | ||
Message string | ||
} | ||
|
||
type WeCom struct { | ||
Payload WeComPayload | ||
} | ||
|
||
func (w WeCom) Send(title string, message string) error { | ||
key := w.Payload.Key | ||
|
||
sendMessageUrl := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=%s", key) | ||
sendMessageBody := map[string]interface{}{ | ||
"msgtype": "text", | ||
"text": map[string]string{ | ||
"content": fmt.Sprintf("%s\n%s", title, message), | ||
}, | ||
} | ||
jsonBody, _ := json.Marshal(sendMessageBody) | ||
resp, err := http.Post(sendMessageUrl, "application/json", bytes.NewBuffer(jsonBody)) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
return nil | ||
} |