making the bot listen with mentions in DMs
This commit is contained in:
1 parent
650bcff83a
commit
3ba96528bb
2 files changed
+22
-3
No files matched your search
@@ -152,13 +152,12 @@ func (b *Bot) onEvent(ctx context.Context, ev slackevents.EventsAPIEvent) {
|
|||||||
if e.BotID != "" {
|
if e.BotID != "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
text := strings.TrimSpace(strings.ReplaceAll(e.Text, "<@"+b.botID+">", ""))
|
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, b.stripMention(e.Text), false, false})
|
||||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, false, false})
|
|
||||||
case *slackevents.MessageEvent:
|
case *slackevents.MessageEvent:
|
||||||
if e.BotID != "" || e.SubType != "" || e.User == "" || e.User == b.botID {
|
if e.BotID != "" || e.SubType != "" || e.User == "" || e.User == b.botID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
text := strings.TrimSpace(e.Text)
|
text := b.stripMention(e.Text)
|
||||||
switch {
|
switch {
|
||||||
case e.ChannelType == "im":
|
case e.ChannelType == "im":
|
||||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, true, false})
|
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) {
|
func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||||
if b.listenCommand(ctx, m) {
|
if b.listenCommand(ctx, m) {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -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) {
|
func TestCommandsInAssistantThreads(t *testing.T) {
|
||||||
log := &slackLog{text: map[string]string{}, under: map[string]string{}}
|
log := &slackLog{text: map[string]string{}, under: map[string]string{}}
|
||||||
srv := httptest.NewServer(log)
|
srv := httptest.NewServer(log)
|
||||||
|
|||||||
Reference in new issue
Block a user