-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspotifyToSlackStatus.R
104 lines (97 loc) · 2.54 KB
/
spotifyToSlackStatus.R
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
#devtools::install_github("lynuhs/spotifyr")
library(spotifyr)
library(httr)
bearer <- "Bearer [ENTER_SLACK_OAUTH_TOKEN]"
Sys.setenv(SPOTIFY_CLIENT_ID = [ENTER_SPOTIFY_API_CLIENT_ID])
Sys.setenv(SPOTIFY_CLIENT_SECRET = [ENTER_SPOTIFY_API_CLIENT_SECRET])
user_id <- [ENTER_SLACK_MEMBER_ID]
access_token <- get_spotify_access_token()
tryCatch({
current_track <- get_my_currently_playing()
if(current_track$is_playing){
if(current_track$currently_playing_type == "episode"){
POST(
url = "https://slack.com/api/users.profile.set",
encode = "json",
add_headers(
.headers = c(
"Authorization" = bearer,
"Content-Type" = "application/json; charset=utf-8"
)
),
body = list(
profile = list(
status_emoji = ":headphones:",
status_text = "Podcast"
),
user = user_id
)
)
} else {
track <- current_track$item$name
artist <- paste(current_track$item$artists$name, collapse = ", ")
external_link <- current_track$item$external_urls$spotify
text = paste0(track, " (", artist, ")")
POST(
url = "https://slack.com/api/users.profile.set",
encode = "json",
add_headers(
.headers = c(
"Authorization" = bearer,
"Content-Type" = "application/json; charset=utf-8"
)
),
body = list(
profile = list(
status_emoji = ":headphones:",
status_text = text,
fields = list(
[CUSTOM_FIELD_ID] = list(
value = external_link,
alt = text
)
)
),
user = user_id
)
)
}
} else {
POST(
url = "https://slack.com/api/users.profile.set",
encode = "json",
add_headers(
.headers = c(
"Authorization" = bearer,
"Content-Type" = "application/json; charset=utf-8"
)
),
body = list(
profile = list(
status_emoji = "",
status_text = ""
),
user = user_id
)
)
}
}, error = function(e){
POST(
url = "https://slack.com/api/users.profile.set",
encode = "json",
add_headers(
.headers = c(
"Authorization" = bearer,
"Content-Type" = "application/json; charset=utf-8"
)
),
body = list(
profile = list(
status_emoji = "",
status_text = ""
),
user = user_id
)
)
}
})