summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
authorPaweł Dybiec <pawel@dybiec.info>2022-12-25 22:47:41 +0000
committerPaweł Dybiec <pawel@dybiec.info>2022-12-25 22:47:41 +0000
commit0087545a51a33182309fb60b0980e3ef45b63b6d (patch)
tree66137df60807056941ef4f8a4ac0be3bd8c0197f /src/main.rs
parentSupport for custom message handlers (diff)
Add reply to method with example use-case
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs56
1 files changed, 49 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs
index e2a9028..36dfbb8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,15 +1,57 @@
 mod mattermost;
 
+use anyhow::anyhow;
 use tokio::{self};
 use tracing::{debug, info};
 
-struct Vav{
-
-}
+struct Vav {}
 #[async_trait::async_trait]
-impl mattermost::Handler for Vav{
-    async fn handle(&self,message:serde_json::Value,client: & mattermost::Client) {
-        debug!("{message:?}");
+impl mattermost::Handler for Vav {
+    async fn handle(
+        &self,
+        value: serde_json::Value,
+        client: &mattermost::Client,
+    ) -> Result<(), anyhow::Error> {
+        debug!("{value:?}");
+        let object = value
+            .as_object()
+            .ok_or(anyhow!("Response from mattermost should be object"))?;
+        if object
+            .get("event")
+            .ok_or(anyhow!("missing event type"))?
+            .as_str()
+            .ok_or(anyhow!("event type should be a string"))?
+            != "posted"
+        {
+            return Ok(());
+        }
+        let post: serde_json::Value = serde_json::from_str(
+            object
+                .get("data")
+                .unwrap()
+                .get("post")
+                .ok_or(anyhow!("missing post field"))?
+                .as_str()
+                .ok_or(anyhow!("post should be string"))?,
+        )?;
+        debug!("Post field {post:?}");
+        let object = post
+            .as_object()
+            .ok_or(anyhow!("Post field should represent legit object"))?;
+        let message = object
+            .get("message")
+            .ok_or(anyhow!("missing message"))?
+            .as_str()
+            .ok_or(anyhow!("message should be string"))?;
+        if !message.starts_with('!') {
+            return Ok(());
+        }
+        let message = message.strip_prefix('!').unwrap();
+        match message{
+            "adres" => client.reply_to(&post, "Uniwersytet Wrocławski Instytut Informatyki; pl. Frederyka Joliot-Curie 15; 50-383 Wrocław".to_owned()).await?,
+            _ => return Err(anyhow!("Unrecognized command {message}")),
+        }
+        Ok(())
     }
 }
 
@@ -21,6 +63,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
     let auth = mattermost::AuthData { login, password };
     let mut client = mattermost::Client::new(auth, "https://mattermost.continuum.ii.uni.wroc.pl");
     client.update_bearer_token().await?;
-    client.handle_websocket_stream(Vav{}).await?;
+    client.handle_websocket_stream(Vav {}).await?;
     Ok(())
 }