From fbc6aef78362be2fc522adf1b1ab38a667d2494e Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Tue, 13 Jun 2023 19:58:56 +0200 Subject: [PATCH] default bot for unaddressed questions --- bot/bot.go | 62 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 17c2283..5359864 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -1,8 +1,8 @@ package bot import ( - "fmt" "strings" + "sync" "github.com/sashabaranov/go-openai" "golang.org/x/exp/slog" @@ -13,19 +13,44 @@ import ( "maunium.net/go/mautrix/id" ) +var ( + botNames = []string{} + mu = &sync.Mutex{} +) + +func BotNameAppend(name string) { + mu.Lock() + defer mu.Unlock() + + botNames = append(botNames, name) +} + +func BotNameRegistered(want string) bool { + mu.Lock() + defer mu.Unlock() + for _, got := range botNames { + if strings.ToLower(want) == strings.ToLower(got) { + return true + } + } + + return false +} + type ConfigOpenAI struct { APIKey string } type ConfigBot struct { - DBPath string - Pickle string - Homeserver string - UserID string - UserAccessKey string - UserPassword string - UserDisplayName string - SystemPrompt string + DBPath string + Pickle string + Homeserver string + UserID string + UserAccessKey string + UserPassword string + UserDisplayName string + SystemPrompt string + AnswerUnaddressed bool } type Config struct { @@ -78,6 +103,9 @@ func (m *Bot) Init() error { m.AddEventHandler(m.InviteHandler()) m.AddEventHandler(m.ResponseHandler()) + m.config.UserDisplayName = strings.ToLower(m.config.UserDisplayName) + BotNameAppend(m.config.UserDisplayName) + return nil } @@ -156,13 +184,25 @@ func (m *Bot) ResponseHandler() (event.Type, mautrix.EventHandler) { } } - // find out if message is a new question addressed to the bot m.logger.Info(content.Body) - if conv == nil && strings.HasPrefix(strings.ToLower(content.Body), strings.ToLower(fmt.Sprintf("%s: ", m.config.UserDisplayName))) { + addressedTo, _, isAddressed := strings.Cut(content.Body, ": ") + addressedTo = strings.TrimSpace(strings.ToLower(addressedTo)) + if strings.Contains(addressedTo, " ") { + isAddressed = false // only display names without spaces, otherwise no way to know if it's a name or not + } + + // find out if message is a new question addressed to the bot + if conv == nil && isAddressed && addressedTo == m.config.UserDisplayName { m.logger.Info("message is addressed to bot", slog.String("event_id", eventID.String())) conv = NewConversation(eventID, m.config.SystemPrompt, content.Body) m.conversations = append(m.conversations, conv) } + // find out if the message is addressed to no-one and this bot answers those + if conv == nil && !isAddressed && m.config.AnswerUnaddressed { + m.logger.Info("message is addressed to no-one", slog.String("event_id", eventID.String())) + conv = NewConversation(eventID, m.config.SystemPrompt, content.Body) + m.conversations = append(m.conversations, conv) + } if conv == nil { m.logger.Info("apparently not for us, ignoring", slog.String("event_id", eventID.String()))