-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
64 lines (54 loc) · 1.7 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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/go-plugin"
"github.com/kubeshop/botkube/pkg/api"
"github.com/kubeshop/botkube/pkg/api/source"
)
var _ source.Source = (*Forwarder)(nil)
// version is set via ldflags by GoReleaser.
var version = "dev"
// Forwarder implements the Botkube executor plugin interface.
type Forwarder struct {
// specify that the source doesn't handle streaming events
source.StreamUnimplemented
}
// Metadata returns details about the Forwarder plugin.
func (Forwarder) Metadata(_ context.Context) (api.MetadataOutput, error) {
return api.MetadataOutput{
Version: version,
Description: "Emits an event every time a message is sent as an incoming webhook request",
}, nil
}
// payload is the incoming webhook payload.
type payload struct {
Message string `json:"message"`
}
// HandleExternalRequest handles incoming payload and returns an event based on it.
//
//nolint:gocritic // hugeParam: in is heavy (104 bytes); consider passing it by pointer
func (Forwarder) HandleExternalRequest(_ context.Context, in source.ExternalRequestInput) (source.ExternalRequestOutput, error) {
var p payload
err := json.Unmarshal(in.Payload, &p)
if err != nil {
return source.ExternalRequestOutput{}, fmt.Errorf("while unmarshaling payload: %w", err)
}
if p.Message == "" {
return source.ExternalRequestOutput{}, fmt.Errorf("message cannot be empty")
}
msg := fmt.Sprintf("*Incoming webhook event:* %s", p.Message)
return source.ExternalRequestOutput{
Event: source.Event{
Message: api.NewPlaintextMessage(msg, true),
},
}, nil
}
func main() {
source.Serve(map[string]plugin.Plugin{
"forwarder": &source.Plugin{
Source: &Forwarder{},
},
})
}