-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
49 lines (43 loc) · 1.02 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
package main
import (
"errors"
"log"
"bytes"
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"net/http"
"os"
)
const slackWebhookKey = "WEBHOOK_URL"
var (
ErrInvalidStatusCode = errors.New("invalid status code")
ErrSlackWebhookNotFound = errors.New("slack webhook not found in env variables")
)
func Handler(request SNSMessage) error {
log.Printf("processing message from SNS: %v\n", request)
if err := request.Validate(); err != nil {
return err
}
slackURL, found := os.LookupEnv(slackWebhookKey)
if !found {
return ErrSlackWebhookNotFound
}
input := request.Records[0].SNS
payload := SlackPayload{
Text: fmt.Sprintf("%s: %s", input.Subject, input.Message),
}
payloadJSON, _ := json.Marshal(payload)
resp, err := http.Post(slackURL, "application/json", bytes.NewBuffer(payloadJSON))
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return ErrInvalidStatusCode
}
log.Printf("message with id %s send", input.MessageID)
return nil
}
func main() {
lambda.Start(Handler)
}