Allow slack bots to initiate conversations
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
@@ -41,6 +41,7 @@ type Agent struct {
|
||||
|
||||
mcpActions types.Actions
|
||||
|
||||
subscriberMutex sync.Mutex
|
||||
newMessagesSubscribers []func(openai.ChatCompletionMessage)
|
||||
}
|
||||
|
||||
@@ -118,7 +119,10 @@ func (a *Agent) startNewConversationsConsumer() {
|
||||
return
|
||||
|
||||
case msg := <-a.newConversations:
|
||||
for _, s := range a.newMessagesSubscribers {
|
||||
a.subscriberMutex.Lock()
|
||||
subs := a.newMessagesSubscribers
|
||||
a.subscriberMutex.Unlock()
|
||||
for _, s := range subs {
|
||||
s(msg)
|
||||
}
|
||||
}
|
||||
@@ -126,6 +130,12 @@ func (a *Agent) startNewConversationsConsumer() {
|
||||
}()
|
||||
}
|
||||
|
||||
func (a *Agent) AddSubscriber(f func(openai.ChatCompletionMessage)) {
|
||||
a.subscriberMutex.Lock()
|
||||
defer a.subscriberMutex.Unlock()
|
||||
a.newMessagesSubscribers = append(a.newMessagesSubscribers, f)
|
||||
}
|
||||
|
||||
// StopAction stops the current action
|
||||
// if any. Can be called before adding a new job.
|
||||
func (a *Agent) StopAction() {
|
||||
|
||||
@@ -642,6 +642,11 @@ func (t *Slack) handleMention(
|
||||
}
|
||||
|
||||
func (t *Slack) Start(a *agent.Agent) {
|
||||
postMessageParams := slack.PostMessageParameters{
|
||||
LinkNames: 1,
|
||||
Markdown: true,
|
||||
}
|
||||
|
||||
api := slack.New(
|
||||
t.botToken,
|
||||
// slack.OptionDebug(true),
|
||||
@@ -649,13 +654,23 @@ func (t *Slack) Start(a *agent.Agent) {
|
||||
slack.OptionAppLevelToken(t.appToken),
|
||||
)
|
||||
|
||||
t.apiClient = api
|
||||
|
||||
postMessageParams := slack.PostMessageParameters{
|
||||
LinkNames: 1,
|
||||
Markdown: true,
|
||||
if t.channelID != "" {
|
||||
// handle new conversations
|
||||
a.AddSubscriber(func(ccm openai.ChatCompletionMessage) {
|
||||
_, _, err := api.PostMessage(t.channelID,
|
||||
slack.MsgOptionLinkNames(true),
|
||||
slack.MsgOptionEnableLinkUnfurl(),
|
||||
slack.MsgOptionText(ccm.Content, true),
|
||||
slack.MsgOptionPostMessageParameters(postMessageParams),
|
||||
)
|
||||
if err != nil {
|
||||
xlog.Error(fmt.Sprintf("Error posting message: %v", err))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.apiClient = api
|
||||
|
||||
client := socketmode.New(
|
||||
api,
|
||||
//socketmode.OptionDebug(true),
|
||||
|
||||
Reference in New Issue
Block a user