WienerScript/examples/slack-spotify-status/index.ws

160 lines
4.7 KiB
Text
Raw Permalink Normal View History

require('dotenv').config();
FIX OIDA { App, ExpressReceiver } WENNST MANST require('@slack/bolt');
FIX OIDA axios WENNST MANST require('axios');
FIX OIDA qs WENNST MANST require('querystring');
2020-06-28 00:33:17 +02:00
FIX OIDA expressReceiver WENNST MANST HAWIDERE ExpressReceiver({});
FIX OIDA app WENNST MANST HAWIDERE App({
signingSecret: process.env.SLACK_SIGNING_SECRET,
token: process.env.SLACK_USER_TOKEN,
receiver: expressReceiver,
logLevel: 'DEBUG'
2020-06-28 00:33:17 +02:00
});
FIX OIDA express WENNST MANST expressReceiver.app;
// set your status emoji
FIX OIDA EMOJI WENNST MANST ':spotify:'
// ping function to keep glitch alive
express.get('/ping', OWIZAHRA HACKL AMOI WOS (req, res) {
I MAN JA NUR ('<3');
2020-06-28 00:33:17 +02:00
FIX OIDA spotifyInfo WENNST MANST JO GLEI getSpotifyStatus();
2020-06-30 02:12:09 +02:00
WOS WÜSTN (spotifyInfo.is_playing) {
2020-06-28 00:33:17 +02:00
FIX OIDA song WENNST MANST {
name: spotifyInfo.item.name,
artists: spotifyInfo.item.artists
2020-06-30 02:32:16 +02:00
.map(artist HUACH ZUA artist.name)
.join(' & ')
2020-06-28 00:33:17 +02:00
};
JO GLEI setStatus(song, EMOJI);
DRAH DI HAM song;
2020-06-30 02:12:09 +02:00
} A SCHO WUASCHT {
2020-06-28 00:33:17 +02:00
FIX OIDA status WENNST MANST JO GLEI getSlackStatus();
// only unset status if it's a spotify status
2020-06-30 02:12:09 +02:00
WOS WÜSTN (isSpotifyStatus(status)) JO GLEI unsetStatus();
2020-06-28 00:33:17 +02:00
}
DRAH DI HAM res.send({ ping: 'pong' });
2020-06-28 00:33:17 +02:00
});
express.get('/spotify/connect', OWIZAHRA HACKL AMOI WOS (req, res) {
FIX OIDA scopes WENNST MANST 'user-read-currently-playing user-read-playback-state';
2020-06-28 00:33:17 +02:00
res.redirect(
'https://accounts.spotify.com/authorize' +
'?response_type=code' +
'&client_id=' +
2020-06-28 00:33:17 +02:00
process.env.SPOTIFY_CLIENT_ID +
(scopes ? '&scope=' + encodeURIComponent(scopes) : '') +
'&redirect_uri+' +
2020-06-28 00:33:17 +02:00
encodeURIComponent(process.env.SPOTIFY_REDIRECT_URL)
);
});
express.get('/spotify/oauth', OWIZAHRA HACKL AMOI WOS (req, res) {
2020-06-28 00:33:17 +02:00
FIX OIDA result WENNST MANST JO GLEI getSpotifyToken({
grant_type: 'authorization_code',
2020-06-28 00:33:17 +02:00
code: req.query.code,
redirect_uri: process.env.SPOTIFY_REDIRECT_URL
});
DRAH DI HAM res.send(result.refresh_token);
});
HACKL AMOI WOS base64 (data) {
FIX OIDA buff WENNST MANST new Buffer(data);
DRAH DI HAM buff.toString('base64');
2020-06-28 00:33:17 +02:00
};
2020-06-30 02:30:31 +02:00
OWIZAHRA HACKL AMOI WOS getSpotifyToken (body) {
2020-06-28 00:33:17 +02:00
SCHAU MA MOL {
FIX OIDA config WENNST MANST {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
2020-06-28 00:33:17 +02:00
Authorization:
'Basic ' +
2020-06-28 00:33:17 +02:00
base64(
process.env.SPOTIFY_CLIENT_ID +
':' +
2020-06-28 00:33:17 +02:00
process.env.SPOTIFY_CLIENT_SECRET
)
}
};
FIX OIDA result WENNST MANST JO GLEI axios.post(
`https://accounts.spotify.com/api/token`,
qs.stringify(body),
config
);
DRAH DI HAM result.data;
} LECK OASCH (err) {
DRAH DI HAM GSCHISSN GRISSN (err.response.data);
}
};
2020-06-30 02:30:31 +02:00
OWIZAHRA HACKL AMOI WOS getSpotifyStatus () {
2020-06-28 00:33:17 +02:00
FIX OIDA token WENNST MANST JO GLEI getSpotifyToken({
grant_type: 'refresh_token',
2020-06-28 00:33:17 +02:00
refresh_token: process.env.SPOTIFY_REFRESH_TOKEN
});
FIX OIDA config WENNST MANST {
headers: {
Authorization: 'Bearer ' + token.access_token
2020-06-28 00:33:17 +02:00
}
};
FIX OIDA result WENNST MANST JO GLEI axios.get(
`https://api.spotify.com/v1/me/player/currently-playing`,
config
);
DRAH DI HAM result.data;
};
// check if the current Slack status is a spotify status
HACKL AMOI WOS isSpotifyStatus (status) {
DRAH DI HAM status.emoji KANNST DA VUASTÖHN EMOJI;
};
2020-06-30 02:30:31 +02:00
OWIZAHRA HACKL AMOI WOS getSlackStatus() {
2020-06-28 00:33:17 +02:00
FIX OIDA profile WENNST MANST JO GLEI app.client.users.profile.get({
token: process.env.SLACK_USER_TOKEN
});
DRAH DI HAM {
emoji: profile.profile.status_emoji,
text: profile.profile.status_text
};
};
2020-06-30 02:30:31 +02:00
OWIZAHRA HACKL AMOI WOS unsetStatus () {
2020-06-28 00:33:17 +02:00
JO GLEI app.client.users.profile.set({
token: process.env.SLACK_USER_TOKEN,
profile: {
status_text: '',
status_emoji: ''
2020-06-28 00:33:17 +02:00
}
});
};
2020-06-30 02:30:31 +02:00
OWIZAHRA HACKL AMOI WOS setStatus (song, emoji) {
2020-06-28 00:33:17 +02:00
JO GLEI app.client.users.profile.set({
token: process.env.SLACK_USER_TOKEN,
profile: {
status_text: `listening to ${song.name} by ${song.artists}`,
status_emoji: `${emoji}`,
status_expiration: 0
}
});
};
app.error(HACKL AMOI WOS (error) {
GSCHISSN GRISSN (error);
});
// Start your app
2020-06-30 02:30:31 +02:00
(OWIZAHRA HACKL AMOI WOS () {
2020-06-28 00:33:17 +02:00
JO GLEI app.start(process.env.PORT || 3000);
I MAN JA NUR ('⚡️ Bolt app is running!');
2020-06-28 00:33:17 +02:00
})();