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

MVP working

This commit is contained in:
Anson 2023-07-07 23:09:04 +00:00
commit 6b6e93234f
5 changed files with 1929 additions and 0 deletions

View File

@ -0,0 +1,31 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/rust
{
"name": "Rust",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/rust:1-1-bullseye"
// Use 'mounts' to make the cargo cache persistent in a Docker Volume.
// "mounts": [
// {
// "source": "devcontainer-cargo-cache-${devcontainerId}",
// "target": "/usr/local/cargo",
// "type": "volume"
// }
// ]
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "rustc --version",
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1800
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

15
Cargo.toml Normal file
View File

@ -0,0 +1,15 @@
[package]
name = "rust"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
teloxide = { version = "0.12", features = ["macros"] }
log = "0.4"
pretty_env_logger = "0.5.0"
tokio = { version = "1.8", features = ["rt-multi-thread", "macros"] }
ureq = { version = "2.7.1", features = ["json"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1.0"

82
src/main.rs Normal file
View 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;
}