From c84e0af26f63703f16e6333f0d32a5494fce0a96 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Tue, 25 Apr 2023 12:04:12 -0600 Subject: [PATCH] echobot example --- Cargo.toml | 2 +- examples/echobot.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 examples/echobot.rs diff --git a/Cargo.toml b/Cargo.toml index dd92d85..d90206c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/echobot.rs b/examples/echobot.rs new file mode 100644 index 0000000..595c95c --- /dev/null +++ b/examples/echobot.rs @@ -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.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, 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() + ); + } + _ => (), + }; + } +}