-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (62 loc) · 1.79 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
package main
import (
"context"
"fmt"
"log"
"os"
"team-radio/auth"
"team-radio/utils"
"github.com/joho/godotenv"
"github.com/shomali11/slacker"
)
func main() {
godotenv.Load(".env")
var prevSongLink string
botToken := os.Getenv("SLACK_BOT_AUTH_TOKEN")
appToken := os.Getenv("SLACK_APP_AUTH_TOKEN")
code, err := auth.FetchAuthCode()
if err != nil {
log.Println(err)
return
}
accessToken, _, err := auth.GetTokens(code)
log.Printf("Token: %s", accessToken)
if err != nil {
log.Println(err)
return
}
bot := slacker.NewClient(botToken, appToken)
go utils.LogCommandEvents(bot.CommandEvents())
bot.Command("spin", &slacker.CommandDefinition{
Description: "Add music to team radio",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
if prevSongLink == botCtx.Event().Text {
return
}
prevSongLink = botCtx.Event().Text
var tracksToAdd []string
playlistId := os.Getenv("PLAYLIST_ID")
songId, err := utils.PrepareSongID(botCtx.Event().Text)
if err != nil {
response.Reply("Sorry, the link is not from spotify.")
log.Printf("Error getting song ID: %v", err)
return
}
log.Printf("new music link: %v | song ID: %v | playlist ID: %v ", botCtx.Event().Text, songId, playlistId)
tracksToAdd = append(tracksToAdd, songId)
code := utils.AddMusicToPlaylist(accessToken, tracksToAdd, playlistId)
if code != 201 {
log.Println("could not add music to playlist")
response.Reply("Sorry, couldnt add song to playlist")
return
}
response.Reply(fmt.Sprintf("%s put something on the turntable", botCtx.Event().UserProfile.FirstName))
},
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err = bot.Listen(ctx)
if err != nil {
log.Fatal(err)
}
}