use crate::client::SlackClient;
pub enum CreateMessage {
#[allow(dead_code)]
Attachments(serde_json::Value),
Blocks(serde_json::Value),
Text(String),
}
impl CreateMessage {
fn key_value(self) -> (&'static str, serde_json::Value) {
match self {
CreateMessage::Attachments(value) => ("attachments", value),
CreateMessage::Blocks(value) => ("blocks", value),
CreateMessage::Text(value) => ("text", serde_json::Value::String(value)),
}
}
pub fn to_request(self) -> serde_json::Value {
let (message_type, value) = self.key_value();
serde_json::json!({
message_type: value,
})
}
pub async fn send_to_channel(
self,
client: &SlackClient,
channel: String,
) -> Result<reqwest::Response, reqwest::Error> {
let mut request = self.to_request();
request["channel"] = serde_json::Value::String(channel);
client.send_message(request.to_string(), None).await
}
pub async fn send_response_url(
self,
client: &SlackClient,
response_url: &str,
) -> Result<reqwest::Response, reqwest::Error> {
let request = self.to_request();
client
.send_message(request.to_string(), Some(response_url))
.await
}
}