mirror of
https://gitlab.com/Anson-Projects/wiki-location-bot.git
synced 2025-08-05 21:11:27 +00:00
Resolve "Add location request button to bot"
This commit is contained in:
51
src/main.rs
51
src/main.rs
@@ -1,6 +1,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use teloxide::prelude::*;
|
||||
use teloxide::types::{ButtonRequest, KeyboardButton, KeyboardMarkup};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct PageInfo {
|
||||
@@ -47,34 +48,52 @@ async fn main() {
|
||||
let bot = Bot::from_env();
|
||||
|
||||
teloxide::repl(bot, |bot: Bot, msg: Message| async move {
|
||||
log::info!("Message recieved.");
|
||||
log::info!("Message received.");
|
||||
|
||||
let location_button = KeyboardButton {
|
||||
text: "Nearby Articles".to_string(),
|
||||
request: Some(ButtonRequest::Location),
|
||||
};
|
||||
|
||||
let location_button_markup = KeyboardMarkup::new([[location_button]])
|
||||
.one_time_keyboard(true)
|
||||
.resize_keyboard(true);
|
||||
|
||||
match msg.location() {
|
||||
Some(user_location) => {
|
||||
log::info!("Location received.");
|
||||
bot.send_message(msg.chat.id, "Searching for nearby locations...").await?;
|
||||
|
||||
let nearby_locations =
|
||||
ureq::get(&format!("https://en.wikipedia.org/w/api.php?action=query&format=json&list=geosearch&formatversion=2&gscoord={}|{}&gsradius=10000&gslimit=5",user_location.latitude,user_location.longitude))
|
||||
.call()
|
||||
.unwrap()
|
||||
.into_json::<Root>().unwrap().query.geosearch.unwrap();
|
||||
|
||||
// Not working, and also the wikipedia API is so fast its not necessary.
|
||||
// bot.send_chat_action(msg.chat.id,teloxide::types::ChatAction::FindLocation).await?;
|
||||
for location in nearby_locations {
|
||||
let nearby_locations = ureq::get(&format!(
|
||||
"https://en.wikipedia.org/w/api.php?action=query&format=json&list=geosearch&formatversion=2&gscoord={}|{}&gsradius=10000&gslimit=5",
|
||||
user_location.latitude, user_location.longitude
|
||||
))
|
||||
.call()
|
||||
.unwrap()
|
||||
.into_json::<Root>()
|
||||
.unwrap()
|
||||
.query
|
||||
.geosearch
|
||||
.unwrap();
|
||||
|
||||
bot.send_location(msg.chat.id,location.lat,location.lon).await?;
|
||||
for location in nearby_locations {
|
||||
bot.send_location(msg.chat.id, location.lat, location.lon).await?;
|
||||
|
||||
let url = teloxide::utils::markdown::link(&format!("http://en.wikipedia.org/?curid={}",location.pageid),&location.title);
|
||||
let url = teloxide::utils::markdown::link(&format!("http://en.wikipedia.org/?curid={}", location.pageid), &location.title);
|
||||
let bold_url = teloxide::utils::markdown::bold(&url);
|
||||
|
||||
bot.send_message(msg.chat.id, bold_url).parse_mode(teloxide::types::ParseMode::MarkdownV2).await?;
|
||||
bot.send_message(msg.chat.id, bold_url)
|
||||
.parse_mode(teloxide::types::ParseMode::MarkdownV2)
|
||||
.await?;
|
||||
}
|
||||
bot.send_message(msg.chat.id, "Send a location to see nearby places that have a wikipedia page!")
|
||||
.reply_markup(location_button_markup)
|
||||
.await?;
|
||||
}
|
||||
None => {
|
||||
log::info!("Something other than a location recived.");
|
||||
bot.send_message(msg.chat.id, "Send a location to see nearby places that have a wikipedia page!").await?;
|
||||
log::info!("Something other than a location received.");
|
||||
bot.send_message(msg.chat.id, "Send a location to see nearby places that have a wikipedia page!")
|
||||
.reply_markup(location_button_markup)
|
||||
.await?;
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user