-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (131 loc) · 3.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package integrations
import (
"errors"
"fmt"
)
type IntegrationInterface interface {
MapWebhook(input interface{}, adapterType IntegrationType, eventType EventType) (*Webhook, error)
GetIntegrationDetails() IntegrationDetailMap
}
type adapterService struct {
IntegrationInterface
}
type adapterData struct{}
var _ IntegrationInterface = &adapterService{}
type adapterDetail struct {
Name string
Icon string
Color string
Help string
}
type IntegrationDetailMap map[IntegrationType]adapterDetail
type IntegrationType int64
type InputFormFinished struct {
LinkText string
LinkUrl string
Title string
FormTranslation string
Nodes []InputFormFinishedNode
Contact *InputContactNode
}
type InputFormFinishedNode struct {
Relation int64
NodeType int64
NodeTranslation string
ContactNode InputContactNode
SelectNode InputSelectNode
RatingNode InputRatingNode
ChoiceNode InputChoiceNode
}
type InputContactNode struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Email string `json:"email"`
Company string `json:"company"`
Phone string `json:"phone"`
Details string `json:"details"`
}
type InputSelectNode struct {
Label string `json:"label"`
Selected []string `json:"selected"`
}
type InputRatingNode struct {
Label string `json:"label"`
Elements []struct {
Label string `json:"label"`
Value int64 `json:"value"`
} `json:"elements"`
}
type InputChoiceNode struct {
Elements []struct {
Label string `json:"label"`
AnswerShort string `json:"answerShort"`
AnswerLong string `json:"answerLong"`
} `json:"elements"`
}
type EventType string
type Webhook struct {
Data interface{}
Headers map[string][]string
}
const (
IntegrationGeneric IntegrationType = 0
IntegrationMattermost IntegrationType = 1
IntegrationSlack IntegrationType = 2
IntegrationNtfy IntegrationType = 3
IntegrationTeams IntegrationType = 4
MinTypeID int64 = int64(IntegrationGeneric)
MaxTypeID int64 = int64(IntegrationTeams)
EventFormFinished EventType = "form.finished"
)
var adapterDetails = IntegrationDetailMap{
// IntegrationGeneric: {
// Name: "Generic Webhook",
// Icon: "logos:webhooks",
// },
IntegrationMattermost: {
Name: "Mattermost",
Icon: "logos:mattermost-icon",
Help: "https://developers.mattermost.com/integrate/webhooks/incoming/#create-an-incoming-webhook",
},
IntegrationSlack: {
Name: "Slack",
Icon: "logos:slack-icon",
Help: "https://api.slack.com/messaging/webhooks",
},
// IntegrationNtfy: {
// Name: "Ntfy",
// Icon: "simple-icons:ntfy",
// Color: "#10b981",
// },
IntegrationTeams: {
Name: "Teams",
Icon: "logos:microsoft-teams",
Help: "https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook",
},
}
func NewIntegration() *adapterService {
return &adapterService{
&adapterData{},
}
}
func (ad *adapterData) GetIntegrationDetails() IntegrationDetailMap {
return adapterDetails
}
var sendWebhookMap = map[IntegrationType]func(input interface{}, eventType EventType) (*Webhook, error){
// IntegrationGeneric: generic,
IntegrationMattermost: mattermost,
IntegrationSlack: slack,
// IntegrationNtfy: ntfy,
IntegrationTeams: teams,
}
func (ad *adapterData) MapWebhook(input interface{}, adapterType IntegrationType, eventType EventType) (*Webhook, error) {
if input == nil {
return nil, errors.New("input not defined")
}
if sendWebhookFunc, ok := sendWebhookMap[adapterType]; ok {
return sendWebhookFunc(input, eventType)
} else {
return nil, errors.New(fmt.Sprint("map function not defined, type: ", adapterType))
}
}