Skip to content

Commit dd78e29

Browse files
committed
feat(alerting): Add basic send mode implementation
1 parent 40b1576 commit dd78e29

File tree

10 files changed

+440
-0
lines changed

10 files changed

+440
-0
lines changed

alerting/alert/type.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ const (
110110
// TypeTelegram is the Type for the telegram alerting provider
111111
TypeTelegram Type = "telegram"
112112

113+
// TypeThreemaGateway is the Type for the threema-gateway alerting provider
114+
TypeThreemaGateway Type = "threema-gateway"
115+
113116
// TypeTwilio is the Type for the twilio alerting provider
114117
TypeTwilio Type = "twilio"
115118

alerting/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/TwiN/gatus/v5/alerting/provider/teams"
4242
"github.com/TwiN/gatus/v5/alerting/provider/teamsworkflows"
4343
"github.com/TwiN/gatus/v5/alerting/provider/telegram"
44+
"github.com/TwiN/gatus/v5/alerting/provider/threemagateway"
4445
"github.com/TwiN/gatus/v5/alerting/provider/twilio"
4546
"github.com/TwiN/gatus/v5/alerting/provider/vonage"
4647
"github.com/TwiN/gatus/v5/alerting/provider/webex"
@@ -156,6 +157,9 @@ type Config struct {
156157
// Telegram is the configuration for the telegram alerting provider
157158
Telegram *telegram.AlertProvider `yaml:"telegram,omitempty"`
158159

160+
// ThreemaGateway is the configuration for the threema-gatway alerting provider
161+
ThreemaGateway *threemagateway.AlertProvider `yaml:"threema-gateway,omitempty"`
162+
159163
// Twilio is the configuration for the twilio alerting provider
160164
Twilio *twilio.AlertProvider `yaml:"twilio,omitempty"`
161165

alerting/provider/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/TwiN/gatus/v5/alerting/provider/teams"
3838
"github.com/TwiN/gatus/v5/alerting/provider/teamsworkflows"
3939
"github.com/TwiN/gatus/v5/alerting/provider/telegram"
40+
"github.com/TwiN/gatus/v5/alerting/provider/threemagateway"
4041
"github.com/TwiN/gatus/v5/alerting/provider/twilio"
4142
"github.com/TwiN/gatus/v5/alerting/provider/webex"
4243
"github.com/TwiN/gatus/v5/alerting/provider/zapier"
@@ -126,6 +127,7 @@ var (
126127
_ AlertProvider = (*teams.AlertProvider)(nil)
127128
_ AlertProvider = (*teamsworkflows.AlertProvider)(nil)
128129
_ AlertProvider = (*telegram.AlertProvider)(nil)
130+
_ AlertProvider = (*threemagateway.AlertProvider)(nil)
129131
_ AlertProvider = (*twilio.AlertProvider)(nil)
130132
_ AlertProvider = (*webex.AlertProvider)(nil)
131133
_ AlertProvider = (*zapier.AlertProvider)(nil)
@@ -167,6 +169,7 @@ var (
167169
_ Config[teams.Config] = (*teams.Config)(nil)
168170
_ Config[teamsworkflows.Config] = (*teamsworkflows.Config)(nil)
169171
_ Config[telegram.Config] = (*telegram.Config)(nil)
172+
_ Config[threemagateway.Config] = (*threemagateway.Config)(nil)
170173
_ Config[twilio.Config] = (*twilio.Config)(nil)
171174
_ Config[webex.Config] = (*webex.Config)(nil)
172175
_ Config[zapier.Config] = (*zapier.Config)(nil)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package threemagateway
2+
3+
import (
4+
"encoding"
5+
"errors"
6+
"fmt"
7+
"strings"
8+
)
9+
10+
// TODO#1464: Add tests
11+
12+
const (
13+
defaultRecipientType = RecipientTypeId
14+
)
15+
16+
var (
17+
ErrInvalidRecipientFormat = errors.New("recipient must be in the format '[<type>:]<value>'")
18+
ErrInvalidRecipientType = fmt.Errorf("invalid recipient type, must be one of: %v", joinKeys(validRecipientTypes, ", "))
19+
validRecipientTypes = map[string]RecipientType{
20+
"id": RecipientTypeId,
21+
"phone": RecipientTypePhone,
22+
"email": RecipientTypeEmail,
23+
}
24+
)
25+
26+
type RecipientType int
27+
28+
const (
29+
RecipientTypeInvalid RecipientType = iota
30+
RecipientTypeId
31+
RecipientTypePhone
32+
RecipientTypeEmail
33+
)
34+
35+
func parseRecipientType(s string) RecipientType {
36+
if val, ok := validRecipientTypes[s]; ok {
37+
return val
38+
}
39+
return RecipientTypeInvalid
40+
}
41+
42+
type Recipient struct {
43+
Value string `yaml:"-"`
44+
Type RecipientType `yaml:"-"`
45+
}
46+
47+
var _ encoding.TextUnmarshaler = (*Recipient)(nil)
48+
var _ encoding.TextMarshaler = (*Recipient)(nil)
49+
50+
func (r *Recipient) UnmarshalText(text []byte) error {
51+
parts := strings.Split(string(text), ":")
52+
switch {
53+
case len(parts) > 2:
54+
return ErrInvalidRecipientFormat
55+
case len(parts) == 2:
56+
if r.Type = parseRecipientType(parts[0]); r.Type == RecipientTypeInvalid {
57+
return ErrInvalidRecipientType
58+
}
59+
r.Value = parts[1]
60+
default:
61+
r.Type = defaultRecipientType
62+
r.Value = parts[0]
63+
}
64+
return nil
65+
}
66+
67+
func (r Recipient) MarshalText() ([]byte, error) {
68+
return []byte(r.Value), nil
69+
}
70+
71+
func (r *Recipient) Validate() error {
72+
if len(r.Value) == 0 {
73+
return ErrInvalidRecipientFormat
74+
}
75+
switch r.Type {
76+
case RecipientTypeId:
77+
if err := validateThreemaId(r.Value); err != nil {
78+
return err
79+
}
80+
case RecipientTypePhone:
81+
// Basic validation for phone number # TODO#1464: improve phone number validation
82+
if !strings.HasPrefix(r.Value, "+") || len(r.Value) < 8 {
83+
return errors.New("invalid phone number format")
84+
}
85+
case RecipientTypeEmail:
86+
// Basic validation for email address // TODO#1464: improve email validation
87+
if !strings.Contains(r.Value, "@") {
88+
return errors.New("invalid email address format")
89+
}
90+
default:
91+
return ErrInvalidRecipientType
92+
}
93+
return nil
94+
}

0 commit comments

Comments
 (0)