Allow slack connector to answer to channel messages
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/mudler/local-agent-framework/agent"
|
"github.com/mudler/local-agent-framework/agent"
|
||||||
|
|
||||||
@@ -16,12 +17,16 @@ import (
|
|||||||
type Slack struct {
|
type Slack struct {
|
||||||
appToken string
|
appToken string
|
||||||
botToken string
|
botToken string
|
||||||
|
channelID string
|
||||||
|
alwaysReply bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSlack(config map[string]string) *Slack {
|
func NewSlack(config map[string]string) *Slack {
|
||||||
return &Slack{
|
return &Slack{
|
||||||
appToken: config["appToken"],
|
appToken: config["appToken"],
|
||||||
botToken: config["botToken"],
|
botToken: config["botToken"],
|
||||||
|
channelID: config["channelID"],
|
||||||
|
alwaysReply: config["alwaysReply"] == "true",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +53,7 @@ func (t *Slack) Start(a *agent.Agent) {
|
|||||||
|
|
||||||
client := socketmode.New(
|
client := socketmode.New(
|
||||||
api,
|
api,
|
||||||
// socketmode.OptionDebug(true),
|
//socketmode.OptionDebug(true),
|
||||||
//socketmode.OptionLog(log.New(os.Stdout, "socketmode: ", log.Lshortfile|log.LstdFlags)),
|
//socketmode.OptionLog(log.New(os.Stdout, "socketmode: ", log.Lshortfile|log.LstdFlags)),
|
||||||
)
|
)
|
||||||
go func() {
|
go func() {
|
||||||
@@ -83,15 +88,23 @@ func (t *Slack) Start(a *agent.Agent) {
|
|||||||
|
|
||||||
switch ev := innerEvent.Data.(type) {
|
switch ev := innerEvent.Data.(type) {
|
||||||
case *slackevents.MessageEvent:
|
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 {
|
if b.UserID == ev.User {
|
||||||
// Skip messages from ourselves
|
// Skip messages from ourselves
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
message := ev.Text
|
message := ev.Text
|
||||||
res := a.Ask(
|
res := a.Ask(
|
||||||
agent.WithText(message),
|
agent.WithText(message),
|
||||||
)
|
)
|
||||||
|
|
||||||
_, _, err = api.PostMessage(ev.Channel,
|
_, _, err = api.PostMessage(ev.Channel,
|
||||||
slack.MsgOptionText(res.Response, false),
|
slack.MsgOptionText(res.Response, false),
|
||||||
slack.MsgOptionPostMessageParameters(slack.PostMessageParameters{LinkNames: 1}))
|
slack.MsgOptionPostMessageParameters(slack.PostMessageParameters{LinkNames: 1}))
|
||||||
@@ -102,10 +115,14 @@ func (t *Slack) Start(a *agent.Agent) {
|
|||||||
|
|
||||||
if b.UserID == ev.User {
|
if b.UserID == ev.User {
|
||||||
// Skip messages from ourselves
|
// Skip messages from ourselves
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
message := ev.Text
|
message := ev.Text
|
||||||
|
|
||||||
|
// strip our id from the message
|
||||||
|
message = strings.ReplaceAll(message, "<@"+b.UserID+"> ", "")
|
||||||
|
fmt.Println("Message", message)
|
||||||
|
|
||||||
res := a.Ask(
|
res := a.Ask(
|
||||||
agent.WithText(message),
|
agent.WithText(message),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -216,7 +216,7 @@ func (a *App) Delete(pool *AgentPool) func(c *fiber.Ctx) error {
|
|||||||
fmt.Println("Error removing agent", err)
|
fmt.Println("Error removing agent", err)
|
||||||
return c.Status(http.StatusInternalServerError).SendString(err.Error())
|
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())
|
c.Status(http.StatusInternalServerError).SendString(err.Error())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return c.Redirect("/")
|
return c.Redirect("/agents")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,15 +21,20 @@ oauth_config:
|
|||||||
- channels:read
|
- channels:read
|
||||||
- chat:write
|
- chat:write
|
||||||
- commands
|
- commands
|
||||||
|
- groups:history
|
||||||
- im:history
|
- im:history
|
||||||
- im:read
|
- im:read
|
||||||
- im:write
|
- im:write
|
||||||
|
- mpim:history
|
||||||
- users:read
|
- users:read
|
||||||
- users:read.email
|
- users:read.email
|
||||||
|
- groups:read
|
||||||
settings:
|
settings:
|
||||||
event_subscriptions:
|
event_subscriptions:
|
||||||
bot_events:
|
bot_events:
|
||||||
- app_mention
|
- app_mention
|
||||||
|
- message.channels
|
||||||
|
- message.groups
|
||||||
- message.im
|
- message.im
|
||||||
interactivity:
|
interactivity:
|
||||||
is_enabled: true
|
is_enabled: true
|
||||||
|
|||||||
Reference in New Issue
Block a user