Songlify/src/main.rs

96 lines
3.2 KiB
Rust

use log::{debug, info, LevelFilter};
use search::spotify;
use sentry::ClientInitGuard;
use std::env;
use teloxide::prelude::*;
use search::spotify::ContentKind::Track;
use search::spotify::TrackInfo;
use crate::search::get_spotify_kind;
use search::spotify::ContentKind::{Album, Playlist};
mod search;
mod tgformatter;
#[tokio::main]
async fn main() {
if env::var("RUST_LOG").is_err() {
pretty_env_logger::formatted_builder()
.filter_module("songlify", LevelFilter::Info)
.filter_module("teloxide", LevelFilter::Info)
.init();
} else {
pretty_env_logger::init();
}
log::info!("Starting Songlify...");
log::trace!("You're running in trace mode!");
let mut _guard: ClientInitGuard;
match env::var("SENTRY_DSN") {
Ok(sentry_dsn) => {
log::debug!("Sentry DSN found, enabling error reporting");
_guard = sentry::init((
sentry_dsn,
sentry::ClientOptions {
release: sentry::release_name!(),
..Default::default()
},
));
}
Err(_) => {
log::warn!("No sentry DSN set, errors will not be reported. Use SENTRY_DSN env variable if you want to set error reporting")
}
}
let bot = Bot::from_env().auto_send();
teloxide::repl(bot, |message: Message, bot: AutoSend<Bot>| async move {
let music_engine = search::Engine::new().await;
let opt_text_message = message.text();
if opt_text_message.is_none() {
return respond(());
}
let text_message = opt_text_message.unwrap();
let content_kind = opt_text_message.and_then(|x| get_spotify_kind(x));
let option_reply = match content_kind {
None => return respond(()),
Some(content) => match content {
Track(id) => {
info!("Processing song with spotify id: {}", id);
let track_item = music_engine.get_song_from_spotify_id(text_message).await;
tgformatter::format_track_message(track_item)
}
Album(id) => {
info!("Processing album with spotify id: {}", id);
let album_item = music_engine.get_album_from_spotify_id(text_message).await;
tgformatter::format_album_message(album_item)
}
Playlist(id) => {
info!("Processing playlist with spotify id: {}", id);
let playlist_item = music_engine
.get_playlist_from_spotify_id(text_message)
.await;
tgformatter::format_playlist_message(playlist_item)
}
_ => {
log::warn!("This kind of media has been not supported yet");
None
}
},
};
if option_reply.is_some() {
debug!("Got reply to send back");
let reply = option_reply.unwrap();
bot.send_message(message.chat.id, reply)
.reply_to_message_id(message.id)
.await?;
}
return respond(());
})
.await;
log::info!("Exiting...");
}