summary refs log tree commit diff
path: root/src/main.rs
blob: b735100651c8d54d852a9beddf3765bb45dd06de (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
mod mattermost;
use anyhow::anyhow;
use tokio::{self};
use tracing::debug;

struct Vav {}
#[async_trait::async_trait]
impl mattermost::Handler for Vav {
    async fn handle(
        &self,
        update: mattermost::model::WebsocketUpdate,
        client: &mattermost::Client,
    ) -> Result<(), anyhow::Error> {
        debug!("as json: {update:?}");

        let posted = match update.update {
            mattermost::model::Update::Posted(posted) => posted,
            _ => return Ok(()),
        };
        let message = &posted.post.message;
        if !message.starts_with('!') {
            return Ok(());
        }
        let message = message.strip_prefix('!').unwrap();
        match message{
            "adres" => client.reply_to(posted.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(())
}