Initial Commit
This commit is contained in:
commit
6b65045795
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
/target
|
||||
**/*.rs.bk
|
||||
/score.json
|
||||
1302
Cargo.lock
generated
Normal file
1302
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
16
Cargo.toml
Normal file
16
Cargo.toml
Normal file
@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "telegram-leetbot"
|
||||
version = "0.1.0"
|
||||
authors = ["Andreas Larsen <andreas@northcode.no>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
telegram-bot = "0.6.1"
|
||||
futures = "*"
|
||||
tokio-core = "*"
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
chrono = "0.4.6"
|
||||
|
||||
[patch.crates-io]
|
||||
openssl = { git = "https://github.com/ishitatsuyuki/rust-openssl", branch = "0.9.x" }
|
||||
0
src/lib.rs
Normal file
0
src/lib.rs
Normal file
108
src/main.rs
Normal file
108
src/main.rs
Normal file
@ -0,0 +1,108 @@
|
||||
extern crate telegram_bot;
|
||||
extern crate futures;
|
||||
extern crate tokio_core;
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
extern crate chrono;
|
||||
|
||||
use std::io::Write;
|
||||
use crate::futures::Stream;
|
||||
use tokio_core::reactor::Core;
|
||||
use std::env;
|
||||
use telegram_bot::*;
|
||||
|
||||
use chrono::prelude::*;
|
||||
|
||||
use std::sync::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
const SCORE_FILE_PATH : &str = "score.json";
|
||||
|
||||
fn main() {
|
||||
let mut core = Core::new().unwrap();
|
||||
|
||||
let token = env::var("TELEGRAM_BOT_TOKEN").unwrap();
|
||||
let api = Api::configure(token).build(core.handle()).unwrap();
|
||||
|
||||
let leet_time = NaiveTime::from_hms(13, 37, 30);
|
||||
|
||||
let scores : Arc<Mutex<HashMap<String, usize>>> = Arc::new(Mutex::new(HashMap::new()));
|
||||
|
||||
{
|
||||
|
||||
let scores = scores.clone();
|
||||
|
||||
std::thread::spawn(move || {
|
||||
loop {
|
||||
{
|
||||
let map = scores.lock().unwrap();
|
||||
|
||||
let json_str = serde_json::to_string(&*map).unwrap();
|
||||
|
||||
let mut score_file = std::fs::File::create(SCORE_FILE_PATH).expect("Unable to open or create file");
|
||||
|
||||
score_file.write_all(json_str.as_bytes()).expect("unable to write to score file!");
|
||||
}
|
||||
|
||||
println!("written score file");
|
||||
|
||||
std::thread::sleep(std::time::Duration::from_secs(5*60));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
let future = api.stream().for_each(|update| {
|
||||
if let UpdateKind::Message(message) = update.kind {
|
||||
if let MessageKind::Text {ref data, ..} = message.kind {
|
||||
println!("<{}>: {}", message.from.first_name, data);
|
||||
|
||||
let current_time = Local::now().time();
|
||||
|
||||
let allowed_timegap = 30;
|
||||
|
||||
if data.to_lowercase() == "leet" || data == "1337" {
|
||||
dbg!(data);
|
||||
|
||||
let leet_distance = current_time - leet_time;
|
||||
|
||||
if leet_distance.num_seconds() > allowed_timegap {
|
||||
api.spawn(message.text_reply(
|
||||
"It is not leet right now".to_string()
|
||||
));
|
||||
} else {
|
||||
let mut map = scores.lock().unwrap();
|
||||
|
||||
let entry = map.entry(message.from.first_name.clone()).or_insert(0);
|
||||
|
||||
*entry += 1;
|
||||
|
||||
api.spawn(message.text_reply(
|
||||
format!("{} just hit leet! New score: {}", message.from.first_name, *entry)
|
||||
));
|
||||
}
|
||||
|
||||
} else if data.contains("-score") {
|
||||
let map = scores.lock().unwrap();
|
||||
|
||||
let json = serde_json::to_string(&*map).unwrap();
|
||||
|
||||
api.spawn(message.text_reply(
|
||||
format!("Score json: {}", json)
|
||||
));
|
||||
} else if data.contains("-time") {
|
||||
|
||||
api.spawn(message.text_reply(
|
||||
format!("local time: {}, leet time: {}, time distance to leet: {:#?}", current_time, leet_time, (current_time - leet_time).num_seconds())
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
});
|
||||
|
||||
core.run(future).unwrap();
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user