aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjan <jan@ruken.pw>2016-04-06 17:54:12 (UTC)
committerjan <jan@ruken.pw>2016-04-06 17:54:12 (UTC)
commit314047dda73387fff1fd4ccdb35f5db6ea6d7d5a (patch)
treef6b0d4c52db0a93c9afa9989b4624f23193b3a28 /src
parentac1f32f068a8c1e856b6db00a8058d9bf372a006 (diff)
FEATURES!
Diffstat (limited to 'src')
-rw-r--r--src/feature/jisoku.rs55
-rw-r--r--src/feature/mod.rs23
-rw-r--r--src/feature/tasterank.rs61
-rw-r--r--src/main.rs21
4 files changed, 156 insertions, 4 deletions
diff --git a/src/feature/jisoku.rs b/src/feature/jisoku.rs
new file mode 100644
index 0000000..70bc886
--- /dev/null
+++ b/src/feature/jisoku.rs
@@ -0,0 +1,55 @@
1extern crate telegram_bot;
2use telegram_bot::{Api, Message, MessageType};
3use std::sync::{Arc, Mutex};
4use std::thread;
5use std::time::Duration;
6
7use std::ascii::AsciiExt;
8
9use feature::FeatureResult;
10use feature::Feature;
11
12pub struct Jisoku {
13 running: bool,
14}
15
16const MESSAGES: [&'static str; 5] = ["JISOKU METER FURIKIRI UBAU GAME",
17 "I AM A CHASER DARE YORI HAYAI RACE",
18 "LIFE IS FIRE TSUKIRU MADE HIGH PACE",
19 "LETS GET READY TO RUMBLE MEZAMERO PASSION",
20 "*insert dubstep*"];
21
22impl Feature for Jisoku {
23 fn name(&self) -> &'static str {
24 "Jisoku Meter"
25 }
26 fn init(&mut self) {}
27 fn handle(&mut self, a: Api, m: Message) -> Result<FeatureResult, String> {
28 if let MessageType::Text(ref s) = m.msg {
29 if !(&s).contains("JISOKU METER") {
30 return Ok(FeatureResult::Skip);
31 }
32 } else {
33 return Ok(FeatureResult::Skip);
34 }
35
36 if self.running {
37 return Ok(FeatureResult::Skip);
38 }
39 self.running = true;
40 thread::spawn(move || {
41 for s in MESSAGES.into_iter() {
42 a.send_message(m.chat.id(), (*s).to_owned(), None, None, None, None);
43 thread::sleep(Duration::new(1, 0));
44 }
45 });
46 Ok(FeatureResult::Handled)
47 }
48}
49
50impl Jisoku {
51 pub fn new() -> Jisoku {
52 let mut j = Jisoku { running: false };
53 j
54 }
55}
diff --git a/src/feature/mod.rs b/src/feature/mod.rs
new file mode 100644
index 0000000..5e00620
--- /dev/null
+++ b/src/feature/mod.rs
@@ -0,0 +1,23 @@
1use std::boxed::Box;
2use std::error::Error;
3
4extern crate telegram_bot;
5use telegram_bot::{Api, Message};
6
7pub mod jisoku;
8pub mod tasterank;
9
10pub enum FeatureResult {
11 Handled,
12 Skip,
13}
14
15pub trait Feature {
16 fn name(&self) -> &'static str;
17 fn init(&mut self);
18 fn handle(&mut self, Api, Message) -> Result<FeatureResult, String>;
19}
20
21pub fn init() -> Vec<Box<Feature>> {
22 vec![Box::new(tasterank::Tasterank::new()), Box::new(jisoku::Jisoku::new())]
23}
diff --git a/src/feature/tasterank.rs b/src/feature/tasterank.rs
new file mode 100644
index 0000000..c4a29f9
--- /dev/null
+++ b/src/feature/tasterank.rs
@@ -0,0 +1,61 @@
1use std::option::Option;
2use std::io;
3use std::io::prelude::*;
4use std::fs::File;
5
6use std::ascii::AsciiExt;
7
8extern crate telegram_bot;
9use telegram_bot::{Api, Message, MessageType};
10
11use feature::FeatureResult;
12use feature::Feature;
13
14pub struct Tasterank {
15 ranks: Vec<String>,
16}
17
18impl Feature for Tasterank {
19 fn name(&self) -> &'static str {
20 "Tasterank"
21 }
22 fn init(&mut self) {
23 if let Err(e) = self.update_ranks() {
24 panic!(e);
25 }
26 }
27 fn handle(&mut self, a: Api, m: Message) -> Result<FeatureResult, String> {
28 if let MessageType::Text(s) = m.msg {
29 if !(&s).to_ascii_lowercase().contains("luggas tasterank") {
30 return Ok(FeatureResult::Skip);
31 }
32 let mut msg = String::new();
33 for (i, rank) in self.ranks.iter().enumerate() {
34 msg.push_str(&format!("{}. {}\n", i + 1, rank));
35 }
36 a.send_message(m.chat.id(), msg, None, None, Some(m.message_id), None);
37 return Ok(FeatureResult::Handled);
38 }
39 Ok(FeatureResult::Skip)
40 }
41}
42
43impl Tasterank {
44 pub fn new() -> Tasterank {
45 let mut t = Tasterank { ranks: vec![] };
46 t.init();
47 t
48 }
49 fn update_ranks(&mut self) -> Result<(), io::Error> {
50 let mut f = try!(File::open("data/tasterank.list"));
51 let mut buf = String::new();
52 try!(f.read_to_string(&mut buf));
53 let temp: Vec<&str> = (&buf).split("\n").collect();
54 self.ranks.clear();
55 for r in &temp {
56 self.ranks.push(r.to_string());
57 }
58 self.ranks.pop().unwrap();
59 Ok(())
60 }
61}
diff --git a/src/main.rs b/src/main.rs
index 4cca6d1..eaec1f4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,18 +1,31 @@
1extern crate telegram_bot; 1extern crate telegram_bot;
2use telegram_bot::{Api, ListeningMethod, ListeningAction, Update};
2 3
3use telegram_bot::{Api, ListeningMethod, ListeningAction, MessageType, Update}; 4mod feature;
5use feature::{Feature, FeatureResult};
4 6
5fn main() { 7fn main() {
6 let api = Api::from_env("TELEGRAM_BOT_TOKEN").unwrap(); 8 let api = Api::from_env("TELEGRAM_BOT_TOKEN").unwrap();
7 9
8 println!("api started: {:?}", api.get_me()); 10 println!("api started: {:?}", api.get_me());
11 let mut features = feature::init();
9 12
10 let mut listener = api.listener(ListeningMethod::LongPoll(None)); 13 let mut listener = api.listener(ListeningMethod::LongPoll(None));
11 let res = listener.listen(|u| { 14 let res = listener.listen(|u| {
12 if let Some(m) = u.message { 15 if let Some(m) = u.message {
13 let name = m.from.first_name; 16 for f in &mut features {
14 17 match (**f).handle(api.clone(), m.clone()) {
15 println!("got message from {}", name); 18 Ok(r) => {
19 match r {
20 FeatureResult::Handled => break,
21 FeatureResult::Skip => continue,
22 };
23 }
24 Err(e) => {
25 println!("Error in feature {}: {}", f.name(), e);
26 }
27 };
28 }
16 }; 29 };
17 Ok(ListeningAction::Continue) 30 Ok(ListeningAction::Continue)
18 }); 31 });