Skip to content

Commit

Permalink
reply in threads
Browse files Browse the repository at this point in the history
  • Loading branch information
radiantspace committed Oct 26, 2023
1 parent 98e80ed commit b54b593
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
4 changes: 3 additions & 1 deletion backend/app/slack/message_processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
}
Expand Down
21 changes: 14 additions & 7 deletions backend/app/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"talk2robots/m/v2/app/config"
"talk2robots/m/v2/app/db/redis"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

0 comments on commit b54b593

Please sign in to comment.