Skip to content

Commit 6a35205

Browse files
committed
feat(slack): handle being tagged in slack channels
1 parent c264874 commit 6a35205

File tree

1 file changed

+67
-28
lines changed

1 file changed

+67
-28
lines changed

src/channels/slack.rs

Lines changed: 67 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,44 @@ async fn handle_app_mention_event(
651651
user_id, channel_id, thread_ts, text
652652
);
653653

654+
// Check if user is approved before proceeding
655+
let user_id_str = user_id.to_string();
656+
let mut store = PairingStore::load()?;
657+
658+
if !store.is_approved("slack", &user_id_str) {
659+
let settings = crate::config::Config::load()
660+
.map(|c: crate::config::Config| c.channel_settings("slack"))
661+
.unwrap_or_default();
662+
663+
if !settings.auto_approve {
664+
send_ephemeral_message(
665+
&client,
666+
&token,
667+
&channel_id,
668+
&user_id,
669+
"Hi! I don't recognize you yet. Please send me a direct message to get started.",
670+
)
671+
.await;
672+
return Ok(());
673+
}
674+
675+
// Auto-approve the user
676+
let (username, display_name) = get_user_info(&client, &token, &user_id).await;
677+
store.auto_approve("slack", &user_id_str, username, display_name)?;
678+
}
679+
680+
let onboarding_complete = crate::onboarding::is_complete_for_user("slack", &user_id_str)?;
681+
if !onboarding_complete {
682+
send_ephemeral_message(
683+
&client,
684+
&token,
685+
&channel_id,
686+
&user_id,
687+
"Tip: Send me a direct message to set up your profile for more personalized responses.",
688+
)
689+
.await;
690+
}
691+
654692
// Track thread for session management
655693
{
656694
let ts_str = thread_ts.to_string();
@@ -678,9 +716,6 @@ async fn handle_app_mention_event(
678716
}
679717
}
680718

681-
// Get user info for display name
682-
let (username, display_name) = get_user_info(&client, &token, &user_id).await;
683-
684719
// Create channel wrapper - always reply in thread
685720
let channel: Arc<dyn Channel> = Arc::new(SlackChannel::new(
686721
client.clone(),
@@ -690,37 +725,41 @@ async fn handle_app_mention_event(
690725
));
691726

692727
// Session key includes thread for continuity
693-
let user_id_str = user_id.to_string();
694728
let session_user_id = format!("{}:{}", user_id, thread_ts);
695729

696-
// Determine what action to take
697-
let mut store = PairingStore::load()?;
730+
let text_with_images = build_text_with_images(&text, &image_paths);
731+
let user_key = format!("{}:{}", channel.name(), session_user_id);
732+
let channel_clone = channel.clone();
733+
let session_user_id_clone = session_user_id.clone();
698734

699-
let action = determine_action(
700-
channel.name(),
701-
&user_id_str,
702-
&text,
703-
&image_paths,
704-
&mut store,
705-
username,
706-
display_name,
707-
)?;
735+
task_manager
736+
.process_message(user_key, text_with_images, move |messages| async move {
737+
execute_claude_query(channel_clone, &session_user_id_clone, messages).await;
738+
})
739+
.await;
708740

709-
// Execute the action
710-
if let Some(query_text) = execute_action(channel.as_ref(), &user_id_str, action).await? {
711-
let text_with_images = build_text_with_images(&query_text, &image_paths);
712-
let user_key = format!("{}:{}", channel.name(), session_user_id);
713-
let channel_clone = channel.clone();
714-
let session_user_id_clone = session_user_id.clone();
741+
Ok(())
742+
}
715743

716-
task_manager
717-
.process_message(user_key, text_with_images, move |messages| async move {
718-
execute_claude_query(channel_clone, &session_user_id_clone, messages).await;
719-
})
720-
.await;
721-
}
744+
/// Send an ephemeral message visible only to a specific user
745+
async fn send_ephemeral_message(
746+
client: &Arc<SlackHyperClient>,
747+
token: &SlackApiToken,
748+
channel_id: &SlackChannelId,
749+
user_id: &SlackUserId,
750+
message: &str,
751+
) {
752+
let session = client.open_session(token);
722753

723-
Ok(())
754+
let request = SlackApiChatPostEphemeralRequest::new(
755+
channel_id.clone(),
756+
user_id.clone(),
757+
SlackMessageContent::new().with_text(message.to_string()),
758+
);
759+
760+
if let Err(e) = session.chat_post_ephemeral(&request).await {
761+
warn!("Failed to send ephemeral message: {}", e);
762+
}
724763
}
725764

726765
/// Get user info from Slack API

0 commit comments

Comments
 (0)