zeph-channels 0.22.0

Multi-channel I/O adapters (CLI, Telegram, Discord, Slack) for Zeph
Documentation
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Slack Web API client for message operations.

use std::time::Duration;

use serde::{Deserialize, Serialize};
use serde_json::Value;

const SLACK_API: &str = "https://slack.com/api";
const MAX_AUDIO_BYTES: usize = 25 * 1024 * 1024;

pub struct SlackApi {
    client: reqwest::Client,
    token: String,
}

impl std::fmt::Debug for SlackApi {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SlackApi")
            .field("token", &"[REDACTED]")
            .finish_non_exhaustive()
    }
}

#[derive(Deserialize)]
struct SlackResponse {
    ok: bool,
    #[serde(default)]
    error: Option<String>,
    #[serde(default)]
    ts: Option<String>,
}

#[derive(Serialize)]
struct PostMessage<'a> {
    channel: &'a str,
    text: &'a str,
}

#[derive(Serialize)]
struct UpdateMessage<'a> {
    channel: &'a str,
    ts: &'a str,
    text: &'a str,
}

impl SlackApi {
    #[must_use]
    pub fn new(token: String) -> Self {
        Self {
            client: zeph_core::http::default_client(),
            token,
        }
    }

    /// Call auth.test to retrieve the bot's own user ID.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP request or Slack API fails.
    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(name = "channels.slack.auth_test", skip_all)
    )]
    pub async fn auth_test(&self) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
        let resp: Value = tokio::time::timeout(
            Duration::from_secs(15),
            self.client
                .post(format!("{SLACK_API}/auth.test"))
                .bearer_auth(&self.token)
                .send(),
        )
        .await
        .map_err(|_| "slack auth.test timed out")?
        .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?
        .json()
        .await
        .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;

        if resp.get("ok").and_then(Value::as_bool) != Some(true) {
            let err = resp
                .get("error")
                .and_then(|v| v.as_str())
                .unwrap_or("unknown");
            return Err(format!("slack auth.test: {err}").into());
        }
        resp.get("user_id")
            .and_then(|v| v.as_str())
            .map(String::from)
            .ok_or_else(|| "no user_id in auth.test response".into())
    }

    /// Post a new message, returning the message timestamp (ts).
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP request or Slack API fails.
    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(name = "channels.slack.post_message", skip_all)
    )]
    pub async fn post_message(
        &self,
        channel: &str,
        text: &str,
    ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
        let resp: SlackResponse = tokio::time::timeout(
            Duration::from_secs(15),
            self.client
                .post(format!("{SLACK_API}/chat.postMessage"))
                .bearer_auth(&self.token)
                .json(&PostMessage { channel, text })
                .send(),
        )
        .await
        .map_err(|_| "slack chat.postMessage timed out")?
        .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?
        .json()
        .await
        .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;

        if !resp.ok {
            return Err(
                format!("slack chat.postMessage: {}", resp.error.unwrap_or_default()).into(),
            );
        }
        resp.ts.ok_or_else(|| "no ts in response".into())
    }

    /// Update an existing message.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP request or Slack API fails.
    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(name = "channels.slack.update_message", skip_all)
    )]
    pub async fn update_message(
        &self,
        channel: &str,
        ts: &str,
        text: &str,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let resp: SlackResponse = tokio::time::timeout(
            Duration::from_secs(15),
            self.client
                .post(format!("{SLACK_API}/chat.update"))
                .bearer_auth(&self.token)
                .json(&UpdateMessage { channel, ts, text })
                .send(),
        )
        .await
        .map_err(|_| "slack chat.update timed out")?
        .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?
        .json()
        .await
        .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;

        if !resp.ok {
            return Err(format!("slack chat.update: {}", resp.error.unwrap_or_default()).into());
        }
        Ok(())
    }

    /// Download a file from Slack using the bot token for authorization.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP request fails or the response status is not success.
    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(name = "channels.slack.download_file", skip_all)
    )]
    pub async fn download_file(
        &self,
        url: &str,
    ) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
        let host = reqwest::Url::parse(url)
            .ok()
            .and_then(|u| u.host_str().map(String::from));
        if !host.is_some_and(|h| h.ends_with(".slack.com")) {
            return Err(format!("refusing to send token to non-slack host: {url}").into());
        }

        let resp = tokio::time::timeout(
            Duration::from_secs(15),
            self.client.get(url).bearer_auth(&self.token).send(),
        )
        .await
        .map_err(|_| "slack file download timed out")?
        .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
        if !resp.status().is_success() {
            return Err(format!("slack file download failed: {}", resp.status()).into());
        }
        let bytes = tokio::time::timeout(Duration::from_secs(15), resp.bytes())
            .await
            .map_err(|_| "slack file download body timed out")?
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
        if bytes.len() > MAX_AUDIO_BYTES {
            return Err(format!(
                "slack file too large: {} bytes (max {MAX_AUDIO_BYTES})",
                bytes.len()
            )
            .into());
        }
        Ok(bytes.to_vec())
    }
}