Skip to content

Commit

Permalink
tc-cli: remove optional slack thread and check for env content
Browse files Browse the repository at this point in the history
  • Loading branch information
penumbra23 committed Feb 26, 2025
1 parent 8fcfe02 commit a9e8ad5
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions tc-cli/src/slack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct Slack {
client: SlackHyperClient,
token: SlackApiToken,
channel: SlackChannelId,
thread: Option<SlackTs>,
thread: SlackTs,
}

impl Slack {
Expand All @@ -79,14 +79,11 @@ impl Slack {
let token_value = SlackApiTokenValue::new(token);
let token = SlackApiToken::new(token_value);
let channel = std::env::var("SLACK_CHANNEL_ID")?;
anyhow::ensure!(!channel.is_empty());
let channel = SlackChannelId::new(channel);

// If thread isn't declared or empty, we should skip message sending
let thread = match std::env::var("SLACK_THREAD_TS") {
Ok(t) if t.is_empty() => None,
Ok(t) => Some(SlackTs::new(t)),
Err(e) => return Err(e.into()),
};
let thread = std::env::var("SLACK_THREAD_TS")?;
anyhow::ensure!(!thread.is_empty());
let thread = SlackTs::new(thread);
let client = SlackClient::new(SlackClientHyperConnector::new()?);
Ok(Self { client, token, channel, thread })
}
Expand All @@ -97,18 +94,13 @@ impl Slack {
msg: SlackMessageContent,
) -> Result<SlackTs> {
let session = self.client.open_session(&self.token);
// Skip sending if no thread is specified
if self.thread.is_none() {
return Ok(SlackTs::new(String::new()));
}

if let Some(id) = id {
let req = SlackApiChatUpdateRequest::new(self.channel.clone(), msg, id.clone());
session.chat_update(&req).await?;
Ok(id)
} else {
let req = SlackApiChatPostMessageRequest::new(self.channel.clone(), msg)
.with_thread_ts(self.thread.clone().unwrap());
.with_thread_ts(self.thread.clone());
let resp = session.chat_post_message(&req).await?;
Ok(resp.ts)
}
Expand All @@ -121,10 +113,6 @@ impl Slack {
content: Vec<u8>,
) -> Result<SlackFileId> {
let session = self.client.open_session(&self.token);
// Skip sending if no thread is specified
if self.thread.is_none() {
return Ok(SlackFileId::new(String::new()));
}
let req =
SlackApiFilesGetUploadUrlExternalRequest::new(format!("{name}.csv"), content.len());
let resp = session.get_upload_url_external(&req).await?;
Expand All @@ -142,7 +130,7 @@ impl Slack {
resp.file_id,
)])
.with_channel_id(self.channel.clone())
.with_thread_ts(self.thread.clone().unwrap());
.with_thread_ts(self.thread.clone());
let resp = session.files_complete_upload_external(&req).await?;
Ok(resp.files[0].id.clone())
}
Expand Down

0 comments on commit a9e8ad5

Please sign in to comment.