-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 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,52 @@ | ||
package teams | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type TeamsPlugin struct{} | ||
|
||
func (plugin *TeamsPlugin) Send(url string, message string) error { | ||
config, err := plugin.CreateConfigFromURL(url) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
postUrl := buildUrl(config) | ||
return plugin.doSend(postUrl, message) | ||
} | ||
|
||
func (plugin *TeamsPlugin) doSend(postUrl string, message string) error { | ||
body := TeamsJson{ | ||
CardType: "MessageCard", | ||
Context: "http://schema.org/extensions", | ||
Markdown: true, | ||
Text: message, | ||
} | ||
|
||
jsonBody, err := json.Marshal(body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
res, err := http.Post(postUrl, "application/json", bytes.NewBuffer(jsonBody)) | ||
if res.StatusCode != http.StatusOK { | ||
msg := fmt.Sprintf("failed to send notification to teams, response status code %s", res.Status) | ||
return errors.New(msg) | ||
} | ||
return nil | ||
} | ||
|
||
func buildUrl(config *TeamsConfig) string { | ||
var baseUrl = "https://outlook.office.com/webhook" | ||
return fmt.Sprintf( | ||
"%s/%s/IncomingWebhook/%s/%s", | ||
baseUrl, | ||
config.Token.A, | ||
config.Token.B, | ||
config.Token.C) | ||
} |
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 teams | ||
|
||
import ( | ||
"errors" | ||
. "github.com/containrrr/shoutrrr/pkg/plugins" | ||
) | ||
|
||
type TeamsConfig struct { | ||
Token TeamsToken | ||
} | ||
|
||
func (plugin *TeamsPlugin) CreateConfigFromURL(url string) (*TeamsConfig, error) { | ||
arguments, err := ExtractArguments(url); | ||
if err != nil { | ||
return nil, err | ||
} else if !isTokenValid(arguments) { | ||
return nil, errors.New("invalid service url. malformed tokens") | ||
} | ||
return &TeamsConfig{ | ||
Token:TeamsToken{ | ||
A: arguments[0], | ||
B: arguments[1], | ||
C: arguments[2], | ||
}, | ||
}, 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,8 @@ | ||
package teams | ||
|
||
type TeamsJson struct { | ||
CardType string `json:"@type"` | ||
Context string `json:"@context"` | ||
Markdown bool `json:"markdown,bool"` | ||
Text string `json:"text,omitempty"` | ||
} |
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,38 @@ | ||
package teams | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
var uuid4_pattern = "[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}" | ||
|
||
type TeamsToken struct { | ||
A string | ||
B string | ||
C string | ||
} | ||
|
||
func isTokenValid(arguments []string) bool { | ||
return isTokenAValid(arguments[0]) && | ||
isTokenBValid(arguments[1]) && | ||
isTokenCValid(arguments[2]) | ||
} | ||
|
||
func isTokenAValid(token string) bool { | ||
pattern := fmt.Sprintf("%s@%s", uuid4_pattern) | ||
return matchesRegexp(pattern, token) | ||
} | ||
|
||
func isTokenBValid(token string) bool { | ||
return matchesRegexp("[A-Za-z0-9]{32}", token) | ||
} | ||
|
||
func isTokenCValid(token string) bool { | ||
return matchesRegexp(uuid4_pattern, token) | ||
} | ||
|
||
func matchesRegexp(pattern string, token string) bool { | ||
matched, err := regexp.MatchString(pattern, token) | ||
return !matched || err != nil | ||
} |