2021-09-16 02:39:30 +00:00
use std ::thread ;
2021-09-08 07:32:56 +00:00
use libcwtch ;
2021-09-16 02:39:30 +00:00
use libcwtch ::structs ::* ;
2021-09-08 07:32:56 +00:00
use libcwtch ::CwtchLib ;
2022-07-21 08:04:21 +00:00
use libcwtch ::event ::Event ;
2021-09-08 07:32:56 +00:00
fn main ( ) {
2021-09-16 02:39:30 +00:00
let bot_home = " example_cwtch_dir " ;
2022-01-15 14:10:55 +00:00
match std ::fs ::remove_dir_all ( & bot_home ) {
2022-01-17 23:24:58 +00:00
// this will always error on first run as we haven't created the directory, it's just a function to clear it on successive runs, so this error is to be ignored
2022-01-16 19:06:30 +00:00
_ = > ( ) ,
2022-01-15 14:10:55 +00:00
}
2021-09-16 02:39:30 +00:00
std ::fs ::create_dir_all ( & bot_home ) . expect ( " Error creating bot_home directory " ) ;
2021-09-08 07:32:56 +00:00
let cwtch = libcwtch ::new_cwtchlib_go ( ) ;
println! ( " start_cwtch " ) ;
2021-09-16 02:39:30 +00:00
let ret = cwtch . start_cwtch ( bot_home , " " ) ;
2021-09-08 07:32:56 +00:00
println! ( " start_cwtch returned {} " , ret ) ;
let event_loop_handle = thread ::spawn ( move | | {
loop {
2022-07-21 08:04:21 +00:00
let event = cwtch . get_appbus_event ( ) ;
println! ( " event: {:?} " , event ) ;
2021-09-08 07:32:56 +00:00
2022-07-21 08:04:21 +00:00
match event {
Event ::CwtchStarted = > {
2021-09-08 07:32:56 +00:00
println! ( " event CwtchStarted! " ) ;
println! ( " Creating bot " ) ;
2023-05-07 18:21:14 +00:00
cwtch . create_profile ( " Echobot " , " be gay do crime " , true ) ;
2021-09-16 02:39:30 +00:00
}
2022-07-21 08:04:21 +00:00
Event ::NewPeer { profile_id , tag , created , name , default_picture , picture , online , profile_data } = > {
2021-09-16 02:39:30 +00:00
println! (
" \n ***** {} at {} ***** \n " ,
2022-07-21 08:04:21 +00:00
name , profile_id . as_str ( )
2021-09-16 02:39:30 +00:00
) ;
2021-09-08 07:32:56 +00:00
// process json for profile, contacts and servers...else {
2022-07-21 08:04:21 +00:00
let profile = profile_data ;
2021-09-08 07:32:56 +00:00
print! ( " profile: {:?} " , profile ) ;
}
2022-07-21 08:04:21 +00:00
Event ::NewMessageFromPeer { profile_id , conversation_id , contact_id : contact , nick , timestamp_received , message , notification , picture } = > {
2023-05-07 18:21:14 +00:00
let response = MessageWrapper { o : message . o . into ( ) , d : message . d } ;
2023-04-25 13:35:39 +00:00
cwtch . send_message ( & profile_id , conversation_id , & response ) ;
2021-09-08 07:32:56 +00:00
}
2021-09-16 02:39:30 +00:00
_ = > eprintln! ( " unhandled event! " ) ,
2021-09-08 07:32:56 +00:00
} ;
2021-09-16 02:39:30 +00:00
}
2021-09-08 07:32:56 +00:00
} ) ;
2021-09-16 02:39:30 +00:00
event_loop_handle . join ( ) . expect ( " Error running event loop " ) ;
2021-09-08 07:32:56 +00:00
}