diff --git a/Cargo.toml b/Cargo.toml index d7bfc9e..d95666d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "frogbot" -version = "0.1.1" +version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/embeds.rs b/src/embeds.rs index 15741b9..9f76a51 100644 --- a/src/embeds.rs +++ b/src/embeds.rs @@ -15,7 +15,6 @@ use regex::Regex; use scraper::{Html, Selector}; /// Represents an Embed in the chat -#[derive(Default)] pub struct Embed { /// The title of the embed pub title: String, @@ -31,7 +30,7 @@ impl Embed { } /// Scrapes the HTML of a webpage and generates an [`Embed`] with the scraped information. -pub fn parse_metadata(page: &str) -> Option { +pub fn parse_metadata(page: &str) -> Embed { let doc_body = Html::parse_document(page); // Selectors used to get metadata are defined here @@ -45,11 +44,6 @@ pub fn parse_metadata(page: &str) -> Option { let mut meta_title = String::default(); let mut meta_description = String::default(); - if let (None, None) = (title, desc) { - warn!("Couldn't parse any metadata for URL"); - return None; - } - if let Some(title) = title { meta_title = title.text().collect(); } else { @@ -62,7 +56,7 @@ pub fn parse_metadata(page: &str) -> Option { warn!("Failed to parse description HTML"); } - Some(Embed::new(meta_title, meta_description)) + Embed::new(meta_title, meta_description) } /// Check if the message has any urls in it and get them if it does @@ -125,52 +119,40 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie let urls = get_urls_from_message(&text_content.body); - let reqwest_client = reqwest::Client::builder().user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36").build().unwrap(); + let reqwest_client = reqwest::Client::builder().user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36").build().unwrap(); for url in urls { if let Ok(req) = reqwest_client.get(url).send().await { if let Ok(res) = req.text().await { // beware, dirty HTML parsing code - let metadata = parse_metadata(&res); + let embed = parse_metadata(&res); - // Build and send our message reply - if metadata.is_some() { - let embed = metadata.unwrap(); - let bot_reply = RoomMessageEventContent::text_html( - &embed.title, - format!( - "
-

{}

-

{}

-
", - &embed.title, &embed.description - ), - ) - .make_reply_to(&full_reply_event); + // Build our message reply + let bot_reply = RoomMessageEventContent::text_html( + &embed.title, + format!( + r#" +
+
{}
+

{}

+

{}

+
+ "#, + &url, &url, &embed.title, &embed.description + ), + ) + .make_reply_to(&full_reply_event); - // Finally send the reply to the room - warn!("Sending embed for URL: '{}'", &url); - if room.send(bot_reply, None).await.is_err() { - warn!("Failed to send embed for URL: '{}'", &url); - } - // If we didn't get any metadata send a generic "No metadata" response - } else { - let bot_reply = RoomMessageEventContent::text_html( - "Couldn't parse metadata for URL", - "
Couldn't parse metadata for URL
", - ) - .make_reply_to(&full_reply_event); - // Send the reply to the room - warn!("Sending 'No metadata' embed for URL: '{}'", &url); - if room.send(bot_reply, None).await.is_err() { - warn!("Failed to send embed for URL: '{}'", &url); - } + // Finally send the reply to the room + warn!("Sending embed for URL: '{}'", &url); + if room.send(bot_reply, None).await.is_err() { + warn!("Failed to send embed for URL: '{}'", &url); } } else { warn!("Failed to parse HTML for URL: '{}'", &url); } } else { - warn!("Failed to fetch metadata for '{}'", &url); + warn!("Failed to get metadata for '{}'", &url); } } }; diff --git a/src/lib.rs b/src/lib.rs index 2cf40ee..829cec6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -141,12 +141,6 @@ pub async fn run(config: Config) -> anyhow::Result<()> { .await .expect("frogbot couldn't log into it's account."); - // Set the bot account's display name according to config - client - .account() - .set_display_name(Some(&config.display_name)) - .await?; - warn!("Logged in successfully!"); warn!( "server: '{}', username: '{}', display name: '{}'",