echobot example #1
|
@ -12,5 +12,5 @@ documentation = "https://docs.rs/cwtch-imp/"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
libcwtch = "0.4.0"
|
||||
libcwtch = "0.4.2"
|
||||
chrono = "0.4.19"
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
use std::borrow::BorrowMut;
|
||||
use std::thread;
|
||||
use chrono::{DateTime, FixedOffset};
|
||||
use libcwtch;
|
||||
use libcwtch::CwtchLib;
|
||||
use libcwtch::structs::*;
|
||||
use libcwtch::event::*;
|
||||
use cwtch_imp::imp;
|
||||
use cwtch_imp::behaviour::*;
|
||||
use cwtch_imp::imp::Imp;
|
||||
|
||||
const BOT_HOME: &str = "~/.cwtch/bots/echobot";
|
||||
const PASSWORD: &str = "be gay do crime";
|
||||
const BOT_NAME: &str = "echobot";
|
||||
|
||||
struct Echobot {}
|
||||
|
||||
fn main() {
|
||||
let behaviour: Behaviour = BehaviourBuilder::new().name(BOT_NAME.to_string()).new_contact_policy(NewContactPolicy::Accept).build();
|
||||
let event_loop_handle = thread::spawn(move || {
|
||||
let mut echobot = Echobot {};
|
||||
let mut bot = Imp::spawn(behaviour, PASSWORD.to_string(), BOT_HOME.to_string());
|
||||
bot.event_loop::<Echobot>(echobot.borrow_mut());
|
||||
});
|
||||
event_loop_handle.join().expect("Error running event loop");
|
||||
}
|
||||
|
||||
impl imp::EventHandler for Echobot {
|
||||
fn on_new_message_from_contact(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, conversation_id: ConversationID, handle: String, timestamp_received: DateTime<FixedOffset>, message: Message) {
|
||||
let response = Message {
|
||||
o: MessageType::TextMessage,
|
||||
d: message.d,
|
||||
};
|
||||
cwtch.send_message(&profile.profile_id, conversation_id, &response);
|
||||
}
|
||||
|
||||
fn handle(&mut self, cwtch: &dyn CwtchLib, profile_opt: Option<&Profile>, event: &Event) {
|
||||
match event {
|
||||
Event::NewPeer { profile_id, tag, created, name, default_picture, picture, online, profile_data } => {
|
||||
println!(
|
||||
"\n***** {} at {} *****\n",
|
||||
name, profile_id.as_str()
|
||||
);
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue