making the bot listen with mentions in DMs

This commit is contained in:
ari committed 2026-11-01 14:24:32 -05:00
1 parent 650bcff83a
commit 3ba96528bb
2 files changed
+22 -3

No files matched your search

+6 -3
View File
@@ -152,13 +152,12 @@ func (b *Bot) onEvent(ctx context.Context, ev slackevents.EventsAPIEvent) {
if e.BotID != "" {
return
}
text := strings.TrimSpace(strings.ReplaceAll(e.Text, "<@"+b.botID+">", ""))
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, false, false})
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, b.stripMention(e.Text), false, false})
case *slackevents.MessageEvent:
if e.BotID != "" || e.SubType != "" || e.User == "" || e.User == b.botID {
return
}
text := strings.TrimSpace(e.Text)
text := b.stripMention(e.Text)
switch {
case e.ChannelType == "im":
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, true, false})
@@ -174,6 +173,10 @@ func (b *Bot) onEvent(ctx context.Context, ev slackevents.EventsAPIEvent) {
}
}
func (b *Bot) stripMention(text string) string {
return strings.TrimSpace(strings.ReplaceAll(text, "<@"+b.botID+">", ""))
}
func (b *Bot) onMessage(ctx context.Context, m incoming) {
if b.listenCommand(ctx, m) {
return
+16
View File
@@ -311,6 +311,22 @@ func TestStopReactionCancelsPrompt(t *testing.T) {
}
}
func TestMentionedCommandInDM(t *testing.T) {
log := &slackLog{text: map[string]string{}, under: map[string]string{}}
srv := httptest.NewServer(log)
defer srv.Close()
store, _ := state.Open(filepath.Join(t.TempDir(), "state.json"))
b := &Bot{store: store, botID: "UBOT", api: slack.New("xoxb", slack.OptionAPIURL(srv.URL+"/"))}
b.onEvent(context.Background(), slackevents.EventsAPIEvent{InnerEvent: slackevents.EventsAPIInnerEvent{
Data: &slackevents.MessageEvent{User: "U1", Channel: "D1", ChannelType: "im", TimeStamp: "3.0", Text: "<@UBOT> help"},
}})
log.mu.Lock()
defer log.mu.Unlock()
if len(log.order) != 1 || !strings.Contains(log.text[log.order[0]], "Commands") {
t.Fatalf("@mentioning the bot in a DM should still run the command: %v %v", log.order, log.text)
}
}
func TestCommandsInAssistantThreads(t *testing.T) {
log := &slackLog{text: map[string]string{}, under: map[string]string{}}
srv := httptest.NewServer(log)