From 5e5e262ee93383f19f241fbfdf078ae66ac2d443 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Sat, 10 Aug 2019 18:57:15 -0700 Subject: [PATCH] Use blocks to display listener count, album, etc. To implement this, I switched to using github.com/nlopes/slack instead of implementing the structs directly. Fixes #1. --- Dockerfile | 8 +++---- go.mod | 6 +++++ go.sum | 13 +++++++++++ main.go | 5 ----- wuvt.go | 64 +++++++++++++++++++++++++++++++++++++++++++++--------- yi.go | 4 +++- 6 files changed, 80 insertions(+), 20 deletions(-) create mode 100644 go.sum diff --git a/Dockerfile b/Dockerfile index 5e3295c..7f6efd4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,14 @@ -FROM golang:alpine3.8 AS build +FROM golang:1.12-alpine3.10 AS build -COPY . /go/src/github.com/wuvt/slack-hooks -WORKDIR /go/src/github.com/wuvt/slack-hooks +COPY . /usr/src/github.com/wuvt/slack-hooks +WORKDIR /usr/src/github.com/wuvt/slack-hooks RUN set -ex \ && apk add --no-cache --virtual .build-deps git \ && go get -v . \ && apk del .build-deps -FROM alpine:3.8 +FROM alpine:3.10 RUN apk add --no-cache ca-certificates diff --git a/go.mod b/go.mod index 804f6c5..160cbc5 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module github.com/wuvt/slack-hooks go 1.12 + +require ( + github.com/gorilla/websocket v1.4.0 // indirect + github.com/nlopes/slack v0.5.1-0.20190809025457-0492f2f7dba4 + github.com/pkg/errors v0.8.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7bbbcfc --- /dev/null +++ b/go.sum @@ -0,0 +1,13 @@ +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= +github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/nlopes/slack v0.5.0 h1:NbIae8Kd0NpqaEI3iUrsuS0KbcEDhzhc939jLW5fNm0= +github.com/nlopes/slack v0.5.0/go.mod h1:jVI4BBK3lSktibKahxBF74txcK2vyvkza1z/+rRnVAM= +github.com/nlopes/slack v0.5.1-0.20190809025457-0492f2f7dba4 h1:wZWNOnQaLK28GKsbzyW7u2naAuwTi+6v6s0aQJah1+U= +github.com/nlopes/slack v0.5.1-0.20190809025457-0492f2f7dba4/go.mod h1:JzQ9m3PMAqcpeCam7UaHSuBuupz7CmpjehYMayT6YOk= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= diff --git a/main.go b/main.go index 45cd189..726e346 100644 --- a/main.go +++ b/main.go @@ -4,11 +4,6 @@ import ( "net/http" ) -type SlackWebhookResponse struct { - ResponseType string `json:"response_type"` - Text string `json:"text"` -} - func main() { http.HandleFunc("/wuvt", wuvtHandler) http.HandleFunc("/yi", yiHandler) diff --git a/wuvt.go b/wuvt.go index 1fd4df2..191b8b1 100644 --- a/wuvt.go +++ b/wuvt.go @@ -6,18 +6,23 @@ import ( "io/ioutil" "log" "net/http" + + "github.com/nlopes/slack" ) const trackmanURL = "https://trackman-fm.apps.wuvt.vt.edu/api" const djLinkURL = "https://www.wuvt.vt.edu/playlists/dj/%d" +const trackLinkURL = "https://www.wuvt.vt.edu/playlists/track/%d" type TrackmanLatestTrackResponse struct { - Album string `json:"album"` - Artist string `json:"artist"` - DJ string `json:"dj"` - DJID int `json:"dj_id"` - Label string `json:"label"` - Title string `json:"title"` + Album string `json:"album"` + Artist string `json:"artist"` + DJ string `json:"dj"` + DJID int `json:"dj_id"` + Label string `json:"label"` + Title string `json:"title"` + Listeners int `json:"listeners"` + TrackID int `json:"id"` } func wuvtHandler(w http.ResponseWriter, r *http.Request) { @@ -51,10 +56,49 @@ func wuvtHandler(w http.ResponseWriter, r *http.Request) { djLink = track.DJ } - output, err := json.Marshal(SlackWebhookResponse{ - ResponseType: "in_channel", - Text: fmt.Sprintf("*%s - %s*\nDJ: %s", track.Artist, track.Title, djLink), - }) + trackLink := fmt.Sprintf("<%s|%s>", fmt.Sprintf(trackLinkURL, track.TrackID), track.Title) + + message := slack.NewBlockMessage( + slack.NewSectionBlock( + slack.NewTextBlockObject( + slack.MarkdownType, + fmt.Sprintf("*%s - %s*", track.Artist, trackLink), + false, + false, + ), + []*slack.TextBlockObject{ + slack.NewTextBlockObject( + slack.MarkdownType, + fmt.Sprintf("Album: %s", track.Album), + false, + false, + ), + slack.NewTextBlockObject( + slack.MarkdownType, + fmt.Sprintf("Label: %s", track.Label), + false, + false, + ), + slack.NewTextBlockObject( + slack.MarkdownType, + fmt.Sprintf("DJ: %s", djLink), + false, + false, + ), + slack.NewTextBlockObject( + slack.MarkdownType, + fmt.Sprintf("Listeners: %d", track.Listeners), + false, + false, + ), + }, + nil, + ), + ) + + message.Msg.ResponseType = "in_channel" + + output, err := json.Marshal(message) if err != nil { log.Print(err) http.Error(w, "Unable to marshal JSON response", http.StatusInternalServerError) diff --git a/yi.go b/yi.go index 30d764e..a9286be 100644 --- a/yi.go +++ b/yi.go @@ -5,6 +5,8 @@ import ( "log" "net/http" "time" + + "github.com/nlopes/slack" ) func isItYi() string { @@ -23,7 +25,7 @@ func isItYi() string { } func yiHandler(w http.ResponseWriter, r *http.Request) { - output, err := json.Marshal(SlackWebhookResponse{ + output, err := json.Marshal(slack.Msg{ ResponseType: "in_channel", Text: isItYi(), })