From b54b593c8c0c4855b8fef553b1d8872957d35350 Mon Sep 17 00:00:00 2001 From: Yury Nevalenny Date: Thu, 26 Oct 2023 12:04:33 +0000 Subject: [PATCH] reply in threads --- backend/app/slack/message_processors.go | 4 +++- backend/app/slack/slack.go | 21 ++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/backend/app/slack/message_processors.go b/backend/app/slack/message_processors.go index 693115a..cb01872 100644 --- a/backend/app/slack/message_processors.go +++ b/backend/app/slack/message_processors.go @@ -14,12 +14,14 @@ import ( func ProcessStreamingMessage( ctx context.Context, channelId string, + messageTs string, message string, seedData []models.Message, userMessagePrimer string, mode lib.ModeName, engineModel models.Engine, cancelContext context.CancelFunc, + replyInThread bool, ) { userId := ctx.Value(models.UserContext{}).(string) messageChannel, err := BOT.API.ChatCompleteStreaming( @@ -52,7 +54,7 @@ func ProcessStreamingMessage( } else if mode == lib.Grammar { responseText = "👀: " } - _, ts, _, err := BOT.SendMessageContext(ctx, channelId, slack.MsgOptionText(responseText, false)) + _, ts, _, err := BOT.SendMessageContext(ctx, channelId, slack.MsgOptionText(responseText, false), slack.MsgOptionTS(messageTs)) if err != nil { log.Errorf("Failed to send primer message in chat: %s, userId: %s, %v", channelId, userId, err) } diff --git a/backend/app/slack/slack.go b/backend/app/slack/slack.go index 647ab29..c3e034f 100644 --- a/backend/app/slack/slack.go +++ b/backend/app/slack/slack.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "net/http" + "strconv" "strings" "talk2robots/m/v2/app/config" "talk2robots/m/v2/app/db/redis" @@ -88,14 +89,19 @@ func slackEventsHandler(ctx *fasthttp.RequestCtx) { innerEvent := eventsAPIEvent.InnerEvent switch ev := innerEvent.Data.(type) { case *slackevents.AppMentionEvent: - handleAppMentionEvent(ctx, "slack:"+ev.User, ev.Channel, ev.Text) + if ev.User != "" && ev.BotID == "" { + handleMessageEvent(ctx, "slack:"+ev.User, ev.Channel, ev.TimeStamp, ev.Text, true) + } return case *slackevents.MessageEvent: log.Infof("Received slack message event: %+v", ev) if ev.ChannelType == "im" && ev.User != "" && ev.SubType != "bot_message" && ev.BotID == "" { - handleAppMentionEvent(ctx, "slack:"+ev.User, ev.Channel, ev.Text) + handleMessageEvent(ctx, "slack:"+ev.User, ev.Channel, ev.TimeStamp, ev.Text, false) } return + case *slackevents.ReactionAddedEvent: + log.Infof("Received slack reaction event: %+v", ev) + return case *slackevents.AppHomeOpenedEvent: handleAppHomeOpenedEvent(ctx, ev) return @@ -203,7 +209,7 @@ func handleUrlVerificationEvent(ctx *fasthttp.RequestCtx) { func handleAppHomeOpenedEvent(requestContext *fasthttp.RequestCtx, ev *slackevents.AppHomeOpenedEvent) { userId := "slack:" + ev.User - user, currentContext, cancelFunc, err := lib.SetupUserAndContext(userId, "slack") + user, _, cancelFunc, err := lib.SetupUserAndContext(userId, "slack") if err != nil { if err == lib.ErrUserBanned { log.Infof("User %s is banned", userId) @@ -251,15 +257,16 @@ func handleAppHomeOpenedEvent(requestContext *fasthttp.RequestCtx, ev *slackeven }, } - hash := sha256.New() - _, err = BOT.Client.PublishViewContext(currentContext, ev.User, homeTabContent, string(hash.Sum(nil))) + seed := time.Now().UnixNano() + hash := sha256.New().Sum([]byte(strconv.FormatInt(seed, 10))) + _, err = BOT.Client.PublishView(ev.User, homeTabContent, string(hash)) if err != nil { log.Errorf("Failed to publish home tab view: %v", err) } } -func handleAppMentionEvent(requestContext *fasthttp.RequestCtx, userId string, channel string, messageText string) { +func handleMessageEvent(requestContext *fasthttp.RequestCtx, userId string, channel string, messageTs string, messageText string, replyInThread bool) { // throw err if current user doesn't start with "slack:" if !strings.HasPrefix(userId, "slack:") { log.Errorf("Invalid user: %s", userId) @@ -294,5 +301,5 @@ func handleAppMentionEvent(requestContext *fasthttp.RequestCtx, userId string, c log.Infof("Received message in slack chat: %s, user: %s, mode: %s, initiating request to OpenAI", channel, userId, mode) engineModel := redis.GetChatEngine(userId) - ProcessStreamingMessage(currentContext, channel, messageText, seedData, userMessagePrimer, mode, engineModel, cancelFunc) + ProcessStreamingMessage(currentContext, channel, messageTs, messageText, seedData, userMessagePrimer, mode, engineModel, cancelFunc, replyInThread) }