1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use mxlink::helpers::encryption::EncryptionKey;
use mxlink::{InvitationDecision, MessageResponseType};
use mxlink::{InitConfig, LoginConfig, LoginCredentials, LoginEncryption, MatrixLink, PersistenceConfig};

// You can run this example either by modifying the configuration below,
// or against the Synapse server provided by baibot (https://github.com/etkecc/baibot/) - see its docs/development.md guide.

const HOMESERVER_URL: &str = "http://synapse.127.0.0.1.nip.io:42020";

const LOGIN_USERNAME: &str = "baibot";
const LOGIN_PASSWORD: &str = "baibot";
const LOGIN_ENCRYPTION_RECOVERY_PASSPHRASE: &str = "long-and-secure-passphrase-here";
const LOGIN_ENCRYPTION_RESET_ALLOWED: bool = false;

const DEVICE_DISPLAY_NAME: &str = LOGIN_USERNAME;

const PERSISTENCE_SESSION_FILE_PATH: &str = "/tmp/mxlink-session.json";
const PERSISTENCE_SESSION_ENCRYPTION_KEY: &str =
    "ef4d037845e5591c6627122112b7f30b2154e7354f928ff75e4dda206ba84338";
const PERSISTENCE_DB_DIR_PATH: &str = "/tmp/mxlink-db";

#[tokio::main]
async fn main() {
    let matrix_link = create_matrix_link().await;

    // matrix_link can be cloned freely
    register_event_handlers(matrix_link.clone()).await;

    matrix_link
        .start()
        .await
        .expect("Failed to start MatrixLink");

    println!("Done");
}

async fn create_matrix_link() -> MatrixLink {
    let login_creds =
        LoginCredentials::UserPassword(LOGIN_USERNAME.to_owned(), LOGIN_PASSWORD.to_owned());

    let login_encryption = LoginEncryption::new(
        Some(LOGIN_ENCRYPTION_RECOVERY_PASSPHRASE.to_owned()),
        LOGIN_ENCRYPTION_RESET_ALLOWED,
    );

    let login_config = LoginConfig::new(
        HOMESERVER_URL.to_owned(),
        login_creds,
        Some(login_encryption),
        DEVICE_DISPLAY_NAME.to_owned(),
    );

    let session_file_path = std::path::PathBuf::from(PERSISTENCE_SESSION_FILE_PATH);
    let session_encryption_key = EncryptionKey::from_hex_str(PERSISTENCE_SESSION_ENCRYPTION_KEY)
        .expect("Invalid encryption key hex string");
    let db_dir_path = std::path::PathBuf::from(PERSISTENCE_DB_DIR_PATH);

    let persistence_config =
        PersistenceConfig::new(session_file_path, Some(session_encryption_key), db_dir_path);

    let init_config = InitConfig::new(login_config, persistence_config);

    mxlink::init(&init_config)
        .await
        .expect("Failed to initialize MatrixLink")
}

async fn register_event_handlers(matrix_link: MatrixLink) {
    let rooms = matrix_link.rooms();

    // We auto-accept all invitations
    rooms.on_invitation(|_event, _room| async move { Ok(InvitationDecision::Join) });

    // We send an introduction to all rooms we join
    let messaging = matrix_link.messaging();
    rooms.on_joined(|_event, room| async move {
        let _ = messaging
            .send_text_markdown(&room, "Hello!".to_owned(), MessageResponseType::InRoom)
            .await
            .expect("Failed to send message");

        Ok(())
    });

    // We listen for reactions to messages and send a reply to the original message that received the reaction
    let messaging = matrix_link.messaging();
    let reacting = matrix_link.reacting();
    reacting.on_actionable_reaction(|event, room, reaction_event_content| async move {
        let response_text = if reaction_event_content.relates_to.key == "👍️" {
            format!("{} reacted to this message with thumbs up!", event.sender())
        } else {
            format!(
                "{} reacted to this message with an unknown-to-me reaction ({}).",
                event.sender(),
                reaction_event_content.relates_to.key,
            )
        };

        let response_type =
            MessageResponseType::Reply(reaction_event_content.relates_to.event_id.clone());

        let _ = messaging
            .send_notice_markdown(&room, response_text, response_type)
            .await
            .expect("Failed to send message");

        Ok(())
    });
}