accept invites only when allowed

This commit is contained in:
Erik Winter 2023-06-14 15:19:24 +02:00
parent fbc6aef783
commit b241fc6961
2 changed files with 14 additions and 4 deletions

View File

@ -77,7 +77,7 @@ func New(openaiKey string, cfg ConfigBot, logger *slog.Logger) *Bot {
} }
} }
func (m *Bot) Init() error { func (m *Bot) Init(acceptInvites bool) error {
client, err := mautrix.NewClient(m.config.Homeserver, id.UserID(m.config.UserID), m.config.UserAccessKey) client, err := mautrix.NewClient(m.config.Homeserver, id.UserID(m.config.UserID), m.config.UserAccessKey)
if err != nil { if err != nil {
return err return err
@ -100,7 +100,9 @@ func (m *Bot) Init() error {
m.client.Crypto = m.cryptoHelper m.client.Crypto = m.cryptoHelper
m.gptClient = NewGPT(m.openaiKey) m.gptClient = NewGPT(m.openaiKey)
m.conversations = make(Conversations, 0) m.conversations = make(Conversations, 0)
if acceptInvites {
m.AddEventHandler(m.InviteHandler()) m.AddEventHandler(m.InviteHandler())
}
m.AddEventHandler(m.ResponseHandler()) m.AddEventHandler(m.ResponseHandler())
m.config.UserDisplayName = strings.ToLower(m.config.UserDisplayName) m.config.UserDisplayName = strings.ToLower(m.config.UserDisplayName)
@ -168,8 +170,10 @@ func (m *Bot) ResponseHandler() (event.Type, mautrix.EventHandler) {
var conv *Conversation var conv *Conversation
// find out if it is a reply to a known conversation // find out if it is a reply to a known conversation
parentID := id.EventID("") parentID := id.EventID("")
var hasParent bool
if relatesTo := content.GetRelatesTo(); relatesTo != nil { if relatesTo := content.GetRelatesTo(); relatesTo != nil {
if parentID = relatesTo.GetReplyTo(); parentID != "" { if parentID = relatesTo.GetReplyTo(); parentID != "" {
hasParent = true
m.logger.Info("message is a reply", slog.String("parent_id", parentID.String())) m.logger.Info("message is a reply", slog.String("parent_id", parentID.String()))
if c := m.conversations.FindByEventID(parentID); c != nil { if c := m.conversations.FindByEventID(parentID); c != nil {
m.logger.Info("found parent, appending message to conversation", slog.String("event_id", eventID.String())) m.logger.Info("found parent, appending message to conversation", slog.String("event_id", eventID.String()))
@ -198,7 +202,7 @@ func (m *Bot) ResponseHandler() (event.Type, mautrix.EventHandler) {
m.conversations = append(m.conversations, conv) m.conversations = append(m.conversations, conv)
} }
// find out if the message is addressed to no-one and this bot answers those // find out if the message is addressed to no-one and this bot answers those
if conv == nil && !isAddressed && m.config.AnswerUnaddressed { if conv == nil && !isAddressed && !hasParent && m.config.AnswerUnaddressed {
m.logger.Info("message is addressed to no-one", slog.String("event_id", eventID.String())) m.logger.Info("message is addressed to no-one", slog.String("event_id", eventID.String()))
conv = NewConversation(eventID, m.config.SystemPrompt, content.Body) conv = NewConversation(eventID, m.config.SystemPrompt, content.Body)
m.conversations = append(m.conversations, conv) m.conversations = append(m.conversations, conv)

View File

@ -48,11 +48,17 @@ func main() {
config.OpenAI = bot.ConfigOpenAI{ config.OpenAI = bot.ConfigOpenAI{
APIKey: getParam("OPENAI_API_KEY", ""), APIKey: getParam("OPENAI_API_KEY", ""),
} }
var acceptInvites bool
if getParam("MATRIX_ACCEPT_INVITES", "false") == "true" {
acceptInvites = true
}
logger.Info("loaded config", slog.Int("bots", len(config.Bots))) logger.Info("loaded config", slog.Int("bots", len(config.Bots)))
for _, bc := range config.Bots { for _, bc := range config.Bots {
b := bot.New(config.OpenAI.APIKey, bc, logger) b := bot.New(config.OpenAI.APIKey, bc, logger)
if err := b.Init(); err != nil { if err := b.Init(acceptInvites); err != nil {
logger.Error(err.Error()) logger.Error(err.Error())
os.Exit(1) os.Exit(1)
} }