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:
parent
6910d21035
commit
cc990ad7b1
22
src/main.rs
22
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(())
|
||||
|
@ -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<String>,
|
||||
// display_name: String,
|
||||
// boundingbox: Vec<String>,
|
||||
}
|
||||
|
||||
pub struct Location {
|
||||
@ -27,7 +30,7 @@ pub struct Location {
|
||||
}
|
||||
|
||||
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")])
|
||||
.expect("Failed to construct OSM URL");
|
||||
|
Loading…
x
Reference in New Issue
Block a user