Files
matrix-rust-sdk/examples/command_bot/src/main.rs
T

84 lines
2.6 KiB
Rust
Raw Normal View History

2020-04-14 08:39:51 -04:00
use std::{env, process::exit};
2020-04-14 08:39:51 -04:00
use matrix_sdk::{
2022-03-11 18:29:43 +01:00
config::SyncSettings,
2021-10-01 16:49:10 +02:00
ruma::events::room::message::{
MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent,
2020-06-20 17:18:20 -04:00
},
2023-07-17 17:33:37 +02:00
Client, Room, RoomState,
2020-04-14 08:39:51 -04:00
};
2023-07-17 17:33:37 +02:00
async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
2023-07-17 17:06:27 +02:00
if room.state() != RoomState::Joined {
return;
}
let MessageType::Text(text_content) = event.content.msgtype else {
return;
};
2020-04-14 08:39:51 -04:00
2023-07-17 17:06:27 +02:00
if text_content.body.contains("!party") {
let content = RoomMessageEventContent::text_plain("🎉🎊🥳 let's PARTY!! 🥳🎊🎉");
2020-04-14 08:39:51 -04:00
2023-07-17 17:06:27 +02:00
println!("sending");
2021-06-22 11:36:33 +02:00
2023-07-17 17:06:27 +02:00
// send our message to the room we found the "!party" command in
room.send(content).await.unwrap();
2021-06-22 11:36:33 +02:00
2023-07-17 17:06:27 +02:00
println!("message sent");
2020-04-14 08:39:51 -04:00
}
}
async fn login_and_sync(
homeserver_url: String,
username: String,
password: String,
2022-03-11 18:29:43 +01:00
) -> anyhow::Result<()> {
// Note that when encryption is enabled, you should use a persistent store to be
// able to restore the session with a working encryption setup.
// See the `persist_session` example.
let client = Client::builder().homeserver_url(homeserver_url).build().await.unwrap();
client
.matrix_auth()
.login_username(&username, &password)
.initial_device_display_name("command bot")
.await?;
2020-04-14 08:39:51 -04:00
println!("logged in as {username}");
2020-04-14 08:39:51 -04:00
2021-05-12 13:20:52 -04:00
// An initial sync to set up state and so our bot doesn't respond to old
// messages.
let response = client.sync_once(SyncSettings::default()).await.unwrap();
2021-05-12 13:20:52 -04:00
// add our CommandBot to be notified of incoming messages, we do this after the
// initial sync to avoid responding to messages before the bot was running.
client.add_event_handler(on_room_message);
// since we called `sync_once` before we entered our sync loop we must pass
// that sync token to `sync`
let settings = SyncSettings::default().token(response.next_batch);
2021-05-12 13:20:52 -04:00
// this keeps state from the server streaming in to CommandBot via the
// EventHandler trait
client.sync(settings).await?;
2020-04-14 08:39:51 -04:00
Ok(())
}
2020-04-14 18:14:14 -04:00
#[tokio::main]
2022-03-11 18:29:43 +01:00
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
2020-04-14 08:39:51 -04:00
let (homeserver_url, username, password) =
match (env::args().nth(1), env::args().nth(2), env::args().nth(3)) {
(Some(a), Some(b), Some(c)) => (a, b, c),
_ => {
eprintln!(
"Usage: {} <homeserver_url> <username> <password>",
env::args().next().unwrap()
);
exit(1)
}
};
2020-04-14 18:14:14 -04:00
login_and_sync(homeserver_url, username, password).await?;
2020-04-14 14:49:29 -04:00
Ok(())
2020-04-14 08:39:51 -04:00
}