diff options
author | Paweł Dybiec <pawel@dybiec.info> | 2022-12-25 22:47:41 +0000 |
---|---|---|
committer | Paweł Dybiec <pawel@dybiec.info> | 2022-12-25 22:47:41 +0000 |
commit | 0087545a51a33182309fb60b0980e3ef45b63b6d (patch) | |
tree | 66137df60807056941ef4f8a4ac0be3bd8c0197f /src/mattermost.rs | |
parent | Support for custom message handlers (diff) |
Add reply to method with example use-case
Diffstat (limited to 'src/mattermost.rs')
-rw-r--r-- | src/mattermost.rs | 72 |
1 files changed, 67 insertions, 5 deletions
diff --git a/src/mattermost.rs b/src/mattermost.rs index 9b811d1..c8759c3 100644 --- a/src/mattermost.rs +++ b/src/mattermost.rs @@ -16,7 +16,11 @@ pub struct Client { #[async_trait::async_trait] pub trait Handler { - async fn handle(&self, message:serde_json::Value,client: &Client); + async fn handle( + &self, + message: serde_json::Value, + client: &Client, + ) -> Result<(), anyhow::Error>; } impl Client { @@ -48,7 +52,10 @@ impl Client { debug!("Successfully updated bearer token"); Ok(()) } - pub(crate) async fn handle_websocket_stream<T:Handler>(&mut self, handler: T) -> Result<(), anyhow::Error> { + pub(crate) async fn handle_websocket_stream<T: Handler>( + &mut self, + handler: T, + ) -> Result<(), anyhow::Error> { let url = format!("{}/api/v4/websocket", self.url.replacen("http", "ws", 1)); let (mut ws_stream, _) = connect_async(url).await?; let token = self @@ -67,13 +74,18 @@ impl Client { Message::Text(text) => { let json: Result<serde_json::Value, _> = serde_json::from_str(&text); match json { - Ok(json) => {debug!("Got json {json}");handler.handle(json,&self).await;}, - Err(err) => warn!("Error while deserializing {err}"), + Ok(json) => { + // debug!("Got json: {json}"); + if let Err(err) = handler.handle(json, &self).await { + warn!("Handler returned error: {err}"); + }; + } + Err(err) => warn!("Error while deserializing: {err}"), } } Message::Close(_) => break, _ => { - debug!("Message {message:?}"); + debug!("Message: {message:?}"); } }, Err(err) => warn!("Error {err} while reading message"), @@ -81,4 +93,54 @@ impl Client { } Ok(()) } + pub(crate) async fn reply_to( + &self, + post: &serde_json::Value, + reply: String, + ) -> Result<(), anyhow::Error> { + let token = self + .bearer_token + .clone() + .ok_or(anyhow!("Missing bearer token"))?; + let object = post + .as_object() + .ok_or(anyhow!("Response from mattermost should be object"))?; + let id = object + .get("id") + .ok_or(anyhow!("missing id"))? + .as_str() + .ok_or(anyhow!("id should be a string"))?; + let root_id = object + .get("root_id") + .ok_or(anyhow!("missing root_id"))? + .as_str() + .ok_or(anyhow!("root_id should be a string"))?; + let post_id = if root_id == "" { id } else { root_id }; + let channel_id = object + .get("channel_id") + .ok_or(anyhow!("missing channel_id"))? + .as_str() + .ok_or(anyhow!("channel_id should be a string"))?; + + debug!("Sending response"); + let response = self + .client + .post(format!("{}/api/v4/posts", self.url)) + .json(&json!({ + "channel_id":channel_id, + "root_id":post_id, + "message":&reply, + })) + .header("Authorization", format!("Bearer {token}")) + .send() + .await?; + let status = response.status(); + let resp = response.text().await; + if !status.is_success() { + warn!("{:?}", resp); + return Err(anyhow!("Error {}", status)); + } + debug!("Sent response"); + Ok(()) + } } |