integrates changes to echobot example

This commit is contained in:
Sam Schlinkert 2021-09-16 14:03:57 -04:00
commit 47d9a9b7ab
1 changed files with 14 additions and 15 deletions

View File

@ -5,16 +5,13 @@ use libcwtch::structs::*;
use libcwtch::CwtchLib; use libcwtch::CwtchLib;
fn main() { fn main() {
let bot_home: String = "example_cwtch_dir".to_string(); let bot_home = "example_cwtch_dir";
match std::fs::remove_dir_all(&bot_home) { std::fs::remove_dir_all(&bot_home).expect("Error removing previous bot_home directory");
Err(_e) => (), std::fs::create_dir_all(&bot_home).expect("Error creating bot_home directory");
_ => (),
}
std::fs::create_dir_all(&bot_home).unwrap();
let cwtch = libcwtch::new_cwtchlib_go(); let cwtch = libcwtch::new_cwtchlib_go();
println!("start_cwtch"); println!("start_cwtch");
let ret = cwtch.start_cwtch(bot_home.as_str(), ""); let ret = cwtch.start_cwtch(bot_home, "");
println!("start_cwtch returned {}", ret); println!("start_cwtch returned {}", ret);
let event_loop_handle = thread::spawn(move || { let event_loop_handle = thread::spawn(move || {
@ -22,7 +19,8 @@ fn main() {
let event_str = cwtch.get_appbus_event(); let event_str = cwtch.get_appbus_event();
println!("event: {}", event_str); println!("event: {}", event_str);
let event: CwtchEvent = serde_json::from_str(&event_str).unwrap(); let event: CwtchEvent =
serde_json::from_str(&event_str).expect("Error parsing Cwtch event");
match event.event_type.as_str() { match event.event_type.as_str() {
"CwtchStarted" => { "CwtchStarted" => {
println!("event CwtchStarted!"); println!("event CwtchStarted!");
@ -46,19 +44,20 @@ fn main() {
print!("profile: {:?}", profile); print!("profile: {:?}", profile);
} }
"NewMessageFromPeer" => { "NewMessageFromPeer" => {
let to = event.data["ProfileOnion"].to_string(); let to = &event.data["ProfileOnion"];
let conversation = event.data["RemotePeer"].to_string(); let conversation = &event.data["RemotePeer"];
let message: Message = let message: Message =
serde_json::from_str(event.data["Data"].as_str()).unwrap(); serde_json::from_str(&event.data["Data"]).expect("Error parsing message");
let response = Message { o: 1, d: message.d }; let response = Message { o: 1, d: message.d };
let response_json = serde_json::to_string(&response).unwrap(); let response_json =
cwtch.send_message(to.as_str(), conversation.as_str(), response_json.as_str()); serde_json::to_string(&response).expect("Error parsing json response");
cwtch.send_message(&to, &conversation, &response_json);
} }
_ => println!("unhandled event!"), _ => eprintln!("unhandled event!"),
}; };
} }
}); });
event_loop_handle.join().unwrap(); event_loop_handle.join().expect("Error running event loop");
} }