hello world over matrix
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
Jake Hillion 2023-10-22 01:01:27 +01:00
parent e00a8d5083
commit 2d478c2800
4 changed files with 2957 additions and 2 deletions

2845
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,3 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { version = "4.4.6", features = ["derive"] }
matrix-sdk = "0.6.2"
serde = { version = "1.0.189", features = ["derive"] }
serde_yaml = "0.9.25"
thiserror = "1.0.50"
tokio = { version = "1.33.0", features = ["full"] }

31
src/config.rs Normal file
View File

@ -0,0 +1,31 @@
use serde::Deserialize;
use std::fs::File;
use std::path::Path;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("failed to read/write config file")]
Filesystem(#[from] std::io::Error),
#[error("failed to parse yaml")]
Parse(#[from] serde_yaml::Error),
}
#[derive(Deserialize)]
pub struct Matrix {
pub user_id: String,
pub password: String,
}
#[derive(Deserialize)]
pub struct Config {
pub matrix: Matrix,
}
impl Config {
pub fn load(p: &Path) -> Result<Config, Error> {
let f = File::open(p)?;
Ok(serde_yaml::from_reader(f)?)
}
}

View File

@ -1,3 +1,76 @@
fn main() {
println!("Hello, world!");
mod config;
use clap::Parser;
use config::Config;
use matrix_sdk::ruma::api::client::room::{
create_room::v3::Request as CreateRoomRequest, Visibility,
};
use matrix_sdk::{
config::SyncSettings,
ruma::{events::room::message::SyncRoomMessageEvent, user_id},
Client,
};
use matrix_sdk::ruma::events::room::message::RoomMessageEventContent;
use matrix_sdk::ruma::events::room::message::MessageType;
use matrix_sdk::ruma::events::room::message::TextMessageEventContent;
use matrix_sdk::HttpError;
use std::path::PathBuf;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("configuration error")]
Configuration(#[from] config::Error),
#[error("failed to setup client")]
Client(#[from] matrix_sdk::ClientBuildError),
#[error("matrix")]
Matrix(#[from] matrix_sdk::Error),
#[error("matrix (http)")]
Http(#[from] HttpError),
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
// Configuration file path
#[arg(short, long)]
config: PathBuf,
}
#[tokio::main]
async fn main() -> Result<(), Error> {
let args = Args::parse();
let cfg = Config::load(&args.config)?;
let jorah = user_id!("@jorah.cx:hillion.co.uk");
let jake = user_id!("@jake:hillion.co.uk");
let client = Client::builder()
.homeserver_url("https://matrix.hillion.co.uk")
.build()
.await?;
client
.login_username(jorah, &cfg.matrix.password)
.send()
.await?;
let mut room_req = CreateRoomRequest::new();
let invitees = vec![jake.clone().into()];
room_req.invite = invitees.as_slice();
room_req.is_direct = true;
room_req.name = Some("Testing 123");
room_req.topic = Some("Some serious testing going on here.");
room_req.visibility = Visibility::Private;
let room_id = client.create_room(room_req).await?.room_id;
let room = client.get_joined_room(&room_id).unwrap();
let msg = MessageType::Text(TextMessageEventContent::plain("Hello world!"));
let content = RoomMessageEventContent::new(msg);
room.send(content, None).await?;
Ok(())
}