Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tc-cli: skip messages if thread isn't specified #1600

Merged
merged 4 commits into from
Feb 26, 2025
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 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: SlackTs,
thread: Option<SlackTs>,
}

impl Slack {
Expand All @@ -80,8 +80,13 @@ impl Slack {
let token = SlackApiToken::new(token_value);
let channel = std::env::var("SLACK_CHANNEL_ID")?;
let channel = SlackChannelId::new(channel);
let thread = std::env::var("SLACK_THREAD_TS")?;
let thread = SlackTs::new(thread);

// 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 client = SlackClient::new(SlackClientHyperConnector::new()?);
Ok(Self { client, token, channel, thread })
}
Expand All @@ -92,13 +97,18 @@ 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());
.with_thread_ts(self.thread.clone().unwrap());
let resp = session.chat_post_message(&req).await?;
Ok(resp.ts)
}
Expand All @@ -111,7 +121,10 @@ 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 @@ -129,7 +142,7 @@ impl Slack {
resp.file_id,
)])
.with_channel_id(self.channel.clone())
.with_thread_ts(self.thread.clone());
.with_thread_ts(self.thread.clone().unwrap());
let resp = session.files_complete_upload_external(&req).await?;
Ok(resp.files[0].id.clone())
}
Expand Down
Loading