2020-04-15 10:58:25 +00:00
|
|
|
use std::sync::Arc;
|
2020-04-15 11:52:29 +00:00
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
2020-04-14 12:39:51 +00:00
|
|
|
use std::{env, process::exit};
|
2020-04-15 10:58:25 +00:00
|
|
|
|
2020-04-15 11:52:29 +00:00
|
|
|
use js_int::UInt;
|
2020-04-14 12:39:51 +00:00
|
|
|
use matrix_sdk::{
|
|
|
|
self,
|
|
|
|
events::room::message::{MessageEvent, MessageEventContent, TextMessageEventContent},
|
|
|
|
AsyncClient, AsyncClientConfig, EventEmitter, Room, SyncSettings,
|
|
|
|
};
|
2020-04-15 12:29:34 +00:00
|
|
|
use tokio::sync::RwLock;
|
2020-04-15 11:52:29 +00:00
|
|
|
use url::Url;
|
2020-04-14 12:39:51 +00:00
|
|
|
|
|
|
|
struct CommandBot {
|
2020-04-15 11:52:29 +00:00
|
|
|
/// This clone of the `AsyncClient` will send requests to the server,
|
|
|
|
/// while the other keeps us in sync with the server using `sync_forever`.
|
2020-04-15 12:29:34 +00:00
|
|
|
client: AsyncClient,
|
2020-04-15 11:52:29 +00:00
|
|
|
/// A timestamp so we only respond to messages sent after the bot is running.
|
|
|
|
start_time: UInt,
|
2020-04-14 12:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CommandBot {
|
2020-04-14 18:49:29 +00:00
|
|
|
pub fn new(client: AsyncClient) -> Self {
|
2020-04-15 11:52:29 +00:00
|
|
|
let now = SystemTime::now();
|
|
|
|
let timestamp = now.duration_since(UNIX_EPOCH).unwrap().as_millis();
|
2020-04-14 22:10:10 +00:00
|
|
|
Self {
|
2020-04-15 12:29:34 +00:00
|
|
|
client,
|
2020-04-15 11:52:29 +00:00
|
|
|
start_time: UInt::new(timestamp as u64).unwrap(),
|
2020-04-14 22:10:10 +00:00
|
|
|
}
|
2020-04-14 12:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl EventEmitter for CommandBot {
|
2020-04-15 10:58:25 +00:00
|
|
|
async fn on_room_message(&self, room: Arc<RwLock<Room>>, event: &MessageEvent) {
|
2020-04-15 11:52:29 +00:00
|
|
|
let (msg_body, timestamp) = if let MessageEvent {
|
2020-04-14 12:39:51 +00:00
|
|
|
content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
|
2020-04-15 11:52:29 +00:00
|
|
|
origin_server_ts,
|
2020-04-14 12:39:51 +00:00
|
|
|
..
|
2020-04-14 22:10:10 +00:00
|
|
|
} = event
|
2020-04-14 12:39:51 +00:00
|
|
|
{
|
2020-04-15 11:52:29 +00:00
|
|
|
(msg_body.clone(), *origin_server_ts)
|
2020-04-14 22:10:10 +00:00
|
|
|
} else {
|
2020-04-15 11:52:29 +00:00
|
|
|
(String::new(), UInt::min_value())
|
2020-04-14 22:10:10 +00:00
|
|
|
};
|
|
|
|
|
2020-04-15 11:52:29 +00:00
|
|
|
if msg_body.contains("!party") && timestamp > self.start_time {
|
2020-04-14 22:10:10 +00:00
|
|
|
let content = MessageEventContent::Text(TextMessageEventContent {
|
|
|
|
body: "🎉🎊🥳 let's PARTY!! 🥳🎊🎉".to_string(),
|
|
|
|
format: None,
|
|
|
|
formatted_body: None,
|
|
|
|
relates_to: None,
|
|
|
|
});
|
2020-04-15 11:52:29 +00:00
|
|
|
// we clone here to hold the lock for as little time as possible.
|
|
|
|
let room_id = room.read().await.room_id.clone();
|
2020-04-14 22:10:10 +00:00
|
|
|
|
|
|
|
println!("sending");
|
|
|
|
|
|
|
|
self.client
|
2020-04-15 11:52:29 +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.
|
2020-04-14 22:10:10 +00:00
|
|
|
.room_send(&room_id, content, None)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
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-15 10:58:25 +00:00
|
|
|
let client_config = AsyncClientConfig::new()
|
|
|
|
.proxy("http://localhost:8080")?
|
|
|
|
.disable_ssl_verification();
|
|
|
|
|
2020-04-14 12:39:51 +00:00
|
|
|
let homeserver_url = Url::parse(&homeserver_url)?;
|
2020-04-15 11:52:29 +00:00
|
|
|
// create a new AsyncClient with the given homeserver url and config
|
2020-04-14 12:39:51 +00:00
|
|
|
let mut client = AsyncClient::new_with_config(homeserver_url, None, client_config).unwrap();
|
|
|
|
|
|
|
|
client
|
|
|
|
.login(
|
|
|
|
username.clone(),
|
|
|
|
password,
|
|
|
|
None,
|
|
|
|
Some("command bot".to_string()),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2020-04-14 18:49:29 +00:00
|
|
|
println!("logged in as {}", username);
|
2020-04-14 12:39:51 +00:00
|
|
|
|
2020-04-15 12:29:34 +00:00
|
|
|
// initial sync to set up state and so our bot doesn't respond to old messages
|
|
|
|
client.sync(SyncSettings::default()).await.unwrap();
|
|
|
|
// 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_emitter(Box::new(CommandBot::new(client.clone())))
|
|
|
|
.await;
|
|
|
|
|
2020-04-15 11:52:29 +00:00
|
|
|
// this keeps state from the server streaming in to CommandBot via the EventEmitter trait
|
2020-04-14 22:14:14 +00:00
|
|
|
client.sync_forever(SyncSettings::new(), |_| async {}).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-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
|
|
|
}
|