From f3d19182d7a749c29230e2024eb56dd2ba8ef564 Mon Sep 17 00:00:00 2001 From: Davide Polonio Date: Tue, 21 Sep 2021 23:41:50 +0200 Subject: [PATCH] feat: add first bot draft --- .idea/.gitignore | 8 +++++++ .idea/misc.xml | 9 +++++++ .idea/modules.xml | 8 +++++++ .idea/runConfigurations.xml | 10 ++++++++ .idea/vcs.xml | 6 +++++ Cargo.toml | 13 ++++++++++ songlify.iml | 12 ++++++++++ src/main.rs | 47 +++++++++++++++++++++++++++++++++++++ 8 files changed, 113 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/vcs.xml create mode 100644 Cargo.toml create mode 100644 songlify.iml create mode 100644 src/main.rs diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..bfecbb1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..68e6503 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..35ec4a3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "songlify" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +teloxide = { version = "0.5", features = ["auto-send", "macros"] } +log = "0.4" +pretty_env_logger = "0.4.0" +tokio = { version = "1.8", features = ["rt-multi-thread", "macros"] } +rspotify = "0.10.0" \ No newline at end of file diff --git a/songlify.iml b/songlify.iml new file mode 100644 index 0000000..2fecef3 --- /dev/null +++ b/songlify.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..75e8ac2 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,47 @@ +use teloxide::prelude::*; +use crate::Spotify::Track; + +enum Spotify { + Track(String) +} + +fn get_spotify_entry(url : &str) -> Option { + if url.contains("https://open.spotify.com/track/") { + let track_id = url.rsplit('/').next().and_then(|x| x.split('?').next()); + return match track_id { + Some(id) => Some(Spotify::Track(id.to_string())), + None => None + } + } + return None +} + +fn is_spotify_url(url : &str) -> Option<&str> { + // https://open.spotify.com/track/0VffaI2jwQknRrxpECYHsF?si=1e16c64779744375 + if url.contains("https://open.spotify.com/") { + return Some(url) + } + return None +} + +#[tokio::main] +async fn main() { + teloxide::enable_logging!(); + log::info!("Starting Songlify..."); + + let bot = Bot::from_env().auto_send(); + + teloxide::repl(bot, |message| async move { + let text = message.update.text().and_then(get_spotify_entry); + match text { + Some(spotify) => { + match spotify { + Track(id) => Some(message.reply_to(id).await?) + } + } + None => None + }; + respond(()) + }) + .await; +} \ No newline at end of file