Allow slack connector to answer to channel messages

This commit is contained in:
mudler
2024-04-10 20:28:15 +02:00
parent 437da44590
commit 69cbcc5c88
3 changed files with 31 additions and 9 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"strings"
"github.com/mudler/local-agent-framework/agent"
@@ -16,12 +17,16 @@ import (
type Slack struct {
appToken string
botToken string
channelID string
alwaysReply bool
}
func NewSlack(config map[string]string) *Slack {
return &Slack{
appToken: config["appToken"],
botToken: config["botToken"],
channelID: config["channelID"],
alwaysReply: config["alwaysReply"] == "true",
}
}
@@ -48,7 +53,7 @@ func (t *Slack) Start(a *agent.Agent) {
client := socketmode.New(
api,
// socketmode.OptionDebug(true),
//socketmode.OptionDebug(true),
//socketmode.OptionLog(log.New(os.Stdout, "socketmode: ", log.Lshortfile|log.LstdFlags)),
)
go func() {
@@ -83,15 +88,23 @@ func (t *Slack) Start(a *agent.Agent) {
switch ev := innerEvent.Data.(type) {
case *slackevents.MessageEvent:
if t.channelID == "" && !t.alwaysReply || // If we have set alwaysReply and no channelID
t.channelID != ev.Channel { // If we have a channelID and it's not the same as the event channel
// Skip messages from other channels
fmt.Println("Skipping reply to channel", ev.Channel, t.channelID)
continue
}
if b.UserID == ev.User {
// Skip messages from ourselves
return
continue
}
message := ev.Text
res := a.Ask(
agent.WithText(message),
)
_, _, err = api.PostMessage(ev.Channel,
slack.MsgOptionText(res.Response, false),
slack.MsgOptionPostMessageParameters(slack.PostMessageParameters{LinkNames: 1}))
@@ -102,10 +115,14 @@ func (t *Slack) Start(a *agent.Agent) {
if b.UserID == ev.User {
// Skip messages from ourselves
return
continue
}
message := ev.Text
// strip our id from the message
message = strings.ReplaceAll(message, "<@"+b.UserID+"> ", "")
fmt.Println("Message", message)
res := a.Ask(
agent.WithText(message),
)

View File

@@ -216,7 +216,7 @@ func (a *App) Delete(pool *AgentPool) func(c *fiber.Ctx) error {
fmt.Println("Error removing agent", err)
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}
return c.Redirect("/")
return c.Redirect("/agents")
}
}
@@ -237,7 +237,7 @@ func (a *App) Create(pool *AgentPool) func(c *fiber.Ctx) error {
c.Status(http.StatusInternalServerError).SendString(err.Error())
return nil
}
return c.Redirect("/")
return c.Redirect("/agents")
}
}

View File

@@ -21,15 +21,20 @@ oauth_config:
- channels:read
- chat:write
- commands
- groups:history
- im:history
- im:read
- im:write
- mpim:history
- users:read
- users:read.email
- groups:read
settings:
event_subscriptions:
bot_events:
- app_mention
- message.channels
- message.groups
- message.im
interactivity:
is_enabled: true