-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from wutaizeng/master
Added Talk service for alerting.
- Loading branch information
Showing
9 changed files
with
247 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
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,26 @@ | ||
package talk | ||
|
||
import ( | ||
"net/url" | ||
) | ||
|
||
type Config struct { | ||
// Whether Talk integration is enabled. | ||
Enabled bool `toml:"enabled"` | ||
// The Talk webhook URL, can be obtained by adding Incoming Webhook integration. | ||
URL string `toml:"url"` | ||
// The default authorName, can be overriden per alert. | ||
AuthorName string `toml:"author_name"` | ||
} | ||
|
||
func NewConfig() Config { | ||
return Config{} | ||
} | ||
|
||
func (c Config) Validate() error { | ||
_, err := url.Parse(c.URL) | ||
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,61 @@ | ||
package talk | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
type Service struct { | ||
url string | ||
authorName string | ||
logger *log.Logger | ||
} | ||
|
||
func NewService(c Config, l *log.Logger) *Service { | ||
return &Service{ | ||
url: c.URL, | ||
authorName: c.AuthorName, | ||
logger: l, | ||
} | ||
} | ||
|
||
func (s *Service) Open() error { | ||
return nil | ||
} | ||
|
||
func (s *Service) Close() error { | ||
return nil | ||
} | ||
|
||
func (s *Service) Alert(title, text string) error { | ||
postData := make(map[string]interface{}) | ||
postData["title"] = title | ||
postData["text"] = text | ||
postData["authorName"] = s.authorName | ||
|
||
var post bytes.Buffer | ||
enc := json.NewEncoder(&post) | ||
err := enc.Encode(postData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := http.Post(s.url, "application/json", &post) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
type response struct { | ||
Error string `json:"error"` | ||
} | ||
r := &response{Error: "failed to understand Talk response"} | ||
dec := json.NewDecoder(resp.Body) | ||
dec.Decode(r) | ||
return errors.New(r.Error) | ||
} | ||
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