funky little overengineered stupid roomnamebot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

93 lines
2.5 KiB

extern crate percent_encoding;
extern crate reqwest;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate toml;
use std::fs::File;
use std::io::Read;
use reqwest::header::{Authorization, Bearer};
use std::collections::HashMap;
use percent_encoding::{utf8_percent_encode, SIMPLE_ENCODE_SET};
#[derive(Serialize, Deserialize)]
pub struct Config {
rooms: Vec<Room>,
homeserver: String,
token: String,
}
#[derive(Serialize, Deserialize)]
pub struct Room {
id: String,
name_endpoint: String,
}
fn main() {
//Get config
let mut file = match File::open("roomnamebot.toml") {
Ok(file) => file,
Err(_) => {
println!("Could not open roomnamebot.toml");
return;
}
};
let mut contents = String::new();
match file.read_to_string(&mut contents) {
Ok(_) => {}
Err(_) => {
println!("Couldn't read roomnamebot.toml to string");
return;
}
};
let config: Config = match toml::from_str(&contents) {
Ok(config) => config,
Err(_) => {
println!("Couldn't deserialize roomnamebot.toml");
return;
}
};
let http_client = reqwest::Client::new();
//Iterate over rooms
for room in config.rooms {
//Get new room name
let name = match http_client.get(&room.name_endpoint).send() {
Ok(mut response) => match response.text() {
Ok(name) => name,
Err(_) => {
println!("Something went wrong getting the room name for {}", room.id);
continue;
}
},
Err(_) => {
println!("Something went wrong getting the room name for {}", room.id);
continue;
}
};
//create map for event content
let mut map = HashMap::new();
map.insert("name", name);
//And now send it
match http_client
.put(&format!(
"{homeserver}/_matrix/client/r0/rooms/{room_id}/state/m.room.name",
homeserver = config.homeserver,
room_id = utf8_percent_encode(&room.id, SIMPLE_ENCODE_SET)
))
.header(Authorization(Bearer {
token: config.token.clone(),
}))
.json(&map)
.send()
{
Ok(_) => {}
Err(_) => {
println!("Something went wrong setting the room name for {}", room.id);
continue;
}
}
}
}