Skip to content

Commit

Permalink
add logic for msteams
Browse files Browse the repository at this point in the history
  • Loading branch information
simskij committed May 18, 2019
1 parent 6b8dd69 commit ab8d4f7
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pkg/plugins/teams/teams.go
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)
}
26 changes: 26 additions & 0 deletions pkg/plugins/teams/teams_config.go
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
}
8 changes: 8 additions & 0 deletions pkg/plugins/teams/teams_json.go
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"`
}
38 changes: 38 additions & 0 deletions pkg/plugins/teams/teams_token.go
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
}

0 comments on commit ab8d4f7

Please sign in to comment.