Skip to content

Commit 6716902

Browse files
authored
Merge pull request #92 from blinklabs-io/feat/webhook-formats
feat: webhook formats
2 parents 2efd6f0 + 5dbc92d commit 6716902

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

output/webhook/options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ func WithBasicAuth(username, password string) WebhookOptionFunc {
3232
o.password = password
3333
}
3434
}
35+
36+
// WithFormat specifies the output webhook format
37+
func WithFormat(format string) WebhookOptionFunc {
38+
return func(o *WebhookOutput) {
39+
o.format = format
40+
}
41+
}

output/webhook/plugin.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
)
2020

2121
var cmdlineOptions struct {
22+
format string
2223
url string
2324
username string
2425
password string
@@ -32,6 +33,13 @@ func init() {
3233
Description: "send events via HTTP POST to a webhook server",
3334
NewFromOptionsFunc: NewFromCmdlineOptions,
3435
Options: []plugin.PluginOption{
36+
{
37+
Name: "format",
38+
Type: plugin.PluginOptionTypeString,
39+
Description: "specifies the webhook payload format to use",
40+
DefaultValue: "snek",
41+
Dest: &(cmdlineOptions.format),
42+
},
3543
{
3644
Name: "url",
3745
Type: plugin.PluginOptionTypeString,
@@ -62,6 +70,7 @@ func NewFromCmdlineOptions() plugin.Plugin {
6270
p := New(
6371
WithUrl(cmdlineOptions.url),
6472
WithBasicAuth(cmdlineOptions.username, cmdlineOptions.password),
73+
WithFormat(cmdlineOptions.format),
6574
)
6675
return p
6776
}

output/webhook/webhook.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
type WebhookOutput struct {
3535
errorChan chan error
3636
eventChan chan event.Event
37+
format string
3738
url string
3839
username string
3940
password string
@@ -43,6 +44,7 @@ func New(options ...WebhookOptionFunc) *WebhookOutput {
4344
w := &WebhookOutput{
4445
errorChan: make(chan error),
4546
eventChan: make(chan event.Event, 10),
47+
format: "snek",
4648
url: "http://localhost:3000",
4749
}
4850
for _, option := range options {
@@ -95,13 +97,63 @@ func basicAuth(username, password string) string {
9597
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
9698
}
9799

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!\nNumber: %d, Slot: %d\nHash: %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!\nSlot: %d\nHash: %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!\nBlock: %d, Slot: %d, Inputs: %d, Outputs: %d\nHash: %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+
98153
func (w *WebhookOutput) SendWebhook(e *event.Event) error {
99154
logger := logging.GetLogger()
100155
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)
105157
// Setup request
106158
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
107159
defer cancel()

0 commit comments

Comments
 (0)