Skip to content

Commit

Permalink
feat: support specifying url for Bark (close #57)
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed Apr 22, 2023
1 parent da4b719 commit b14c2df
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions channel/bark.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package channel

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"message-pusher/model"
"net/http"
)

type barkMessageRequest struct {
Title string `json:"title"`
Body string `json:"body"`
URL string `json:"url"`
}

type barkMessageResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Expand All @@ -17,13 +24,20 @@ func SendBarkMessage(message *model.Message, user *model.User) error {
if user.BarkServer == "" || user.BarkSecret == "" {
return errors.New("未配置 Bark 消息推送方式")
}
url := ""
if message.Title != "" {
url = fmt.Sprintf("%s/%s/%s/%s", user.BarkServer, user.BarkSecret, message.Title, message.Description)
} else {
url = fmt.Sprintf("%s/%s/%s", user.BarkServer, user.BarkSecret, message.Description)
url := fmt.Sprintf("%s/%s", user.BarkServer, user.BarkSecret)
req := barkMessageRequest{
Title: message.Title,
Body: message.Content,
URL: message.URL,
}
if message.Content == "" {
req.Body = message.Description
}
reqBody, err := json.Marshal(req)
if err != nil {
return err
}
resp, err := http.Get(url)
resp, err := http.Post(url, "application/json", bytes.NewReader(reqBody))
if err != nil {
return err
}
Expand Down

0 comments on commit b14c2df

Please sign in to comment.