use super::types::*;
use crate::config::SlackConfig;
use async_stream::stream;
use futures::Stream;
use plexus_core::plexus;
use plexus_core::serde_helpers;
#[derive(Clone)]
pub struct Slack {
bot_token: String,
client: reqwest::Client,
}
impl Slack {
pub async fn new(config: SlackConfig) -> Result<Self, String> {
Ok(Self {
bot_token: config.bot_token,
client: reqwest::Client::new(),
})
}
}
#[plexus_macros::hub_methods(
namespace = "slack",
version = "1.0.0",
description = "Send messages and interact with Slack workspaces"
)]
impl Slack {
#[plexus_macros::hub_method(
description = "Send a message to a channel",
params(
channel = "Channel ID or name",
text = "Message text",
thread_ts = "Thread timestamp for replies (optional)",
attachments = "Message attachments (optional)"
)
)]
async fn send_message(
&self,
channel: String,
text: String,
thread_ts: Option<String>,
attachments: Option<Vec<serde_json::Value>>,
) -> impl Stream<Item = SendMessageEvent> + Send + 'static {
let _params = SendMessageParams {
channel: channel.clone(),
text,
thread_ts,
attachments,
};
stream! {
yield SendMessageEvent::Sent {
ts: chrono::Utc::now().timestamp().to_string(),
channel,
};
}
}
#[plexus_macros::hub_method(
description = "Create a new channel",
params(
name = "Channel name",
is_private = "Whether the channel is private (optional, default: false)"
)
)]
async fn create_channel(
&self,
name: String,
is_private: Option<bool>,
) -> impl Stream<Item = ChannelEvent> + Send + 'static {
let _params = CreateChannelParams {
name: name.clone(),
is_private,
};
stream! {
yield ChannelEvent::Created {
channel_id: format!("C{}", uuid::Uuid::new_v4().to_string().replace('-', "")),
name,
};
}
}
#[plexus_macros::hub_method(
streaming,
description = "Listen for Slack events (messages, reactions, etc.)"
)]
async fn listen_events(&self) -> impl Stream<Item = SlackEvent> + Send + 'static {
stream! {
yield SlackEvent::Error {
message: "Not implemented yet".to_string(),
};
}
}
}