tg-sync-rs

git clone git://archive.git.mtrnord.blog/Nordgedanken/tg-sync-rs.git
Log | Files | Refs | LICENSE

main.rs (2442B)


      1 extern crate telebot;
      2 extern crate slack;
      3 extern crate tokio_core;
      4 extern crate futures;
      5 
      6 use telebot::bot;
      7 use tokio_core::reactor::Core;
      8 use futures::stream::Stream;
      9 
     10 // import all available functions
     11 use telebot::functions::*;
     12 
     13 struct MyHandler;
     14 
     15 #[allow(unused_variables)]
     16 impl slack::EventHandler for MyHandler {
     17     fn on_event(&mut self, cli: &mut slack::RtmClient, event: Result<slack::Event, slack::Error>, raw_json: &str) {
     18         println!("on_event(event: {:?}, raw_json: {:?})", event, raw_json);
     19     }
     20 
     21     fn on_ping(&mut self, cli: &mut slack::RtmClient) {
     22         println!("on_ping");
     23     }
     24 
     25     fn on_close(&mut self, cli: &mut slack::RtmClient) {
     26         println!("on_close");
     27     }
     28 
     29     fn on_connect(&mut self, cli: &mut slack::RtmClient) {
     30         println!("on_connect");
     31         // Do a few things using the api:
     32         // send a message over the real time api websocket
     33         let _ = cli.send_message("#testing_syncotronic", "Hello world! (rtm)");
     34         // post a message as a user to the web api
     35         let _ = cli.post_message("#testing_syncotronic", "hello world! (postMessage)", None);
     36         // set a channel topic via the web api
     37         // let _ = cli.set_topic("#general", "bots rule!");
     38     }
     39 }
     40 
     41 fn main() {
     42     slack();
     43     tg();
     44 }
     45 
     46 fn slack() {
     47     let args: Vec<String> = std::env::args().collect();
     48     let api_key = match args.len() {
     49         0 | 1 => panic!("No api-key in args! Usage: cargo run -- <api-key>"),
     50         x => {
     51             args[x - 1].clone()
     52         }
     53     };
     54     let mut handler = MyHandler;
     55     let mut cli = slack::RtmClient::new(&api_key);
     56     let r = cli.login_and_run::<MyHandler>(&mut handler);
     57     match r {
     58         Ok(_) => {}
     59         Err(err) => panic!("Error: {}", err),
     60     }
     61     println!("{}", cli.get_name().unwrap());
     62     println!("{}", cli.get_team().unwrap().name);
     63 }
     64 
     65 fn tg() {
     66     let mut lp = Core::new().unwrap();
     67     let bot = bot::RcBot::new(lp.handle(), "315066220:AAFGOUmSvq0T725a6Ke4C-zmfx80m_PcsxM")
     68         .update_interval(200);
     69 
     70     let handle = bot.new_cmd("/reply")
     71         .and_then(|(bot, msg)| {
     72             let mut text = msg.text.unwrap().clone();
     73             if text.is_empty() {
     74                 text = "<empty>".into();
     75             }
     76 
     77             bot.message(msg.chat.id, text).send()
     78         });
     79 
     80     bot.register(handle);
     81 
     82     bot.run(&mut lp).unwrap();
     83 }