Commit 7e050e76 authored by ootoovak's avatar ootoovak

Running both libraries at once.

I had both the Slack Client library and the Rocket Web Framework running
by themselves but one or both of them had blocking code. I used threads
to get them both running at the same time.
parent 093bfd09
......@@ -4,6 +4,7 @@
extern crate rocket;
extern crate slack;
use std::thread;
use std::env;
struct Botket;
......@@ -14,21 +15,21 @@ impl slack::EventHandler for Botket {
cli: &mut slack::RtmClient,
event: Result<slack::Event, slack::Error>,
raw_json: &str) {
println!("on_event(event: {:?}, raw_json: {:?})", event, raw_json);
println!("Event:");
println!("\t{:?}", event);
println!("\t{:?}", raw_json);
}
fn on_ping(&mut self, cli: &mut slack::RtmClient) {
println!("on_ping");
println!("--- pinged");
}
fn on_close(&mut self, cli: &mut slack::RtmClient) {
println!("on_close");
println!("Close action.");
}
fn on_connect(&mut self, cli: &mut slack::RtmClient) {
println!("on_connect");
let _ = cli.send_message("#general", "Hello world! (rtm)");
let _ = cli.post_message("#general", "hello world! (postMessage)", None);
let _ = cli.send_message("#general", "I am awake.");
}
}
......@@ -38,16 +39,21 @@ fn root(message: &str) -> String {
}
fn main() {
let api_key = env::var("SLACK_API_KEY").unwrap();
let mut handler = Botket;
let mut cli = slack::RtmClient::new(&api_key);
let result = cli.login_and_run::<Botket>(&mut handler);
match result {
Ok(_) => {}
Err(err) => panic!("Error: {}", err),
}
println!("{}", cli.get_name().unwrap());
println!("{}", cli.get_team().unwrap().name);
rocket::ignite().mount("/", routes![root]).launch();
let slack_handle = thread::spawn(|| {
println!("===== Starting Slack Client Thread =====");
let api_key = env::var("SLACK_API_KEY").unwrap();
let mut handler = Botket;
let mut cli = slack::RtmClient::new(&api_key);
let _ = cli.login_and_run::<Botket>(&mut handler);
"===== Done Slack Client Thread ====="
});
let rocket_handle = thread::spawn(|| {
println!("===== Starting Rocket Thread =====");
rocket::ignite().mount("/", routes![root]).launch();
"===== Done Rocket Thread ====="
});
println!("{}", slack_handle.join().unwrap());
println!("{}", rocket_handle.join().unwrap());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment