mirror of
https://gitlab.com/Anson-Projects/wiki-location-bot.git
synced 2025-08-03 20:11:29 +00:00
MVP working
This commit is contained in:
82
src/main.rs
Normal file
82
src/main.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use teloxide::prelude::*;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct PageInfo {
|
||||
pageid: usize,
|
||||
ns: usize,
|
||||
title: String,
|
||||
contentmodel: String,
|
||||
pagelanguage: String,
|
||||
pagelanguagehtmlcode: String,
|
||||
pagelanguagedir: String,
|
||||
touched: String,
|
||||
lastrevid: usize,
|
||||
length: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct GeoSearch {
|
||||
pageid: usize,
|
||||
ns: usize,
|
||||
title: String,
|
||||
lat: Option<f64>,
|
||||
lon: Option<f64>,
|
||||
dist: Option<f32>,
|
||||
primary: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Query {
|
||||
geosearch: Option<Vec<GeoSearch>>,
|
||||
pages: Option<HashMap<String, PageInfo>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Root {
|
||||
batchcomplete: bool,
|
||||
query: Query,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
pretty_env_logger::init();
|
||||
log::info!("Starting Wiki Locations Bot");
|
||||
|
||||
// let bot = Bot::from_env();
|
||||
let bot = Bot::new("6377516418:AAFnOnkLwbqpQARVoe0fikawtGqlZfuIgLM");
|
||||
|
||||
teloxide::repl(bot, |bot: Bot, msg: Message| async move {
|
||||
log::info!("Message recieved.");
|
||||
|
||||
match msg.location() {
|
||||
Some(user_location) => {
|
||||
log::info!("Location received.");
|
||||
|
||||
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();
|
||||
|
||||
for location in nearby_locations {
|
||||
bot.send_message(msg.chat.id, format!("http://en.wikipedia.org/?curid={}",location.pageid)).await?;
|
||||
|
||||
}
|
||||
|
||||
// bot.send_message(msg.chat.id, format!("{:#?}", resp))
|
||||
// .await?;
|
||||
// bot.send_message(msg.chat.id, resp.get("key").unwrap())
|
||||
// .await?;
|
||||
}
|
||||
None => {
|
||||
log::info!("Something other than a location recived.");
|
||||
bot.send_message(msg.chat.id, "send a location").await?;
|
||||
}
|
||||
};
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.await;
|
||||
}
|
Reference in New Issue
Block a user