-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (125 loc) · 3.61 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/tidwall/gjson"
Infrastructure "slack-spotify-integration/infrastructure"
Slack "slack-spotify-integration/slack"
Spotify "slack-spotify-integration/spotify"
"github.com/gin-gonic/gin"
)
type Event struct {
Type string `json:"type"`
Text string `json:"text"`
Channel string `json:"channel"`
Ts string `json:"ts"`
BotId string `json:"bot_id"`
}
type JsonRequest struct {
Event Event `json:"event"`
}
func main() {
Infrastructure.NewConfig()
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"Status": "Server is up and running!"})
})
r.POST("/", func(c *gin.Context) {
challenge, _ := ioutil.ReadAll(c.Request.Body)
fmt.Printf("%s", string(challenge))
c.JSON(http.StatusOK, gin.H{
"challenge": string(challenge),
})
})
r.POST("/endpoint", func(c *gin.Context) {
var jsonRequest JsonRequest
if err := c.ShouldBindJSON(&jsonRequest); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var event Event = jsonRequest.Event
if event.BotId == "" && event.Text != "" {
switch strings.ToLower(event.Text) {
case "list":
tracks, _ := Spotify.GetPlaylistQueue()
if tracks == nil {
c.JSON(418, gin.H{"error": "No tracks in the playlist"})
return
}
Slack.SendTracks(event.Channel, event.Ts, tracks, "Remove")
c.JSON(http.StatusOK, nil)
return
case "commands", "help":
err := Slack.SendCommands(event.Channel, event.Ts)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
} else {
c.JSON(http.StatusOK, nil)
return
}
default:
tracks, err := Spotify.GetSongs(event.Text)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if len(tracks) < 1 {
c.JSON(http.StatusNotFound, gin.H{"error": "No track matched keywork passed"})
return
}
Slack.SendTracks(event.Channel, event.Ts, tracks, "Add")
return
}
}
c.JSON(http.StatusOK, nil)
return
})
r.POST("/add-to-playlist", func(c *gin.Context) {
request, _ := ioutil.ReadAll(c.Request.Body)
encodedValue := string(request)
decodedValue, err := url.QueryUnescape(encodedValue)
if err != nil {
return
}
json := decodedValue[8:]
trackValue := (gjson.Get(json, "actions.0.value")).String()
trackId := (gjson.Get(trackValue, "id")).String()
responseUrl := (gjson.Get(json, "response_url")).String()
channelId := (gjson.Get(json, "channel.id")).String()
messageTs := (gjson.Get(json, "container.message_ts")).String()
action := (gjson.Get(trackValue, "action")).String()
if action == "add" {
snapshot, err := Spotify.AddTrackToPlaylist(trackId)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"replace_original": "true",
"text": "Sorry, Something went wrong",
})
} else if snapshot != nil {
Slack.UpdateOriginalMessage(trackValue, channelId, messageTs, responseUrl, action)
}
} else if action == "remove" {
snapshot, err := Spotify.RemoveFromPlaylist(trackId)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"replace_original": "true",
"text": "Sorry, Something went wrong",
})
} else if snapshot != nil {
Slack.UpdateOriginalMessage(trackValue, channelId, messageTs, responseUrl, action)
}
}
})
r.GET("/callback", func(c *gin.Context) {
code := c.Request.URL.Query().Get("code")
fmt.Printf("%s", string(code))
c.JSON(http.StatusOK, gin.H{
"code": string(code),
})
})
r.Run()
}