2020-04-14 08:39:51 -04:00
|
|
|
use std::{env, process::exit};
|
2020-04-15 06:58:25 -04:00
|
|
|
|
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::{
|
2022-11-04 12:43:18 +01:00
|
|
|
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
|
2023-10-30 17:31:02 +01:00
|
|
|
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<()> {
|
2023-02-23 12:09:11 +01:00
|
|
|
// 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();
|
2023-06-14 16:13:28 +02:00
|
|
|
client
|
|
|
|
|
.matrix_auth()
|
|
|
|
|
.login_username(&username, &password)
|
|
|
|
|
.initial_device_display_name("command bot")
|
|
|
|
|
.await?;
|
2020-04-14 08:39:51 -04:00
|
|
|
|
2022-07-25 20:19:47 +02: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
|
2023-02-23 12:09:11 +01:00
|
|
|
// messages.
|
2022-11-17 18:47:26 +01:00
|
|
|
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.
|
2022-08-04 16:24:59 +02:00
|
|
|
client.add_event_handler(on_room_message);
|
2020-04-15 08:29:34 -04:00
|
|
|
|
2020-10-06 15:04:43 +02:00
|
|
|
// since we called `sync_once` before we entered our sync loop we must pass
|
|
|
|
|
// that sync token to `sync`
|
2022-11-17 18:47:26 +01:00
|
|
|
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
|
2022-09-26 17:14:15 +02:00
|
|
|
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<()> {
|
2020-04-23 10:52:47 +02:00
|
|
|
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:10:10 -04:00
|
|
|
|
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
|
|
|
}
|