use std::error::Error;
use std::io::Read;
use colored::Colorize;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};
use reqwest::blocking::Client;
use serde_json::{json, Value};
use crate::config_spec::{CfgField, CfgGroup};
use crate::{trace_var, verboseln};
pub const CONFIG: CfgGroup<'static> = CfgGroup {
name: "matrix",
description: "API Settings for matrix",
fields: &[
CfgField::Default {
key: "homeserver-url",
default: "https://matrix-client.matrix.org",
description: "Homeserver where the bot logs in.",
},
CfgField::Default {
key: "user-id",
default: "@bot_username:matrix.org",
description: "API Username associated with the bot account used for writing messages.",
},
CfgField::Password {
key: "access-token",
description: "Access Token / \"password\" used for authenticating as the bot.",
},
CfgField::Default {
key: "room-id-for-long-messages",
default: "!someLongRoomIdentifier:matrix.org",
description: "API Username associated with the bot account used for writing messages.",
},
CfgField::Default {
key: "room-id-for-short-messages",
default: "!someLongRoomIdentifier:matrix.org",
description: "API Username associated with the bot account used for writing messages.",
},
],
};
pub struct MatrixClient {
homeserver_url: String,
user_id: String,
access_token: String,
is_dry_run: bool,
client: Client,
txn_id: u64,
room_id_for_short_messages: String,
room_id_for_long_messages: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct LoginRequest {
user: String,
password: String,
#[serde(rename = "type")]
login_type: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct LoginResponse {
access_token: String,
}
impl std::fmt::Debug for MatrixClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("matrix")
.field("homeserver_url", &self.homeserver_url)
.field("user_id", &self.user_id)
.field("access_token", &"*****")
.field("is_dry_run", &self.is_dry_run)
.field("client", &self.client)
.finish()
}
}
impl MatrixClient {
pub fn new(
homeserver_url: &str, user_id: &str, access_token: &str, room_id_for_short_messages: &str,
room_id_for_long_messages: &str, is_dry_run: bool,
) -> Self {
Self {
homeserver_url: homeserver_url.to_string(),
user_id: user_id.to_string(),
access_token: access_token.to_string(),
is_dry_run,
client: Client::builder().cookie_store(true).build().unwrap(),
txn_id: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
room_id_for_long_messages: room_id_for_long_messages.to_string(),
room_id_for_short_messages: room_id_for_short_messages.to_string(),
}
}
fn request
");
html = html.replace("\n", "
");
html = html.replace("**", "");
html = html.replace("__", "");
Ok(html)
}
pub fn pandoc_convert_text_to_md(markdown: String) -> Result