Skip to content

Commit

Permalink
Use blocks to display listener count, album, etc.
Browse files Browse the repository at this point in the history
To implement this, I switched to using github.com/nlopes/slack instead
of implementing the structs directly.

Fixes #1.
  • Loading branch information
mutantmonkey committed Aug 11, 2019
1 parent d102bd5 commit 5e5e262
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 20 deletions.
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
5 changes: 0 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
64 changes: 54 additions & 10 deletions wuvt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion yi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log"
"net/http"
"time"

"github.com/nlopes/slack"
)

func isItYi() string {
Expand All @@ -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(),
})
Expand Down

0 comments on commit 5e5e262

Please sign in to comment.