Skip to content

Commit 4f7293b

Browse files
SerhiiZahubaRostislavDugin
authored andcommitted
FEATURE (notifiers): Add MS Teams notifier
1 parent dee330e commit 4f7293b

File tree

25 files changed

+405
-17
lines changed

25 files changed

+405
-17
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/postgresus.iml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/prettier.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/internal/features/notifiers/enums.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ const (
88
NotifierTypeWebhook NotifierType = "WEBHOOK"
99
NotifierTypeSlack NotifierType = "SLACK"
1010
NotifierTypeDiscord NotifierType = "DISCORD"
11+
NotifierTypeTeams NotifierType = "TEAMS"
1112
)

backend/internal/features/notifiers/model.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
discord_notifier "postgresus-backend/internal/features/notifiers/models/discord"
77
"postgresus-backend/internal/features/notifiers/models/email_notifier"
88
slack_notifier "postgresus-backend/internal/features/notifiers/models/slack"
9+
teams_notifier "postgresus-backend/internal/features/notifiers/models/teams"
910
telegram_notifier "postgresus-backend/internal/features/notifiers/models/telegram"
1011
webhook_notifier "postgresus-backend/internal/features/notifiers/models/webhook"
1112

@@ -20,11 +21,12 @@ type Notifier struct {
2021
LastSendError *string `json:"lastSendError" gorm:"column:last_send_error;type:text"`
2122

2223
// specific notifier
23-
TelegramNotifier *telegram_notifier.TelegramNotifier `json:"telegramNotifier" gorm:"foreignKey:NotifierID"`
24-
EmailNotifier *email_notifier.EmailNotifier `json:"emailNotifier" gorm:"foreignKey:NotifierID"`
25-
WebhookNotifier *webhook_notifier.WebhookNotifier `json:"webhookNotifier" gorm:"foreignKey:NotifierID"`
26-
SlackNotifier *slack_notifier.SlackNotifier `json:"slackNotifier" gorm:"foreignKey:NotifierID"`
27-
DiscordNotifier *discord_notifier.DiscordNotifier `json:"discordNotifier" gorm:"foreignKey:NotifierID"`
24+
TelegramNotifier *telegram_notifier.TelegramNotifier `json:"telegramNotifier" gorm:"foreignKey:NotifierID"`
25+
EmailNotifier *email_notifier.EmailNotifier `json:"emailNotifier" gorm:"foreignKey:NotifierID"`
26+
WebhookNotifier *webhook_notifier.WebhookNotifier `json:"webhookNotifier" gorm:"foreignKey:NotifierID"`
27+
SlackNotifier *slack_notifier.SlackNotifier `json:"slackNotifier" gorm:"foreignKey:NotifierID"`
28+
DiscordNotifier *discord_notifier.DiscordNotifier `json:"discordNotifier" gorm:"foreignKey:NotifierID"`
29+
TeamsNotifier *teams_notifier.TeamsNotifier `json:"teamsNotifier,omitempty" gorm:"foreignKey:NotifierID;constraint:OnDelete:CASCADE"`
2830
}
2931

3032
func (n *Notifier) TableName() string {
@@ -64,6 +66,8 @@ func (n *Notifier) getSpecificNotifier() NotificationSender {
6466
return n.SlackNotifier
6567
case NotifierTypeDiscord:
6668
return n.DiscordNotifier
69+
case NotifierTypeTeams:
70+
return n.TeamsNotifier
6771
default:
6872
panic("unknown notifier type: " + string(n.NotifierType))
6973
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package teams_notifier
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"log/slog"
9+
"net/http"
10+
"net/url"
11+
12+
"github.com/google/uuid"
13+
)
14+
15+
type TeamsNotifier struct {
16+
NotifierID uuid.UUID `gorm:"type:uuid;primaryKey;column:notifier_id" json:"notifierId"`
17+
WebhookURL string `gorm:"type:text;not null;column:power_automate_url" json:"powerAutomateUrl"`
18+
}
19+
20+
func (TeamsNotifier) TableName() string {
21+
return "teams_notifiers"
22+
}
23+
24+
func (n *TeamsNotifier) Validate() error {
25+
if n.WebhookURL == "" {
26+
return errors.New("webhook_url is required")
27+
}
28+
u, err := url.Parse(n.WebhookURL)
29+
if err != nil || (u.Scheme != "http" && u.Scheme != "https") {
30+
return errors.New("invalid webhook_url")
31+
}
32+
return nil
33+
}
34+
35+
type cardAttachment struct {
36+
ContentType string `json:"contentType"`
37+
Content interface{} `json:"content"`
38+
}
39+
40+
type payload struct {
41+
Title string `json:"title"`
42+
Text string `json:"text"`
43+
Attachments []cardAttachment `json:"attachments,omitempty"`
44+
}
45+
46+
func (n *TeamsNotifier) Send(logger *slog.Logger, heading, message string) error {
47+
if err := n.Validate(); err != nil {
48+
return err
49+
}
50+
51+
card := map[string]any{
52+
"type": "AdaptiveCard",
53+
"version": "1.4",
54+
"body": []any{
55+
map[string]any{
56+
"type": "TextBlock",
57+
"size": "Medium",
58+
"weight": "Bolder",
59+
"text": heading,
60+
},
61+
map[string]any{"type": "TextBlock", "wrap": true, "text": message},
62+
},
63+
}
64+
65+
p := payload{
66+
Title: heading,
67+
Text: message,
68+
Attachments: []cardAttachment{
69+
{ContentType: "application/vnd.microsoft.card.adaptive", Content: card},
70+
},
71+
}
72+
73+
body, _ := json.Marshal(p)
74+
req, err := http.NewRequest(http.MethodPost, n.WebhookURL, bytes.NewReader(body))
75+
if err != nil {
76+
return err
77+
}
78+
req.Header.Set("Content-Type", "application/json")
79+
80+
resp, err := http.DefaultClient.Do(req)
81+
if err != nil {
82+
return err
83+
}
84+
85+
defer func() {
86+
if closeErr := resp.Body.Close(); closeErr != nil {
87+
logger.Error("failed to close response body", "error", closeErr)
88+
}
89+
}()
90+
91+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
92+
return fmt.Errorf("teams webhook returned status %d", resp.StatusCode)
93+
}
94+
95+
return nil
96+
}

0 commit comments

Comments
 (0)