Skip to content

Commit

Permalink
Merge pull request #92 from blinklabs-io/feat/webhook-formats
Browse files Browse the repository at this point in the history
feat: webhook formats
  • Loading branch information
wolf31o2 authored Oct 1, 2023
2 parents 2efd6f0 + 5dbc92d commit 6716902
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
7 changes: 7 additions & 0 deletions output/webhook/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ func WithBasicAuth(username, password string) WebhookOptionFunc {
o.password = password
}
}

// WithFormat specifies the output webhook format
func WithFormat(format string) WebhookOptionFunc {
return func(o *WebhookOutput) {
o.format = format
}
}
9 changes: 9 additions & 0 deletions output/webhook/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
)

var cmdlineOptions struct {
format string
url string
username string
password string
Expand All @@ -32,6 +33,13 @@ func init() {
Description: "send events via HTTP POST to a webhook server",
NewFromOptionsFunc: NewFromCmdlineOptions,
Options: []plugin.PluginOption{
{
Name: "format",
Type: plugin.PluginOptionTypeString,
Description: "specifies the webhook payload format to use",
DefaultValue: "snek",
Dest: &(cmdlineOptions.format),
},
{
Name: "url",
Type: plugin.PluginOptionTypeString,
Expand Down Expand Up @@ -62,6 +70,7 @@ func NewFromCmdlineOptions() plugin.Plugin {
p := New(
WithUrl(cmdlineOptions.url),
WithBasicAuth(cmdlineOptions.username, cmdlineOptions.password),
WithFormat(cmdlineOptions.format),
)
return p
}
60 changes: 56 additions & 4 deletions output/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
type WebhookOutput struct {
errorChan chan error
eventChan chan event.Event
format string
url string
username string
password string
Expand All @@ -43,6 +44,7 @@ func New(options ...WebhookOptionFunc) *WebhookOutput {
w := &WebhookOutput{
errorChan: make(chan error),
eventChan: make(chan event.Event, 10),
format: "snek",
url: "http://localhost:3000",
}
for _, option := range options {
Expand Down Expand Up @@ -95,13 +97,63 @@ func basicAuth(username, password string) string {
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
}

func formatPayload(e *event.Event, format string) []byte {
var data []byte
var err error
switch format {
case "discord":
var dwe DiscordWebhookEvent
switch e.Type {
case "chainsync.block":
be := e.Payload.(chainsync.BlockEvent)
dwe.Content = fmt.Sprintf(
"New Cardano block!\nNumber: %d, Slot: %d\nHash: %s",
be.BlockNumber,
be.SlotNumber,
be.BlockHash,
)
case "chainsync.rollback":
be := e.Payload.(chainsync.RollbackEvent)
dwe.Content = fmt.Sprintf(
"Cardano rollback!\nSlot: %d\nHash: %s",
be.SlotNumber,
be.BlockHash,
)
case "chainsync.transaction":
te := e.Payload.(chainsync.TransactionEvent)
dwe.Content = fmt.Sprintf(
"New Cardano transaction!\nBlock: %d, Slot: %d, Inputs: %d, Outputs: %d\nHash: %s",
te.BlockNumber,
te.SlotNumber,
len(te.Inputs),
len(te.Outputs),
te.TransactionHash,
)
default:
dwe.Content = fmt.Sprintf("%v", e.Payload)
}

data, err = json.Marshal(dwe)
if err != nil {
return data
}
default:
data, err = json.Marshal(e)
if err != nil {
return data
}
}
return data
}

type DiscordWebhookEvent struct {
Content string `json:"content"`
}

func (w *WebhookOutput) SendWebhook(e *event.Event) error {
logger := logging.GetLogger()
logger.Infof("sending event %s to %s", e.Type, w.url)
data, err := json.Marshal(e)
if err != nil {
return fmt.Errorf("%s", err)
}
data := formatPayload(e, w.format)
// Setup request
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Expand Down

0 comments on commit 6716902

Please sign in to comment.