-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (44 loc) · 1.03 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/dborzov/catbot/hookserve"
)
var githubSecret = os.Getenv("GITHUB_SECRET")
var slackWebHookURL = os.Getenv("SLACK_HOOK")
func main() {
fmt.Println("Catbot listens to the whispers of your heart...")
printARandomThought()
go RandomThoughtTimer()
server := hookserve.NewServer()
server.Secret = githubSecret
server.GoListenAndServe()
for {
select {
case event := <-server.Events:
fmt.Println("Received event: ", event)
go postToSlack(fmtEventMessage(event))
default:
time.Sleep(100)
}
}
}
func postToSlack(message string) {
type responseFormat struct {
Message string `json:"text"`
}
payload := responseFormat{
Message: message,
}
pb, err := json.Marshal(payload)
fmt.Printf("----> sending a message: \n %v\n----> to slack...", message)
resp, err := http.Post(slackWebHookURL, "application/json", bytes.NewBuffer(pb))
if err == nil {
fmt.Printf(" done\n")
}
fmt.Printf("\n----> Slack complains: %v, %v\n", resp, err)
}