example_getting_started/main.rs
1///
2/// This is an example showcasing how to build a very simple bot using the
3/// matrix-sdk. To try it, you need a rust build setup, then you can run:
4/// `cargo run -p example-getting-started -- <homeserver_url> <user> <password>`
5///
6/// Use a second client to open a DM to your bot or invite them into some room.
7/// You should see it automatically join. Then post `!party` to see the client
8/// in action.
9///
10/// Below the code has a lot of inline documentation to help you understand the
11/// various parts and what they do
12// The imports we need
13use std::{env, process::exit};
14
15use matrix_sdk::{
16 Client, Room, RoomState,
17 config::SyncSettings,
18 ruma::events::room::{
19 member::StrippedRoomMemberEvent,
20 message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent},
21 },
22};
23use tokio::time::{Duration, sleep};
24
25/// This is the starting point of the app. `main` is called by rust binaries to
26/// run the program in this case, we use tokio (a reactor) to allow us to use
27/// an `async` function run.
28#[tokio::main]
29async fn main() -> anyhow::Result<()> {
30 // set up some simple stderr logging. You can configure it by changing the env
31 // var `RUST_LOG`
32 tracing_subscriber::fmt::init();
33
34 // parse the command line for homeserver, username and password
35 let (Some(homeserver_url), Some(username), Some(password)) =
36 (env::args().nth(1), env::args().nth(2), env::args().nth(3))
37 else {
38 eprintln!("Usage: {} <homeserver_url> <username> <password>", env::args().next().unwrap());
39 // exit if missing
40 exit(1)
41 };
42
43 // our actual runner
44 login_and_sync(homeserver_url, &username, &password).await?;
45 Ok(())
46}
47
48// The core sync loop we have running.
49async fn login_and_sync(
50 homeserver_url: String,
51 username: &str,
52 password: &str,
53) -> anyhow::Result<()> {
54 // First, we set up the client.
55
56 // Note that when encryption is enabled, you should use a persistent store to be
57 // able to restore the session with a working encryption setup.
58 // See the `persist_session` example.
59 let client = Client::builder()
60 // We use the convenient client builder to set our custom homeserver URL on it.
61 .homeserver_url(homeserver_url)
62 .build()
63 .await?;
64
65 // Then let's log that client in
66 client
67 .matrix_auth()
68 .login_username(username, password)
69 .initial_device_display_name("getting started bot")
70 .await?;
71
72 // It worked!
73 println!("logged in as {username}");
74
75 // Now, we want our client to react to invites. Invites sent us stripped member
76 // state events so we want to react to them. We add the event handler before
77 // the sync, so this happens also for older messages. All rooms we've
78 // already entered won't have stripped states anymore and thus won't fire
79 client.add_event_handler(on_stripped_state_member);
80
81 // An initial sync to set up state and so our bot doesn't respond to old
82 // messages. If the `StateStore` finds saved state in the location given the
83 // initial sync will be skipped in favor of loading state from the store
84 let sync_token = client.sync_once(SyncSettings::default()).await.unwrap().next_batch;
85
86 // now that we've synced, let's attach a handler for incoming room messages, so
87 // we can react on it
88 client.add_event_handler(on_room_message);
89
90 // since we called `sync_once` before we entered our sync loop we must pass
91 // that sync token to `sync`
92 let settings = SyncSettings::default().token(sync_token);
93 // this keeps state from the server streaming in to the bot via the
94 // EventHandler trait
95 client.sync(settings).await?; // this essentially loops until we kill the bot
96
97 Ok(())
98}
99
100// Whenever we see a new stripped room member event, we've asked our client to
101// call this function. So what exactly are we doing then?
102async fn on_stripped_state_member(
103 room_member: StrippedRoomMemberEvent,
104 client: Client,
105 room: Room,
106) {
107 if room_member.state_key != client.user_id().unwrap() {
108 // the invite we've seen isn't for us, but for someone else. ignore
109 return;
110 }
111
112 // The event handlers are called before the next sync begins, but
113 // methods that change the state of a room (joining, leaving a room)
114 // wait for the sync to return the new room state so we need to spawn
115 // a new task for them.
116 tokio::spawn(async move {
117 println!("Autojoining room {}", room.room_id());
118 let mut delay = 2;
119
120 while let Err(err) = room.join().await {
121 // retry autojoin due to synapse sending invites, before the
122 // invited user can join for more information see
123 // https://github.com/matrix-org/synapse/issues/4345
124 eprintln!("Failed to join room {} ({err:?}), retrying in {delay}s", room.room_id());
125
126 sleep(Duration::from_secs(delay)).await;
127 delay *= 2;
128
129 if delay > 3600 {
130 eprintln!("Can't join room {} ({err:?})", room.room_id());
131 break;
132 }
133 }
134 println!("Successfully joined room {}", room.room_id());
135 });
136}
137
138// This fn is called whenever we see a new room message event. You notice that
139// the difference between this and the other function that we've given to the
140// handler lies only in their input parameters. However, that is enough for the
141// rust-sdk to figure out which one to call and only do so, when the parameters
142// are available.
143async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
144 // First, we need to unpack the message: We only want messages from rooms we are
145 // still in and that are regular text messages - ignoring everything else.
146 if room.state() != RoomState::Joined {
147 return;
148 }
149 let MessageType::Text(text_content) = event.content.msgtype else { return };
150
151 // here comes the actual "logic": when the bot see's a `!party` in the message,
152 // it responds
153 if text_content.body.contains("!party") {
154 let content = RoomMessageEventContent::text_plain("🎉🎊🥳 let's PARTY!! 🥳🎊🎉");
155
156 println!("sending");
157
158 // send our message to the room we found the "!party" command in
159 room.send(content).await.unwrap();
160
161 println!("message sent");
162 }
163}