1
+ <?php
2
+
3
+ namespace Bot \Commands ;
4
+
5
+ use SpotifyWebAPI \SpotifyWebAPI ;
6
+ use SpotifyWebAPI \Session ;
7
+
8
+
9
+ class Spotify
10
+ {
11
+ public function getName (): string
12
+ {
13
+ return 'spotify ' ;
14
+ }
15
+ public function getDescription (): string
16
+ {
17
+ return 'Use spotify API to get the last 10 liked songs ' ;
18
+ }
19
+ public function getOptions (): array
20
+ {
21
+ return [];
22
+ }
23
+
24
+ public function handle ()
25
+ {
26
+ // create a new session instance
27
+ $ session = new Session (
28
+ $ _ENV ['SPOTIFY_CLIENT_ID ' ],
29
+ $ _ENV ['SPOTIFY_CLIENT_SECRET ' ],
30
+ $ _ENV ['SPOTIFY_REDIRECT_URI ' ]
31
+ );
32
+
33
+ // request authorization
34
+ $ options = [
35
+ 'scope ' => [
36
+ 'user-read-email ' ,
37
+ 'user-read-private ' ,
38
+ 'user-library-read ' ,
39
+ 'user-top-read ' ,
40
+ 'user-read-recently-played ' ,
41
+ 'user-read-playback-state ' ,
42
+ 'user-read-currently-playing ' ,
43
+ 'user-follow-read ' ,
44
+ 'user-read-playback-position ' ,
45
+ 'user-read-recently-played ' ,
46
+ 'user-read-playback-state ' ,
47
+ 'user-modify-playback-state ' ,
48
+ 'user-read-currently-playing ' ,
49
+ 'user-read-playback-position ' ,
50
+ 'user-read-recently-played ' ,
51
+ 'user-read-playback-state ' ,
52
+ 'user-modify-playback-state ' ,
53
+ 'user-read-currently-playing ' ,
54
+ 'user-read-playback-position ' ,
55
+ 'user-read-recently-played ' ,
56
+ 'user-read-playback-state ' ,
57
+ 'user-modify-playback-state ' ,
58
+ 'user-read-currently-playing ' ,
59
+ 'user-read-playback-position ' ,
60
+ 'user-read-recently-played ' ,
61
+ 'user-read-playback-state ' ,
62
+ 'user-modify-playback-state ' ,
63
+ 'user-read-currently-playing ' ,
64
+ 'user-read-playback-position ' ,
65
+ 'user-read-recently-played ' ,
66
+ 'user-read-playback-state ' ,
67
+ 'user-modify-playback-state ' ,
68
+ 'user-read-currently-playing ' ,
69
+ 'user-read-playback-position ' ,
70
+ 'user-read-recently-played ' ,
71
+ 'user-read-playback-state ' ,
72
+ 'user-modify-playback-state ' ,
73
+ 'user-read-currently-playing ' ,
74
+ 'user-read-playback-position ' ,
75
+ 'user-read-recently-played ' ,
76
+ 'user-read-playback-state ' ,
77
+ 'user-modify-playback-state ' ,
78
+ ],
79
+ ];
80
+
81
+ //use the redirect uri to get the code from spotify
82
+ $ session ->requestCredentialsToken ($ options ['scope ' ]);
83
+ $ accessToken = $ session ->getAccessToken ();
84
+ $ refreshToken = $ session ->getRefreshToken ();
85
+ $ api = new SpotifyWebAPI ();
86
+ $ api ->setAccessToken ($ accessToken );
87
+
88
+ $ url = "https://accounts.spotify.com/authorize?client_id= {$ _ENV ['SPOTIFY_CLIENT_ID ' ]}&response_type=code&redirect_uri= {$ _ENV ['SPOTIFY_REDIRECT_URI ' ]}&scope=user-read-email%20user-read-private%20user-library-read%20user-top-read%20user-read-recently-played%20user-read-playback-state%20user-read-currently-playing%20user-follow-read%20user-read-playback-position%20user-read-recently-played%20user-read-playback-state%20user-modify-playback-state%20user-read-currently-playing%20user-read-playback-position%20user-read-recently-played%20user-read-playback-state%20user-modify-playback-state%20user-read-currently-playing%20user-read-playback-position%20user-read-recently-played%20user-read-playback-state%20user-modify-playback-state%20user-read-currently-playing%20user-read-playback-position%20user-read-recently-played%20user-read-playback-state%20user-modify-playback-state%20user-read-currently-playing%20user-read-playback-position%20user-read-recently-played%20user-read-playback-state%20user-modify-playback-state " ;
89
+ return [
90
+ 'title ' => 'Spotify ' ,
91
+ 'content ' => "Click [here]( $ url) to login to spotify " ,
92
+ 'flags ' => 64 ,
93
+ 'color ' => hexdec ('34ebd8 ' )
94
+ ];
95
+ }
96
+ }
0 commit comments