summary refs log tree commit diff
path: root/src/main.rs
blob: 36dfbb866bbe25942e4cacf672dd3e3c65286453 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
mod mattermost;

use anyhow::anyhow;
use tokio::{self};
use tracing::{debug, info};

struct Vav {}
#[async_trait::async_trait]
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(())
    }
}

#[tokio::main(worker_threads = 2)]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt::init();
    let login = std::env::var("USER_MAIL")?;
    let password = std::env::var("USER_PASSWORD")?;
    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?;
    Ok(())
}