Redo mattermost support

This commit is contained in:
Maxime Augier 2024-08-18 14:35:01 +02:00
parent 7961133964
commit b6135dd717

View File

@ -4,15 +4,23 @@ use anyhow::Result;
pub struct Context {
pub base: String,
pub token: String,
pub auth_header: String,
}
pub struct Channel {
channel_id: String,
}
impl Context {
pub fn new(base: String, token: &str) -> Result<Self> {
Ok(Self {
base,
auth_header: format!("Bearer {token}"),
})
}
fn path(&self, rel: &str) -> String {
format!("{}/api/v4/{}", self.base, rel)
}
@ -38,6 +46,18 @@ impl Context {
self.set_custom_status(text, emoji)
}
pub fn send_to_channel(&self, channel: &Channel, )
pub fn channel(&self, id: &str) -> Channel {
Channel { channel_id: id.to_owned() }
}
pub fn send_to_channel(&self, channel: &Channel, msg: &str) -> Result<()> {
let path = self.path("posts");
ureq::post(&path)
.set("Authorization", &self.auth_header)
.send_json(json!(
{ "channel_id": channel.channel_id, "message": msg }
))?;
Ok(())
}
}