-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
43 lines (35 loc) · 926 Bytes
/
webhook.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
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"net/http"
"strings"
)
// traQにWebhookを送信する
func postWebhook(message string, webhookUrl string, webhookId string, webhookSecret string) error {
webhookFullUrl := webhookUrl + webhookId
// Webhookの署名を生成
mac := hmac.New(sha1.New, []byte(webhookSecret))
_, _ = mac.Write([]byte(message))
sig := hex.EncodeToString(mac.Sum(nil))
req, err := http.NewRequest("POST", webhookFullUrl, strings.NewReader(message))
if err != nil {
return err
}
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
req.Header.Set("X-TRAQ-Signature", sig)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
fmt.Printf("Sent webhook to traQ: statusCode: %d, body: %s\n", res.StatusCode, body)
return nil
}