From cc990ad7b1d5427d7b5804c99747a4e51fef98a3 Mon Sep 17 00:00:00 2001 From: Anson Biggs Date: Mon, 25 Mar 2024 02:52:40 +0000 Subject: [PATCH] clean up some code and fix crashing bug --- src/main.rs | 22 ++++++++++++++-------- src/openstreetmap.rs | 29 ++++++++++++++++------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 91bb0f0..fbe8d77 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,9 +7,9 @@ mod wikipedia; fn help_text() -> String { r#" -This is a simple bot that takes a location and returns all of the nearby locations that have a wikipedia page +This is a simple bot that takes a location and returns all of the nearby locations that have a wikipedia page\. -The bot can either be used by the `Send Location` button, sending a location from the share menu, or typing an address +The bot can either be used by the `Send Location` button, sending a location from the share menu, or typing an address\. This bot was made possible by: [OSM Foundation](https://nominatim.org/) for location searching @@ -27,7 +27,9 @@ async fn main() { teloxide::repl(bot, |bot: Bot, msg: Message| async move { log::info!("Message received."); - bot.send_chat_action(msg.chat.id, teloxide::types::ChatAction::Typing).await?; + bot.send_chat_action(msg.chat.id, teloxide::types::ChatAction::Typing) + .await + .unwrap(); let location_button = KeyboardButton { text: "Send Location".to_string(), @@ -51,13 +53,15 @@ async fn main() { bot.send_message(msg.chat.id, help_text()) .parse_mode(teloxide::types::ParseMode::MarkdownV2) .reply_markup(location_button_markup) - .await?; + .await + .unwrap(); } else { match openstreetmap::geocode_text(text) { Ok(location) => { - bot.send_location(msg.chat.id, location.lat, location.lon).await?; + bot.send_location(msg.chat.id, location.lat, location.lon).await.unwrap(); bot.send_message(msg.chat.id, format!("Location found: {}", location.name)) - .await?; + .await + .unwrap(); wikipedia::send_wikipedia_pages(location.lat, location.lon, bot, msg).await; } Err(error) => { @@ -66,7 +70,8 @@ async fn main() { msg.chat.id, format!("Location query returned no matches:\n\t{}\nTry /help", text), ) - .await?; + .await + .unwrap(); } } } @@ -80,7 +85,8 @@ async fn main() { "Send a location or address to see nearby places that have a wikipedia page!", ) .reply_markup(location_button_markup) - .await?; + .await + .unwrap(); } } Ok(()) diff --git a/src/openstreetmap.rs b/src/openstreetmap.rs index 9196340..66c3f9d 100644 --- a/src/openstreetmap.rs +++ b/src/openstreetmap.rs @@ -1,23 +1,26 @@ use serde::{Deserialize, Serialize}; use url::Url; +// None of the fields are guaranteed, most have a default. +// I think it's just easier to only include the variables I care +// about for now. #[derive(Debug, Serialize, Deserialize)] pub struct Place { - place_id: i64, - licence: String, - osm_type: String, - osm_id: i64, + // place_id: i64, + // licence: String, + // osm_type: String, + // osm_id: i64, lat: String, lon: String, - category: String, - #[serde(rename = "type")] - place_type: String, - place_rank: i32, - importance: f64, - addresstype: String, + // category: String, + // #[serde(rename = "type")] + // place_type: String, + // place_rank: i32, + // importance: f64, + // addresstype: String, name: String, - display_name: String, - boundingbox: Vec, + // display_name: String, + // boundingbox: Vec, } pub struct Location { @@ -27,7 +30,7 @@ pub struct Location { } pub fn geocode_text(search_query: &str) -> Result { - let base_url = "https://nominatim.openstreetmap.org/search.php"; + let base_url = "https://nominatim.openstreetmap.org/search"; let url = Url::parse_with_params(base_url, &[("q", search_query), ("format", "jsonv2"), ("limit", "1")]) .expect("Failed to construct OSM URL");