Skip to content

Commit

Permalink
feat: add notification package with various notification providers
Browse files Browse the repository at this point in the history
  • Loading branch information
funnyzak committed Feb 18, 2024
1 parent 9b1c8c9 commit 97a3c66
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/notification/apprise.go
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
}
37 changes: 37 additions & 0 deletions pkg/notification/dingtalk.go
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
}
43 changes: 43 additions & 0 deletions pkg/notification/ifttt.go
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
}
13 changes: 13 additions & 0 deletions pkg/notification/notification.go
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)
}
37 changes: 37 additions & 0 deletions pkg/notification/smtp.go
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
}
39 changes: 39 additions & 0 deletions pkg/notification/telegram.go
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
}
37 changes: 37 additions & 0 deletions pkg/notification/wecombot.go
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
}

0 comments on commit 97a3c66

Please sign in to comment.