Connects, autojoins room, and responds to messages that aren't made by
itself. Signed-off-by: Louis Hollingworth <louis@hollingworth.nl>
This commit is contained in:
parent
4362601b5a
commit
c1e9f482b3
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
/target
|
||||
.DS_Store
|
||||
.env
|
||||
|
|
102
Cargo.lock
generated
102
Cargo.lock
generated
|
@ -992,20 +992,6 @@ dependencies = [
|
|||
"want",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyper-rustls"
|
||||
version = "0.24.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590"
|
||||
dependencies = [
|
||||
"futures-util",
|
||||
"http",
|
||||
"hyper",
|
||||
"rustls",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyper-tls"
|
||||
version = "0.5.0"
|
||||
|
@ -1872,7 +1858,6 @@ dependencies = [
|
|||
"http",
|
||||
"http-body",
|
||||
"hyper",
|
||||
"hyper-rustls",
|
||||
"hyper-tls",
|
||||
"ipnet",
|
||||
"js-sys",
|
||||
|
@ -1883,15 +1868,12 @@ dependencies = [
|
|||
"once_cell",
|
||||
"percent-encoding",
|
||||
"pin-project-lite",
|
||||
"rustls",
|
||||
"rustls-pemfile",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_urlencoded",
|
||||
"system-configuration",
|
||||
"tokio",
|
||||
"tokio-native-tls",
|
||||
"tokio-rustls",
|
||||
"tokio-util",
|
||||
"tower-service",
|
||||
"url",
|
||||
|
@ -1899,24 +1881,9 @@ dependencies = [
|
|||
"wasm-bindgen-futures",
|
||||
"wasm-streams",
|
||||
"web-sys",
|
||||
"webpki-roots",
|
||||
"winreg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ring"
|
||||
version = "0.17.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "684d5e6e18f669ccebf64a92236bb7db9a34f07be010e3627368182027180866"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"getrandom 0.2.11",
|
||||
"libc",
|
||||
"spin",
|
||||
"untrusted",
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ruma"
|
||||
version = "0.7.4"
|
||||
|
@ -2036,37 +2003,6 @@ dependencies = [
|
|||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls"
|
||||
version = "0.21.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "629648aced5775d558af50b2b4c7b02983a04b312126d45eeead26e7caa498b9"
|
||||
dependencies = [
|
||||
"log",
|
||||
"ring",
|
||||
"rustls-webpki",
|
||||
"sct",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls-pemfile"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c"
|
||||
dependencies = [
|
||||
"base64 0.21.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls-webpki"
|
||||
version = "0.101.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b6275d1ee7a1cd780b64aca7726599a1dbc893b1e64144529e55c3c2f745765"
|
||||
dependencies = [
|
||||
"ring",
|
||||
"untrusted",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.15"
|
||||
|
@ -2088,16 +2024,6 @@ version = "1.2.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "sct"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da046153aa2352493d6cb7da4b6e5c0c057d8a1d0a9aa8560baffdd945acd414"
|
||||
dependencies = [
|
||||
"ring",
|
||||
"untrusted",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "security-framework"
|
||||
version = "2.9.2"
|
||||
|
@ -2272,12 +2198,6 @@ dependencies = [
|
|||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.9.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
|
||||
|
||||
[[package]]
|
||||
name = "spki"
|
||||
version = "0.5.4"
|
||||
|
@ -2440,16 +2360,6 @@ dependencies = [
|
|||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-rustls"
|
||||
version = "0.24.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081"
|
||||
dependencies = [
|
||||
"rustls",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-stream"
|
||||
version = "0.1.14"
|
||||
|
@ -2616,12 +2526,6 @@ dependencies = [
|
|||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.5.0"
|
||||
|
@ -2822,12 +2726,6 @@ dependencies = [
|
|||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "webpki-roots"
|
||||
version = "0.25.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1778a42e8b3b90bff8d0f5032bf22250792889a5cdc752aa0020c84abe3aaf10"
|
||||
|
||||
[[package]]
|
||||
name = "wildmatch"
|
||||
version = "2.1.1"
|
||||
|
|
|
@ -9,7 +9,7 @@ edition = "2021"
|
|||
anyhow = "1.0.75"
|
||||
deepl = "0.4.4"
|
||||
dotenvy = "0.15.7"
|
||||
matrix-sdk = { version = "0.6.2", features = ["anyhow", "markdown", "rustls-tls"] }
|
||||
matrix-sdk = { version = "0.6.2", features = ["anyhow", "markdown"] }
|
||||
tokio = { version = "1.34.0", features = ["macros", "rt-multi-thread", "full"] }
|
||||
tracing = "0.1.40"
|
||||
tracing-subscriber = "0.3.18"
|
||||
|
|
100
src/main.rs
100
src/main.rs
|
@ -1,4 +1,98 @@
|
|||
fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
println!("Hello, world!");
|
||||
use matrix_sdk::ruma::events::room::message;
|
||||
|
||||
async fn on_room_message(event: message::OriginalSyncRoomMessageEvent, room: matrix_sdk::room::Room, client: matrix_sdk::Client) {
|
||||
let cl = client.clone();
|
||||
let this_uid = cl.user_id().unwrap();
|
||||
if event.clone().sender.matrix_uri(false) == this_uid.matrix_uri(false) {
|
||||
return;
|
||||
}
|
||||
let message::MessageType::Text(text_content) = event.clone().content.msgtype else {
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
let r = client.get_joined_room(room.room_id()).unwrap();
|
||||
|
||||
if text_content.body.contains("!diru") {
|
||||
if text_content.body.contains("dpl") {
|
||||
deepl_translation(event, r, &text_content.body).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn autojoin_room(room_member: matrix_sdk::ruma::events::room::member::StrippedRoomMemberEvent, client: matrix_sdk::Client, room: matrix_sdk::room::Room) {
|
||||
if room_member.state_key != client.user_id().unwrap() {
|
||||
return;
|
||||
}
|
||||
|
||||
tokio::spawn(async move {
|
||||
let rid = room.room_id();
|
||||
tracing::info!("Autojoining {}", rid);
|
||||
let mut delay = 2;
|
||||
let r = client.get_invited_room(rid).unwrap();
|
||||
while let Err(err) = r.accept_invitation().await {
|
||||
eprintln!("Failed to join room {} ({err:?}), retrying in {delay}s", room.room_id());
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(delay)).await;
|
||||
delay *= 2;
|
||||
if delay > 3600 {
|
||||
eprintln!("Can't join room {} ({err:?})", room.room_id());
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async fn deepl_translation(event: message::OriginalSyncRoomMessageEvent, room: matrix_sdk::room::Joined, content: &str) {
|
||||
let txn_id = matrix_sdk::ruma::TransactionId::new();
|
||||
let msg = message::RoomMessageEventContent::text_plain(format!("{}", content))
|
||||
.make_reply_to(&event.clone().into_full_event(room.room_id().into()));
|
||||
room.send(msg, Some(&txn_id)).await.unwrap();
|
||||
}
|
||||
|
||||
async fn connect_to_matrix(hs: String, username: String, passwd:String) -> anyhow::Result<()> {
|
||||
let client = matrix_sdk::Client::builder().homeserver_url(hs).build().await.unwrap();
|
||||
let device_id = std::env::var("DEVICE_ID");
|
||||
|
||||
match device_id {
|
||||
Ok(id) => {
|
||||
client
|
||||
.login_username(&username, &passwd)
|
||||
.device_id(&id)
|
||||
.send()
|
||||
.await?;
|
||||
},
|
||||
Err(_) => {
|
||||
client.login_username(&username, &passwd)
|
||||
.initial_device_display_name("Diru Development")
|
||||
.send()
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
println!("Logged in as {username}");
|
||||
let response = client.sync_once(matrix_sdk::config::SyncSettings::default()).await?;
|
||||
client.add_event_handler(on_room_message);
|
||||
client.add_event_handler(autojoin_room);
|
||||
let stg = matrix_sdk::config::SyncSettings::default().token(response.next_batch);
|
||||
client.sync(stg).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
dotenvy::dotenv().ok();
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let (homeserver_url, username, passwd) = match (std::env::var("HOMESERVER_URL"), std::env::var("MATRIX_BOT_NAME"), std::env::var("MATRIX_BOT_PASSWD")) {
|
||||
(Ok(a), Ok(b), Ok(c)) => (a, b, c),
|
||||
_ => {
|
||||
eprintln!("Please ensure the HOMESERVER_URL, MATRIX_BOT_NAME, and MATRIX_BOT_PASSWD environment variables are set.");
|
||||
std::process::exit(1)
|
||||
}
|
||||
};
|
||||
|
||||
connect_to_matrix(homeserver_url, username, passwd).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue