-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (51 loc) · 2.14 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
package main
import (
"net/http"
"os"
"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 {
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:
newMessage := "A commit has just been made to timomak/Golang-SlackAPI-GiphyAPI"
// release := payload.(github.PushPayload)
// newMessage := string(release.Pusher.Name) + " just made a commit to the " + string(release.Repository.FullName) + " repo.\nLook at the changes: " + string(release.Repository.HTMLURL) + "\n"
// fmt.Printf("%+v just made a commit to the %+v repo.\nLook at the changes: %+v\n", release.Pusher.Name, release.Repository.FullName, release.Repository.HTMLURL)
slackIt(newMessage, "portal-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)
}