From 314047dda73387fff1fd4ccdb35f5db6ea6d7d5a Mon Sep 17 00:00:00 2001 From: jan Date: Wed, 6 Apr 2016 19:54:12 +0200 Subject: FEATURES! diff --git a/.gitignore b/.gitignore index eb5a316..80ddbfc 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ target +data 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 @@ +extern crate telegram_bot; +use telegram_bot::{Api, Message, MessageType}; +use std::sync::{Arc, Mutex}; +use std::thread; +use std::time::Duration; + +use std::ascii::AsciiExt; + +use feature::FeatureResult; +use feature::Feature; + +pub struct Jisoku { + running: bool, +} + +const MESSAGES: [&'static str; 5] = ["JISOKU METER FURIKIRI UBAU GAME", + "I AM A CHASER DARE YORI HAYAI RACE", + "LIFE IS FIRE TSUKIRU MADE HIGH PACE", + "LETS GET READY TO RUMBLE MEZAMERO PASSION", + "*insert dubstep*"]; + +impl Feature for Jisoku { + fn name(&self) -> &'static str { + "Jisoku Meter" + } + fn init(&mut self) {} + fn handle(&mut self, a: Api, m: Message) -> Result { + if let MessageType::Text(ref s) = m.msg { + if !(&s).contains("JISOKU METER") { + return Ok(FeatureResult::Skip); + } + } else { + return Ok(FeatureResult::Skip); + } + + if self.running { + return Ok(FeatureResult::Skip); + } + self.running = true; + thread::spawn(move || { + for s in MESSAGES.into_iter() { + a.send_message(m.chat.id(), (*s).to_owned(), None, None, None, None); + thread::sleep(Duration::new(1, 0)); + } + }); + Ok(FeatureResult::Handled) + } +} + +impl Jisoku { + pub fn new() -> Jisoku { + let mut j = Jisoku { running: false }; + j + } +} 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 @@ +use std::boxed::Box; +use std::error::Error; + +extern crate telegram_bot; +use telegram_bot::{Api, Message}; + +pub mod jisoku; +pub mod tasterank; + +pub enum FeatureResult { + Handled, + Skip, +} + +pub trait Feature { + fn name(&self) -> &'static str; + fn init(&mut self); + fn handle(&mut self, Api, Message) -> Result; +} + +pub fn init() -> Vec> { + vec![Box::new(tasterank::Tasterank::new()), Box::new(jisoku::Jisoku::new())] +} 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 @@ +use std::option::Option; +use std::io; +use std::io::prelude::*; +use std::fs::File; + +use std::ascii::AsciiExt; + +extern crate telegram_bot; +use telegram_bot::{Api, Message, MessageType}; + +use feature::FeatureResult; +use feature::Feature; + +pub struct Tasterank { + ranks: Vec, +} + +impl Feature for Tasterank { + fn name(&self) -> &'static str { + "Tasterank" + } + fn init(&mut self) { + if let Err(e) = self.update_ranks() { + panic!(e); + } + } + fn handle(&mut self, a: Api, m: Message) -> Result { + if let MessageType::Text(s) = m.msg { + if !(&s).to_ascii_lowercase().contains("luggas tasterank") { + return Ok(FeatureResult::Skip); + } + let mut msg = String::new(); + for (i, rank) in self.ranks.iter().enumerate() { + msg.push_str(&format!("{}. {}\n", i + 1, rank)); + } + a.send_message(m.chat.id(), msg, None, None, Some(m.message_id), None); + return Ok(FeatureResult::Handled); + } + Ok(FeatureResult::Skip) + } +} + +impl Tasterank { + pub fn new() -> Tasterank { + let mut t = Tasterank { ranks: vec![] }; + t.init(); + t + } + fn update_ranks(&mut self) -> Result<(), io::Error> { + let mut f = try!(File::open("data/tasterank.list")); + let mut buf = String::new(); + try!(f.read_to_string(&mut buf)); + let temp: Vec<&str> = (&buf).split("\n").collect(); + self.ranks.clear(); + for r in &temp { + self.ranks.push(r.to_string()); + } + self.ranks.pop().unwrap(); + Ok(()) + } +} 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 @@ extern crate telegram_bot; +use telegram_bot::{Api, ListeningMethod, ListeningAction, Update}; -use telegram_bot::{Api, ListeningMethod, ListeningAction, MessageType, Update}; +mod feature; +use feature::{Feature, FeatureResult}; fn main() { let api = Api::from_env("TELEGRAM_BOT_TOKEN").unwrap(); println!("api started: {:?}", api.get_me()); + let mut features = feature::init(); let mut listener = api.listener(ListeningMethod::LongPoll(None)); let res = listener.listen(|u| { if let Some(m) = u.message { - let name = m.from.first_name; - - println!("got message from {}", name); + for f in &mut features { + match (**f).handle(api.clone(), m.clone()) { + Ok(r) => { + match r { + FeatureResult::Handled => break, + FeatureResult::Skip => continue, + }; + } + Err(e) => { + println!("Error in feature {}: {}", f.name(), e); + } + }; + } }; Ok(ListeningAction::Continue) }); -- cgit v0.10.1