@@ -34,6 +34,7 @@ import (
34
34
type WebhookOutput struct {
35
35
errorChan chan error
36
36
eventChan chan event.Event
37
+ format string
37
38
url string
38
39
username string
39
40
password string
@@ -43,6 +44,7 @@ func New(options ...WebhookOptionFunc) *WebhookOutput {
43
44
w := & WebhookOutput {
44
45
errorChan : make (chan error ),
45
46
eventChan : make (chan event.Event , 10 ),
47
+ format : "snek" ,
46
48
url : "http://localhost:3000" ,
47
49
}
48
50
for _ , option := range options {
@@ -95,13 +97,63 @@ func basicAuth(username, password string) string {
95
97
return "Basic " + base64 .StdEncoding .EncodeToString ([]byte (auth ))
96
98
}
97
99
100
+ func formatPayload (e * event.Event , format string ) []byte {
101
+ var data []byte
102
+ var err error
103
+ switch format {
104
+ case "discord" :
105
+ var dwe DiscordWebhookEvent
106
+ switch e .Type {
107
+ case "chainsync.block" :
108
+ be := e .Payload .(chainsync.BlockEvent )
109
+ dwe .Content = fmt .Sprintf (
110
+ "New Cardano block!\n Number: %d, Slot: %d\n Hash: %s" ,
111
+ be .BlockNumber ,
112
+ be .SlotNumber ,
113
+ be .BlockHash ,
114
+ )
115
+ case "chainsync.rollback" :
116
+ be := e .Payload .(chainsync.RollbackEvent )
117
+ dwe .Content = fmt .Sprintf (
118
+ "Cardano rollback!\n Slot: %d\n Hash: %s" ,
119
+ be .SlotNumber ,
120
+ be .BlockHash ,
121
+ )
122
+ case "chainsync.transaction" :
123
+ te := e .Payload .(chainsync.TransactionEvent )
124
+ dwe .Content = fmt .Sprintf (
125
+ "New Cardano transaction!\n Block: %d, Slot: %d, Inputs: %d, Outputs: %d\n Hash: %s" ,
126
+ te .BlockNumber ,
127
+ te .SlotNumber ,
128
+ len (te .Inputs ),
129
+ len (te .Outputs ),
130
+ te .TransactionHash ,
131
+ )
132
+ default :
133
+ dwe .Content = fmt .Sprintf ("%v" , e .Payload )
134
+ }
135
+
136
+ data , err = json .Marshal (dwe )
137
+ if err != nil {
138
+ return data
139
+ }
140
+ default :
141
+ data , err = json .Marshal (e )
142
+ if err != nil {
143
+ return data
144
+ }
145
+ }
146
+ return data
147
+ }
148
+
149
+ type DiscordWebhookEvent struct {
150
+ Content string `json:"content"`
151
+ }
152
+
98
153
func (w * WebhookOutput ) SendWebhook (e * event.Event ) error {
99
154
logger := logging .GetLogger ()
100
155
logger .Infof ("sending event %s to %s" , e .Type , w .url )
101
- data , err := json .Marshal (e )
102
- if err != nil {
103
- return fmt .Errorf ("%s" , err )
104
- }
156
+ data := formatPayload (e , w .format )
105
157
// Setup request
106
158
ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
107
159
defer cancel ()
0 commit comments