2020-04-14 12:39:51 +00:00
|
|
|
use std::{env, process::exit};
|
2020-04-15 10:58:25 +00:00
|
|
|
|
2020-04-14 12:39:51 +00:00
|
|
|
use matrix_sdk::{
|
2021-01-04 15:23:44 +00:00
|
|
|
self, async_trait,
|
2020-06-20 21:18:20 +00:00
|
|
|
events::{
|
2021-03-01 17:30:20 +00:00
|
|
|
room::message::{MessageEventContent, MessageType, TextMessageEventContent},
|
2020-08-26 13:41:27 +00:00
|
|
|
AnyMessageEventContent, SyncMessageEvent,
|
2020-06-20 21:18:20 +00:00
|
|
|
},
|
2021-03-08 23:12:59 +00:00
|
|
|
room::Joined,
|
|
|
|
Client, ClientConfig, EventHandler, Room, SyncSettings,
|
2020-04-14 12:39:51 +00:00
|
|
|
};
|
2020-04-15 11:52:29 +00:00
|
|
|
use url::Url;
|
2020-04-14 12:39:51 +00:00
|
|
|
|
|
|
|
struct CommandBot {
|
2020-05-08 12:02:49 +00:00
|
|
|
/// This clone of the `Client` will send requests to the server,
|
2020-10-06 13:04:43 +00:00
|
|
|
/// while the other keeps us in sync with the server using `sync`.
|
2020-05-08 12:02:49 +00:00
|
|
|
client: Client,
|
2020-04-14 12:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CommandBot {
|
2020-05-08 12:02:49 +00:00
|
|
|
pub fn new(client: Client) -> Self {
|
2020-04-15 12:44:29 +00:00
|
|
|
Self { client }
|
2020-04-14 12:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 16:52:53 +00:00
|
|
|
#[async_trait]
|
2021-02-13 10:01:31 +00:00
|
|
|
impl EventHandler for CommandBot {
|
2021-03-05 11:00:43 +00:00
|
|
|
async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) {
|
2021-03-08 23:12:59 +00:00
|
|
|
if let Some(room) = Joined::new(self.client.clone(), room) {
|
2020-07-18 12:37:43 +00:00
|
|
|
let msg_body = if let SyncMessageEvent {
|
2021-03-01 17:30:20 +00:00
|
|
|
content:
|
|
|
|
MessageEventContent {
|
|
|
|
msgtype: MessageType::Text(TextMessageEventContent { body: msg_body, .. }),
|
|
|
|
..
|
|
|
|
},
|
2020-05-06 23:45:27 +00:00
|
|
|
..
|
|
|
|
} = event
|
|
|
|
{
|
2021-03-01 17:30:20 +00:00
|
|
|
msg_body
|
2020-05-06 23:45:27 +00:00
|
|
|
} else {
|
2021-03-01 17:30:20 +00:00
|
|
|
return;
|
2020-05-06 23:45:27 +00:00
|
|
|
};
|
2020-04-14 22:10:10 +00:00
|
|
|
|
2020-05-06 23:45:27 +00:00
|
|
|
if msg_body.contains("!party") {
|
2020-11-25 17:50:50 +00:00
|
|
|
let content = AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain(
|
|
|
|
"🎉🎊🥳 let's PARTY!! 🥳🎊🎉",
|
2020-08-26 13:41:27 +00:00
|
|
|
));
|
2020-04-14 22:10:10 +00:00
|
|
|
|
2020-05-06 23:45:27 +00:00
|
|
|
println!("sending");
|
2020-04-14 22:10:10 +00:00
|
|
|
|
2021-03-08 23:12:59 +00:00
|
|
|
// send our message to the room we found the "!party" command in
|
|
|
|
// the last parameter is an optional Uuid which we don't care about.
|
|
|
|
room.send(content, None).await.unwrap();
|
2020-04-14 22:10:10 +00:00
|
|
|
|
2020-05-06 23:45:27 +00:00
|
|
|
println!("message sent");
|
|
|
|
}
|
2020-04-14 12:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn login_and_sync(
|
|
|
|
homeserver_url: String,
|
|
|
|
username: String,
|
|
|
|
password: String,
|
|
|
|
) -> Result<(), matrix_sdk::Error> {
|
2020-04-26 21:27:06 +00:00
|
|
|
// the location for `JsonStore` to save files to
|
2020-04-25 10:26:35 +00:00
|
|
|
let mut home = dirs::home_dir().expect("no home directory found");
|
|
|
|
home.push("party_bot");
|
|
|
|
|
2021-01-22 09:01:21 +00:00
|
|
|
let client_config = ClientConfig::new().store_path(home);
|
2020-04-15 10:58:25 +00:00
|
|
|
|
2020-05-08 10:39:36 +00:00
|
|
|
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
|
2020-05-08 12:02:49 +00:00
|
|
|
// create a new Client with the given homeserver url and config
|
2020-12-19 19:20:39 +00:00
|
|
|
let client = Client::new_with_config(homeserver_url, client_config).unwrap();
|
2020-04-14 12:39:51 +00:00
|
|
|
|
|
|
|
client
|
2020-08-15 01:09:13 +00:00
|
|
|
.login(&username, &password, None, Some("command bot"))
|
2020-04-14 12:39:51 +00:00
|
|
|
.await?;
|
|
|
|
|
2020-04-14 18:49:29 +00:00
|
|
|
println!("logged in as {}", username);
|
2020-04-14 12:39:51 +00:00
|
|
|
|
2020-04-26 21:27:06 +00:00
|
|
|
// An initial sync to set up state and so our bot doesn't respond to old messages.
|
|
|
|
// If the `StateStore` finds saved state in the location given the initial sync will
|
|
|
|
// be skipped in favor of loading state from the store
|
2020-10-06 09:37:29 +00:00
|
|
|
client.sync_once(SyncSettings::default()).await.unwrap();
|
2020-04-15 12:29:34 +00: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
|
2021-02-13 10:01:31 +00:00
|
|
|
.set_event_handler(Box::new(CommandBot::new(client.clone())))
|
2020-04-15 12:29:34 +00:00
|
|
|
.await;
|
|
|
|
|
2020-10-06 13:04:43 +00:00
|
|
|
// since we called `sync_once` before we entered our sync loop we must pass
|
|
|
|
// that sync token to `sync`
|
2020-04-15 13:54:53 +00:00
|
|
|
let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
|
2021-02-13 10:01:31 +00:00
|
|
|
// this keeps state from the server streaming in to CommandBot via the EventHandler trait
|
2020-10-06 09:37:29 +00:00
|
|
|
client.sync(settings).await;
|
2020-04-14 12:39:51 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-14 22:14:14 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), matrix_sdk::Error> {
|
2020-04-23 08:52:47 +00:00
|
|
|
tracing_subscriber::fmt::init();
|
|
|
|
|
2020-04-14 12:39:51 +00: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 22:10:10 +00:00
|
|
|
|
2020-04-14 22:14:14 +00:00
|
|
|
login_and_sync(homeserver_url, username, password).await?;
|
2020-04-14 18:49:29 +00:00
|
|
|
Ok(())
|
2020-04-14 12:39:51 +00:00
|
|
|
}
|