1
0
mirror of https://gitlab.com/Anson-Projects/wiki-location-bot.git synced 2025-06-15 14:46:39 +00:00

clean up some code and fix crashing bug

This commit is contained in:
Anson Biggs 2024-03-25 02:52:40 +00:00
parent 6910d21035
commit cc990ad7b1
2 changed files with 30 additions and 21 deletions

View File

@ -7,9 +7,9 @@ mod wikipedia;
fn help_text() -> String { fn help_text() -> String {
r#" 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: This bot was made possible by:
[OSM Foundation](https://nominatim.org/) for location searching [OSM Foundation](https://nominatim.org/) for location searching
@ -27,7 +27,9 @@ async fn main() {
teloxide::repl(bot, |bot: Bot, msg: Message| async move { teloxide::repl(bot, |bot: Bot, msg: Message| async move {
log::info!("Message received."); 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 { let location_button = KeyboardButton {
text: "Send Location".to_string(), text: "Send Location".to_string(),
@ -51,13 +53,15 @@ async fn main() {
bot.send_message(msg.chat.id, help_text()) bot.send_message(msg.chat.id, help_text())
.parse_mode(teloxide::types::ParseMode::MarkdownV2) .parse_mode(teloxide::types::ParseMode::MarkdownV2)
.reply_markup(location_button_markup) .reply_markup(location_button_markup)
.await?; .await
.unwrap();
} else { } else {
match openstreetmap::geocode_text(text) { match openstreetmap::geocode_text(text) {
Ok(location) => { 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)) 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; wikipedia::send_wikipedia_pages(location.lat, location.lon, bot, msg).await;
} }
Err(error) => { Err(error) => {
@ -66,7 +70,8 @@ async fn main() {
msg.chat.id, msg.chat.id,
format!("Location query returned no matches:\n\t{}\nTry /help", text), 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!", "Send a location or address to see nearby places that have a wikipedia page!",
) )
.reply_markup(location_button_markup) .reply_markup(location_button_markup)
.await?; .await
.unwrap();
} }
} }
Ok(()) Ok(())

View File

@ -1,23 +1,26 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::Url; 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)] #[derive(Debug, Serialize, Deserialize)]
pub struct Place { pub struct Place {
place_id: i64, // place_id: i64,
licence: String, // licence: String,
osm_type: String, // osm_type: String,
osm_id: i64, // osm_id: i64,
lat: String, lat: String,
lon: String, lon: String,
category: String, // category: String,
#[serde(rename = "type")] // #[serde(rename = "type")]
place_type: String, // place_type: String,
place_rank: i32, // place_rank: i32,
importance: f64, // importance: f64,
addresstype: String, // addresstype: String,
name: String, name: String,
display_name: String, // display_name: String,
boundingbox: Vec<String>, // boundingbox: Vec<String>,
} }
pub struct Location { pub struct Location {
@ -27,7 +30,7 @@ pub struct Location {
} }
pub fn geocode_text(search_query: &str) -> Result<Location, &str> { pub fn geocode_text(search_query: &str) -> Result<Location, &str> {
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")]) let url = Url::parse_with_params(base_url, &[("q", search_query), ("format", "jsonv2"), ("limit", "1")])
.expect("Failed to construct OSM URL"); .expect("Failed to construct OSM URL");