making use of the extended perms and added future work
This commit is contained in:
1 parent
296e465f48
commit
22fd8fbce8
19 files changed
+617
-40
No files matched your search
@@ -0,0 +1,71 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/slack-go/slack"
|
||||
"github.com/slack-go/slack/slackevents"
|
||||
)
|
||||
|
||||
// where describes a conversation for the agent, or "" for a DM.
|
||||
func (b *Bot) where(ctx context.Context, channel string) string {
|
||||
info, ok := b.channels.Load(channel)
|
||||
if !ok {
|
||||
c, err := b.api.GetConversationInfoContext(ctx, &slack.GetConversationInfoInput{ChannelID: channel})
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
info, _ = b.channels.LoadOrStore(channel, c)
|
||||
}
|
||||
c := info.(*slack.Channel)
|
||||
switch {
|
||||
case c.IsIM:
|
||||
return ""
|
||||
case c.IsMpIM:
|
||||
return "a Slack group DM"
|
||||
}
|
||||
return "the Slack channel #" + c.Name
|
||||
}
|
||||
|
||||
func (b *Bot) assistantStarted(ctx context.Context, t slackevents.AssistantThread) {
|
||||
b.setViewing(t)
|
||||
params := slack.AssistantThreadsSetSuggestedPromptsParameters{ChannelID: t.ChannelID, ThreadTS: t.ThreadTimeStamp}
|
||||
params.AddPrompt("Container status", "status")
|
||||
params.AddPrompt("Past sessions", "sessions")
|
||||
params.AddPrompt("What can you do?", "help")
|
||||
_ = b.api.SetAssistantThreadsSuggestedPromptsContext(ctx, params)
|
||||
}
|
||||
|
||||
func (b *Bot) setViewing(t slackevents.AssistantThread) {
|
||||
if t.Context.ChannelID != "" {
|
||||
b.viewing.Store(t.ChannelID+"/"+t.ThreadTimeStamp, t.Context.ChannelID)
|
||||
}
|
||||
}
|
||||
|
||||
// viewingNote tells the agent which channel the user has open next to the
|
||||
// assistant pane, once per change.
|
||||
func (b *Bot) viewingNote(ctx context.Context, channel, threadTS string) string {
|
||||
key := channel + "/" + threadTS
|
||||
v, ok := b.viewing.Load(key)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
if told, _ := b.told.Swap(key, v); told == v {
|
||||
return ""
|
||||
}
|
||||
if w := b.where(ctx, v.(string)); w != "" {
|
||||
return "(The user is looking at " + w + ".)\n"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Assistant thread calls fail harmlessly in ordinary DM threads.
|
||||
func (b *Bot) setAssistantStatus(ctx context.Context, channel, threadTS, status string) {
|
||||
_ = b.api.SetAssistantThreadsStatusContext(ctx, slack.AssistantThreadsSetStatusParameters{
|
||||
ChannelID: channel, ThreadTS: threadTS, Status: status})
|
||||
}
|
||||
|
||||
func (b *Bot) setAssistantTitle(ctx context.Context, channel, threadTS, text string) {
|
||||
_ = b.api.SetAssistantThreadsTitleContext(ctx, slack.AssistantThreadsSetTitleParameters{
|
||||
ChannelID: channel, ThreadTS: threadTS, Title: text})
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -34,6 +35,9 @@ const helpText = "I pass your messages to an agent running in its own container.
|
||||
"• In a DM, the container is yours alone. In a channel, it belongs to the channel " +
|
||||
"and anyone here can use it by @mentioning me.\n" +
|
||||
"• Every new message starts a new session. Reply in its thread to keep going.\n" +
|
||||
"• In a channel thread I only read replies that @mention me. `/listen all-messages` in the thread makes me read them all, " +
|
||||
"`/listen channel all-messages` sets the channel's default.\n" +
|
||||
"• React with :x: to stop a reply part-way.\n" +
|
||||
"*Commands* (send as a new message, not in a thread):\n" +
|
||||
"• `status`: state, size and idle limit, with buttons\n" +
|
||||
"• `sessions`: past sessions, with links\n" +
|
||||
@@ -67,6 +71,11 @@ type Bot struct {
|
||||
busy map[string]int
|
||||
names sync.Map
|
||||
|
||||
channels sync.Map // channel ID → *slack.Channel
|
||||
running sync.Map // "channel/ts" of a prompt's message or reply → its cancel func
|
||||
viewing sync.Map // assistant "channel/thread" → channel the user has open
|
||||
told sync.Map // assistant "channel/thread" → last channel the agent was told about
|
||||
|
||||
waitMu sync.Mutex
|
||||
waiting map[string]map[string]bool
|
||||
}
|
||||
@@ -74,6 +83,7 @@ type Bot struct {
|
||||
type incoming struct {
|
||||
user, channel, ts, threadTS, text string
|
||||
dm bool
|
||||
heard bool
|
||||
}
|
||||
|
||||
func (m incoming) owner() (string, model.OwnerKind) {
|
||||
@@ -143,20 +153,37 @@ func (b *Bot) onEvent(ctx context.Context, ev slackevents.EventsAPIEvent) {
|
||||
return
|
||||
}
|
||||
text := strings.TrimSpace(strings.ReplaceAll(e.Text, "<@"+b.botID+">", ""))
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, false})
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, false, false})
|
||||
case *slackevents.MessageEvent:
|
||||
if e.ChannelType != "im" || e.BotID != "" || e.SubType != "" || e.User == "" || e.User == b.botID {
|
||||
if e.BotID != "" || e.SubType != "" || e.User == "" || e.User == b.botID {
|
||||
return
|
||||
}
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, strings.TrimSpace(e.Text), true})
|
||||
text := strings.TrimSpace(e.Text)
|
||||
switch {
|
||||
case e.ChannelType == "im":
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, true, false})
|
||||
case b.listening(e.Channel, e.ThreadTimeStamp, e.Text):
|
||||
b.onMessage(ctx, incoming{e.User, e.Channel, e.TimeStamp, e.ThreadTimeStamp, text, false, true})
|
||||
}
|
||||
case *slackevents.ReactionAddedEvent:
|
||||
b.onReaction(e)
|
||||
case *slackevents.AssistantThreadStartedEvent:
|
||||
b.assistantStarted(ctx, e.AssistantThread)
|
||||
case *slackevents.AssistantThreadContextChangedEvent:
|
||||
b.setViewing(e.AssistantThread)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
if m.threadTS == "" && b.command(ctx, m) {
|
||||
if b.listenCommand(ctx, m) {
|
||||
return
|
||||
}
|
||||
if m.threadTS != "" && b.moveCommand(ctx, m, m.threadTS) {
|
||||
// Every assistant-pane chat is a DM thread, so its first message counts as new.
|
||||
fresh := m.threadTS == "" || m.dm && b.store.Session(m.channel, m.threadTS) == nil
|
||||
if fresh && b.command(ctx, m) {
|
||||
return
|
||||
}
|
||||
if m.threadTS != "" && !m.heard && b.moveCommand(ctx, m, m.threadTS) {
|
||||
return
|
||||
}
|
||||
root := m.threadTS
|
||||
@@ -179,6 +206,14 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
edit := func(text string) {
|
||||
_, _, _, _ = b.api.UpdateMessageContext(ctx, m.channel, ph, slack.MsgOptionText(text, false))
|
||||
}
|
||||
b.react(ctx, m.channel, m.ts, "eyes")
|
||||
outcome := "warning"
|
||||
defer func() {
|
||||
b.unreact(ctx, m.channel, m.ts, "eyes")
|
||||
if outcome != "" {
|
||||
b.react(ctx, m.channel, m.ts, outcome)
|
||||
}
|
||||
}()
|
||||
|
||||
ownerID, kind := m.owner()
|
||||
var off *node.NodeOfflineError
|
||||
@@ -220,6 +255,10 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
}
|
||||
|
||||
var id string
|
||||
text := m.text
|
||||
if !m.dm {
|
||||
text = b.displayName(ctx, m.user) + ": " + text
|
||||
}
|
||||
if sess == nil {
|
||||
if id, err = acp.NewSession(ctx, b.cfg.AgentCwd); err != nil {
|
||||
edit(":warning: Couldn't start a session: " + err.Error())
|
||||
@@ -229,6 +268,12 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
Channel: m.channel, ThreadTS: root, OwnerID: u.ID,
|
||||
ACPID: id, Title: title(m.text), CreatedAt: time.Now(),
|
||||
})
|
||||
if w := b.where(ctx, m.channel); w != "" {
|
||||
text = "(You're in " + w + ". Each message starts with the sender's name.)\n" + text
|
||||
}
|
||||
if m.dm && m.threadTS != "" {
|
||||
b.setAssistantTitle(ctx, m.channel, root, title(m.text))
|
||||
}
|
||||
} else {
|
||||
id = sess.ACPID
|
||||
if err := acp.LoadSession(ctx, id, b.cfg.AgentCwd); err != nil {
|
||||
@@ -237,16 +282,35 @@ func (b *Bot) onMessage(ctx context.Context, m incoming) {
|
||||
}
|
||||
}
|
||||
|
||||
text := m.text
|
||||
if !m.dm {
|
||||
text = b.displayName(ctx, m.user) + ": " + text
|
||||
if m.dm {
|
||||
text = b.viewingNote(ctx, m.channel, root) + text
|
||||
}
|
||||
|
||||
edit("_working…_")
|
||||
pctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
for _, ts := range []string{m.ts, ph} {
|
||||
key := m.channel + "/" + ts
|
||||
b.running.Store(key, cancel)
|
||||
defer b.running.Delete(key)
|
||||
}
|
||||
if m.dm && m.threadTS != "" {
|
||||
b.setAssistantStatus(ctx, m.channel, root, "is working…")
|
||||
defer b.setAssistantStatus(ctx, m.channel, root, "")
|
||||
}
|
||||
st := NewStreamer(b.api, m.channel, root, ph)
|
||||
stop, err := acp.Prompt(ctx, id, text, st.Update)
|
||||
stop, err := acp.Prompt(pctx, id, text, st.Update)
|
||||
if pctx.Err() != nil && ctx.Err() == nil {
|
||||
stop, err = "cancelled", nil
|
||||
}
|
||||
st.Finish(stop, err)
|
||||
_ = b.store.Touch(u.ID)
|
||||
switch {
|
||||
case stop == "cancelled":
|
||||
outcome = ""
|
||||
case err == nil:
|
||||
outcome = "white_check_mark"
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) command(ctx context.Context, m incoming) bool {
|
||||
@@ -256,16 +320,16 @@ func (b *Bot) command(ctx context.Context, m incoming) bool {
|
||||
args = strings.TrimRight(strings.TrimSpace(args), " .!?")
|
||||
|
||||
if verb == "move" || verb == "migrate" {
|
||||
return b.moveCommand(ctx, m, m.ts)
|
||||
return b.moveCommand(ctx, m, cmp.Or(m.threadTS, m.ts))
|
||||
}
|
||||
if verb == "grant" || verb == "revoke" || verb == "dm" || verb == "managers" || (verb == "permissions" && args != "") {
|
||||
return b.manageCommand(ctx, m, verb, args)
|
||||
}
|
||||
if need := commands.NeededPermission(verb, args); need != "" && !b.permitted(ctx, m.channel, "", m.user, kind, need) {
|
||||
if need := commands.NeededPermission(verb, args); need != "" && !b.permitted(ctx, m.channel, m.threadTS, m.user, kind, need) {
|
||||
return true
|
||||
}
|
||||
if verb == "stop" || verb == "scale" || verb == "storage" || verb == "disk" {
|
||||
if b.blocked(ctx, m.channel, "", ownerID) {
|
||||
if b.blocked(ctx, m.channel, m.threadTS, ownerID) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -277,7 +341,7 @@ func (b *Bot) command(ctx context.Context, m incoming) bool {
|
||||
case verb == "sessions" && args == "":
|
||||
reply = b.sessionList(ctx, ownerID)
|
||||
case verb == "status" && args == "":
|
||||
b.postStatus(ctx, m.channel, ownerID, kind)
|
||||
b.postStatus(ctx, m.channel, m.threadTS, ownerID, kind)
|
||||
return true
|
||||
case verb == "stop" && args == "":
|
||||
reply = b.stopFor(ctx, ownerID)
|
||||
@@ -303,7 +367,7 @@ func (b *Bot) command(ctx context.Context, m incoming) bool {
|
||||
default:
|
||||
return false
|
||||
}
|
||||
b.say(ctx, m.channel, "", reply)
|
||||
b.say(ctx, m.channel, m.threadTS, reply)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -311,6 +375,7 @@ func (b *Bot) onAction(ctx context.Context, cb slack.InteractionCallback) {
|
||||
if cb.Type != slack.InteractionTypeBlockActions {
|
||||
return
|
||||
}
|
||||
thread := cb.Message.ThreadTimestamp
|
||||
for _, a := range cb.ActionCallback.BlockActions {
|
||||
ownerID := a.Value
|
||||
if ownerID != cb.User.ID && ownerID != cb.Channel.ID {
|
||||
@@ -322,7 +387,7 @@ func (b *Bot) onAction(ctx context.Context, cb slack.InteractionCallback) {
|
||||
}
|
||||
u := b.store.Owner(ownerID)
|
||||
if need := map[string]model.Action{"recreate": model.ActMove, "scale": model.ActScale}[a.ActionID]; need != "" &&
|
||||
!b.permitted(ctx, cb.Channel.ID, "", cb.User.ID, kind, need) {
|
||||
!b.permitted(ctx, cb.Channel.ID, thread, cb.User.ID, kind, need) {
|
||||
continue
|
||||
}
|
||||
switch a.ActionID {
|
||||
@@ -331,10 +396,10 @@ func (b *Bot) onAction(ctx context.Context, cb slack.InteractionCallback) {
|
||||
b.dropClient(u)
|
||||
}
|
||||
if err := b.mgr.Recreate(ownerID); err != nil {
|
||||
b.say(ctx, cb.Channel.ID, "", ":warning: "+err.Error())
|
||||
b.say(ctx, cb.Channel.ID, thread, ":warning: "+err.Error())
|
||||
continue
|
||||
}
|
||||
b.say(ctx, cb.Channel.ID, "", "OK. Your next message starts a fresh container on a healthy node. "+
|
||||
b.say(ctx, cb.Channel.ID, thread, "OK. Your next message starts a fresh container on a healthy node. "+
|
||||
"Old threads start new sessions when you reply in them.")
|
||||
case "wait":
|
||||
if u == nil || u.Node == "" {
|
||||
@@ -346,17 +411,17 @@ func (b *Bot) onAction(ctx context.Context, cb slack.InteractionCallback) {
|
||||
}
|
||||
b.waiting[u.Node][cb.Channel.ID] = true
|
||||
b.waitMu.Unlock()
|
||||
b.say(ctx, cb.Channel.ID, "", fmt.Sprintf("OK, waiting for %s. I'll post here when it's back up.", u.Node))
|
||||
b.say(ctx, cb.Channel.ID, thread, fmt.Sprintf("OK, waiting for %s. I'll post here when it's back up.", u.Node))
|
||||
case "stop", "scale":
|
||||
if b.mgr.State(u) == manager.StateOffline {
|
||||
b.deadSlave(ctx, cb.Channel.ID, "", ownerID, u.Node)
|
||||
continue
|
||||
}
|
||||
if a.ActionID == "stop" {
|
||||
b.say(ctx, cb.Channel.ID, "", b.stopFor(ctx, ownerID))
|
||||
b.say(ctx, cb.Channel.ID, thread, b.stopFor(ctx, ownerID))
|
||||
} else {
|
||||
reply, _ := b.scaleFor(ctx, ownerID, kind, "up")
|
||||
b.say(ctx, cb.Channel.ID, "", reply)
|
||||
b.say(ctx, cb.Channel.ID, thread, reply)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -380,7 +445,7 @@ func (b *Bot) sessionList(ctx context.Context, ownerID string) string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (b *Bot) postStatus(ctx context.Context, channel, ownerID string, kind model.OwnerKind) {
|
||||
func (b *Bot) postStatus(ctx context.Context, channel, thread, ownerID string, kind model.OwnerKind) {
|
||||
u := b.store.Owner(ownerID)
|
||||
state := b.mgr.State(u)
|
||||
size := b.cfg.Container
|
||||
@@ -426,7 +491,11 @@ func (b *Bot) postStatus(ctx context.Context, channel, ownerID string, kind mode
|
||||
blocks = append(blocks, slack.NewActionBlock("container", buttons...))
|
||||
}
|
||||
|
||||
if _, _, err := b.api.PostMessageContext(ctx, channel, slack.MsgOptionText(text, false), slack.MsgOptionBlocks(blocks...)); err != nil {
|
||||
opts := []slack.MsgOption{slack.MsgOptionText(text, false), slack.MsgOptionBlocks(blocks...)}
|
||||
if thread != "" {
|
||||
opts = append(opts, slack.MsgOptionTS(thread))
|
||||
}
|
||||
if _, _, err := b.api.PostMessageContext(ctx, channel, opts...); err != nil {
|
||||
log.Printf("slack post: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/slack-go/slack"
|
||||
"github.com/slack-go/slack/slackevents"
|
||||
|
||||
"slackerbot/internal/acp"
|
||||
"slackerbot/internal/manager"
|
||||
@@ -247,3 +248,79 @@ func TestManageCommandsInSlack(t *testing.T) {
|
||||
t.Fatal("reset should drop the channel's own list")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListenInThreads(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"))
|
||||
_ = store.AddSession(&model.Session{Channel: "C1", ThreadTS: "1.0", OwnerID: "C1", ACPID: "a"})
|
||||
b := &Bot{store: store, botID: "UBOT", api: slack.New("xoxb", slack.OptionAPIURL(srv.URL+"/"))}
|
||||
ctx := context.Background()
|
||||
say := func(m incoming) string {
|
||||
if !b.listenCommand(ctx, m) {
|
||||
return "(went to the agent)"
|
||||
}
|
||||
log.mu.Lock()
|
||||
defer log.mu.Unlock()
|
||||
last := log.order[len(log.order)-1]
|
||||
if log.under[last] != m.threadTS {
|
||||
t.Fatalf("reply went to thread %q, want %q", log.under[last], m.threadTS)
|
||||
}
|
||||
return log.text[last]
|
||||
}
|
||||
|
||||
if b.listening("C1", "1.0", "hello") {
|
||||
t.Fatal("threads only read mentions by default")
|
||||
}
|
||||
if got := say(incoming{channel: "C1", threadTS: "1.0", text: "/listen all-messages"}); !strings.Contains(got, "read every reply") {
|
||||
t.Fatalf("thread toggle: %q", got)
|
||||
}
|
||||
if !b.listening("C1", "1.0", "hello") || b.listening("C1", "1.0", "<@UBOT> hello") || b.listening("C1", "9.0", "hello") {
|
||||
t.Fatal("all-messages should read plain replies in session threads only, and leave mentions to app_mention")
|
||||
}
|
||||
if got := say(incoming{channel: "C1", threadTS: "1.0", text: "listen to me carefully"}); got != "(went to the agent)" {
|
||||
t.Fatalf("plain sentence: %q", got)
|
||||
}
|
||||
if got := say(incoming{channel: "C1", threadTS: "9.0", text: "/listen all-messages"}); !strings.Contains(got, "no session") {
|
||||
t.Fatalf("thread without a session: %q", got)
|
||||
}
|
||||
if got := say(incoming{channel: "C1", text: "/listen all-messages"}); !strings.Contains(got, "this channel's threads") {
|
||||
t.Fatalf("channel default: %q", got)
|
||||
}
|
||||
_ = store.AddSession(&model.Session{Channel: "C1", ThreadTS: "2.0", OwnerID: "C1", ACPID: "b"})
|
||||
if !b.listening("C1", "2.0", "hello") {
|
||||
t.Fatal("new threads follow the channel default")
|
||||
}
|
||||
if got := say(incoming{channel: "D1", dm: true, text: "/listen only-mentions"}); !strings.Contains(got, "every message") {
|
||||
t.Fatalf("DMs always listen: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStopReactionCancelsPrompt(t *testing.T) {
|
||||
b := &Bot{}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
b.running.Store("C1/5.0", cancel)
|
||||
b.onReaction(&slackevents.ReactionAddedEvent{Reaction: "thumbsup", Item: slackevents.Item{Type: "message", Channel: "C1", Timestamp: "5.0"}})
|
||||
if ctx.Err() != nil {
|
||||
t.Fatal("other reactions must not cancel")
|
||||
}
|
||||
b.onReaction(&slackevents.ReactionAddedEvent{Reaction: "x", Item: slackevents.Item{Type: "message", Channel: "C1", Timestamp: "5.0"}})
|
||||
if ctx.Err() == nil {
|
||||
t.Fatal(":x: should cancel the reply")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommandsInAssistantThreads(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, api: slack.New("xoxb", slack.OptionAPIURL(srv.URL+"/"))}
|
||||
b.onMessage(context.Background(), incoming{user: "U1", channel: "D1", ts: "3.0", threadTS: "2.0", text: "help", dm: true})
|
||||
log.mu.Lock()
|
||||
defer log.mu.Unlock()
|
||||
if len(log.order) != 1 || log.under[log.order[0]] != "2.0" || !strings.Contains(log.text[log.order[0]], "Commands") {
|
||||
t.Fatalf("the first message of an assistant thread should run commands and answer in the thread: %v %v", log.order, log.under)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/slack-go/slack"
|
||||
"github.com/slack-go/slack/slackevents"
|
||||
|
||||
"slackerbot/internal/commands"
|
||||
"slackerbot/internal/model"
|
||||
)
|
||||
|
||||
var stopReactions = map[string]bool{"x": true, "octagonal_sign": true}
|
||||
|
||||
// A channel message without an @mention only reaches the agent when it's a
|
||||
// reply in a session thread that listens to all messages. Mentions are left to
|
||||
// app_mention, which Slack sends as well.
|
||||
func (b *Bot) listening(channel, threadTS, text string) bool {
|
||||
if threadTS == "" || strings.Contains(text, "<@"+b.botID) {
|
||||
return false
|
||||
}
|
||||
return b.store.Session(channel, threadTS) != nil && b.store.ThreadListen(channel, threadTS) == model.ListenAll
|
||||
}
|
||||
|
||||
func (b *Bot) listenCommand(ctx context.Context, m incoming) bool {
|
||||
verb, args, _ := strings.Cut(strings.TrimSpace(m.text), " ")
|
||||
if !commands.IsListen(strings.ToLower(verb)) {
|
||||
return false
|
||||
}
|
||||
c, err := commands.ParseListen(args)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
b.say(ctx, m.channel, m.threadTS, b.listenReply(m, c))
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *Bot) listenReply(m incoming, c commands.ListenCmd) string {
|
||||
if m.dm {
|
||||
return "In DMs I read every message, so there's nothing to switch."
|
||||
}
|
||||
if c.Channel || m.threadTS == "" {
|
||||
if c.Mode == "" {
|
||||
return "In this channel's threads I " + describeListen(b.store.ChannelListen(m.channel)) +
|
||||
", unless a thread sets its own. Change it with `/listen channel all-messages` or `/listen channel only-mentions`."
|
||||
}
|
||||
if err := b.store.SetChannelListen(m.channel, c.Mode); err != nil {
|
||||
return ":warning: " + err.Error()
|
||||
}
|
||||
return "Done. In this channel's threads I now " + describeListen(c.Mode) + ", unless a thread sets its own."
|
||||
}
|
||||
if b.store.Session(m.channel, m.threadTS) == nil {
|
||||
return "There's no session in this thread yet. Mention me with a message to start one first."
|
||||
}
|
||||
if c.Mode == "" {
|
||||
return "In this thread I " + describeListen(b.store.ThreadListen(m.channel, m.threadTS)) +
|
||||
". Change it with `/listen all-messages` or `/listen only-mentions`."
|
||||
}
|
||||
if err := b.store.SetSessionListen(m.channel, m.threadTS, c.Mode); err != nil {
|
||||
return ":warning: " + err.Error()
|
||||
}
|
||||
return "Done. In this thread I now " + describeListen(c.Mode) + "."
|
||||
}
|
||||
|
||||
func describeListen(mode model.ListenMode) string {
|
||||
if mode == model.ListenAll {
|
||||
return "read every reply (`all-messages`)"
|
||||
}
|
||||
return "only read replies that @mention me (`only-mentions`)"
|
||||
}
|
||||
|
||||
func (b *Bot) onReaction(e *slackevents.ReactionAddedEvent) {
|
||||
if e.Item.Type != "message" || !stopReactions[e.Reaction] {
|
||||
return
|
||||
}
|
||||
if cancel, ok := b.running.Load(e.Item.Channel + "/" + e.Item.Timestamp); ok {
|
||||
cancel.(context.CancelFunc)()
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) react(ctx context.Context, channel, ts, name string) {
|
||||
_ = b.api.AddReactionContext(ctx, name, slack.NewRefToMessage(channel, ts))
|
||||
}
|
||||
|
||||
func (b *Bot) unreact(ctx context.Context, channel, ts, name string) {
|
||||
_ = b.api.RemoveReactionContext(ctx, name, slack.NewRefToMessage(channel, ts))
|
||||
}
|
||||
@@ -36,7 +36,7 @@ func (b *Bot) manageCommand(ctx context.Context, m incoming, verb, args string)
|
||||
_, kind := m.owner()
|
||||
p := b.store.Permissions()
|
||||
reply := func(text string) bool {
|
||||
b.say(ctx, m.channel, "", text)
|
||||
b.say(ctx, m.channel, m.threadTS, text)
|
||||
return true
|
||||
}
|
||||
denied := func(global bool) bool {
|
||||
|
||||
Reference in new issue
Block a user