-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·72 lines (60 loc) · 2.42 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
65
66
67
68
69
70
71
72
package main
import (
"net/http"
"os"
"strings"
"github.com/labstack/echo/v4"
"github.com/nlopes/slack"
"gopkg.in/go-playground/webhooks.v5/github"
_ "github.com/joho/godotenv/autoload"
)
// CreateSlackClient Create a client for the slack API
func CreateSlackClient(apiKey string) *slack.RTM {
api := slack.New(apiKey)
rtm := api.NewRTM()
go rtm.ManageConnection() // goroutine!
return rtm
}
// NotifySlackChannel sends a message to a Slack Channel using the Slack API
func NotifySlackChannel(slackClient *slack.RTM, message, channel string) {
slackMsg := slack.MsgOptionText(message, false) // Not sure why the false.
slackClient.PostMessage(channel, slackMsg) // Channel name, message
}
// main is our entrypoint, where the application initializes the Slackbot.
func main() {
hook, _ := github.New(github.Options.Secret(string(os.Getenv("WEBHOOK")))) // Secret for Webhook.
e := echo.New()
e.POST("/push", func(c echo.Context) error {
// fmt.Println("PUSH route called.")
payload, err := hook.Parse(c.Request(), github.PushEvent)
if err != nil {
if err == github.ErrEventNotFound {
// ok event wasn't one of the ones asked to be parsed
}
}
switch payload.(type) {
case github.PushPayload:
// fmt.Println("Message received")
// newMessage := "A commit has just been made to tsukudabuddha/paysplit"
release := payload.(github.PushPayload)
newMessage := "A Teammate made a commit on a master branch.\n" + string(release.Pusher.Name) + ": " + string(release.HeadCommit.Message) + "\nRepo: " + string(release.Repository.FullName) + "\nURL: " + string(release.Repository.HTMLURL)
// fmt.Println("Release:", release)
// fmt.Printf("%+v", release.Ref)
if strings.Contains(release.Ref, "master") == true || strings.Contains(release.Ref, "dev") { // Filter for commits made on master.
// fmt.Printf("Is Master!")
slackIt(newMessage, "paysplit-devs") // Message, Channel Name
}
// slackIt(newMessage, "paysplit-devs") // Message, Channel Name
}
return c.String(http.StatusOK, "Success.")
})
e.Logger.Fatal(e.Start(":3000"))
}
// slackIt is a function that initializes the Slackbot and sends a custom message to a specific channel.
func slackIt(message, channel string) {
botToken := os.Getenv("BOT_OAUTH_ACCESS_TOKEN")
slackClient := CreateSlackClient(botToken)
// fmt.Println("SENDING MESSASSAGE TO SLACK CHANNEL:", message)
NotifySlackChannel(slackClient, message, channel)
}
// Finished.