echobot/
echobot.rs

1use std::borrow::BorrowMut;
2use std::thread;
3use chrono::{DateTime, FixedOffset};
4use libcwtch;
5use libcwtch::CwtchLib;
6use libcwtch::structs::*;
7use libcwtch::event::*;
8use cwtch_imp::imp;
9use cwtch_imp::behaviour::*;
10use cwtch_imp::imp::Imp;
11
12const BOT_HOME: &str = "~/.cwtch/bots/echobot";
13const PASSWORD: &str = "be gay do crime";
14const BOT_NAME: &str = "echobot";
15
16struct Echobot {}
17
18fn main() {
19    let behaviour: Behaviour = BehaviourBuilder::new().name(BOT_NAME.to_string()).new_contact_policy(NewContactPolicy::Accept).contact_interaction_policy(ContactInteractionPolicy::Accept).build();
20    let event_loop_handle = thread::spawn(move || {
21        let mut echobot = Echobot {};
22        let mut bot = Imp::spawn(behaviour, PASSWORD.to_string(), BOT_HOME.to_string());
23        bot.event_loop::<Echobot>(echobot.borrow_mut());
24    });
25    event_loop_handle.join().expect("Error running event loop");
26}
27
28impl imp::EventHandler for Echobot {
29    fn on_new_message_from_contact(&mut self, cwtch: &dyn libcwtch::CwtchLib, behaviour: &mut Behaviour, profile: &Profile, conversation_id: ConversationID, _handle: String, _timestamp_received: DateTime<FixedOffset>, message: MessageWrapper) {
30        let response = MessageWrapper {
31            o: MessageType::TextMessage,
32            d: message.d,
33        };
34        if let Err(e) = cwtch.send_message(&profile.profile_id, conversation_id, &response) {
35            println!("Error sending message: {}", e.to_string());
36        }
37    }
38
39    fn handle(&mut self, _cwtch: &dyn CwtchLib, _profile_opt: Option<&Profile>, event: &Event) {
40        match event {
41            Event::NewPeer { profile_id, tag: _, created: _, name, default_picture: _, picture: _, online: _, profile_data: _ } => {
42                println!(
43                    "\n***** {} at {} *****\n",
44                    name, profile_id.as_str()
45                );
46            }
47            _ => (),
48        };
49    }
50}