make logger a parameter
This commit is contained in:
parent
5bb6427b1d
commit
4fb1ec915b
|
@ -1,12 +1,12 @@
|
||||||
package bot
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/chzyer/readline"
|
"github.com/chzyer/readline"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/sashabaranov/go-openai"
|
"github.com/sashabaranov/go-openai"
|
||||||
|
"golang.org/x/exp/slog"
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
"maunium.net/go/mautrix/crypto/cryptohelper"
|
"maunium.net/go/mautrix/crypto/cryptohelper"
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
|
@ -97,38 +97,29 @@ func (m *Matrix) AddEventHandler(eventType event.Type, handler mautrix.EventHand
|
||||||
syncer.OnEventType(eventType, handler)
|
syncer.OnEventType(eventType, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Matrix) InviteHandler() (event.Type, mautrix.EventHandler) {
|
func (m *Matrix) InviteHandler(logger *slog.Logger) (event.Type, mautrix.EventHandler) {
|
||||||
return event.StateMember, func(source mautrix.EventSource, evt *event.Event) {
|
return event.StateMember, func(source mautrix.EventSource, evt *event.Event) {
|
||||||
if evt.GetStateKey() == m.client.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
|
if evt.GetStateKey() == m.client.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
|
||||||
_, err := m.client.JoinRoomByID(evt.RoomID)
|
_, err := m.client.JoinRoomByID(evt.RoomID)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
m.client.Log.Info().
|
logger.Error("failed to join room after invite", slog.String("err", err.Error()), slog.String("room_id", evt.RoomID.String()), slog.String("inviter", evt.Sender.String()))
|
||||||
Str("room_id", evt.RoomID.String()).
|
return
|
||||||
Str("inviter", evt.Sender.String()).
|
|
||||||
Msg("Joined room after invite")
|
|
||||||
} else {
|
|
||||||
m.client.Log.Error().Err(err).
|
|
||||||
Str("room_id", evt.RoomID.String()).
|
|
||||||
Str("inviter", evt.Sender.String()).
|
|
||||||
Msg("Failed to join room after invite")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Info("Joined room after invite", slog.String("room_id", evt.RoomID.String()), slog.String("inviter", evt.Sender.String()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Matrix) ResponseHandler() (event.Type, mautrix.EventHandler) {
|
func (m *Matrix) ResponseHandler(logger *slog.Logger) (event.Type, mautrix.EventHandler) {
|
||||||
return event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
return event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
||||||
content := evt.Content.AsMessage()
|
content := evt.Content.AsMessage()
|
||||||
eventID := evt.ID
|
eventID := evt.ID
|
||||||
m.client.Log.Info().
|
logger.Info("received message", slog.String("content", content.Body))
|
||||||
Str("content", content.Body).
|
|
||||||
Msg("received message")
|
|
||||||
|
|
||||||
conv := m.conversations.FindByEventID(eventID)
|
conv := m.conversations.FindByEventID(eventID)
|
||||||
if conv != nil {
|
if conv != nil {
|
||||||
m.client.Log.Info().
|
logger.Info("known message, ignoring", slog.String("event_id", eventID.String()))
|
||||||
Str("event_id", eventID.String()).
|
|
||||||
Msg("already known message, ignoring")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,7 +128,7 @@ func (m *Matrix) ResponseHandler() (event.Type, mautrix.EventHandler) {
|
||||||
parentID = relatesTo.GetReplyTo()
|
parentID = relatesTo.GetReplyTo()
|
||||||
}
|
}
|
||||||
if parentID != "" {
|
if parentID != "" {
|
||||||
m.client.Log.Info().Msg("parent found, looking for conversation")
|
logger.Info("parent found, looking for conversation", slog.String("parent_id", parentID.String()))
|
||||||
conv = m.conversations.FindByEventID(parentID)
|
conv = m.conversations.FindByEventID(parentID)
|
||||||
}
|
}
|
||||||
if conv != nil {
|
if conv != nil {
|
||||||
|
@ -147,18 +138,18 @@ func (m *Matrix) ResponseHandler() (event.Type, mautrix.EventHandler) {
|
||||||
Role: openai.ChatMessageRoleUser,
|
Role: openai.ChatMessageRoleUser,
|
||||||
Content: content.Body,
|
Content: content.Body,
|
||||||
})
|
})
|
||||||
m.client.Log.Info().Msg("found parent, appending message to conversation")
|
logger.Info("found parent, appending message to conversation", slog.String("event_id", eventID.String()))
|
||||||
} else {
|
} else {
|
||||||
conv = NewConversation(eventID, content.Body)
|
conv = NewConversation(eventID, content.Body)
|
||||||
m.conversations = append(m.conversations, conv)
|
m.conversations = append(m.conversations, conv)
|
||||||
m.client.Log.Info().Msg("no parent found, starting new conversation")
|
logger.Info("no parent found, starting new conversation", slog.String("event_id", eventID.String()))
|
||||||
}
|
}
|
||||||
|
|
||||||
if evt.Sender != id.UserID(m.config.UserID) {
|
if evt.Sender != id.UserID(m.config.UserID) {
|
||||||
// get reply from GPT
|
// get reply from GPT
|
||||||
reply, err := m.gptClient.Complete(conv)
|
reply, err := m.gptClient.Complete(conv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.client.Log.Error().Err(err).Msg("OpenAI API returned with ")
|
logger.Error("failed to get reply from openai", slog.String("err", err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,11 +160,14 @@ func (m *Matrix) ResponseHandler() (event.Type, mautrix.EventHandler) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if _, err := m.client.SendMessageEvent(evt.RoomID, event.EventMessage, &formattedReply); err != nil {
|
if _, err := m.client.SendMessageEvent(evt.RoomID, event.EventMessage, &formattedReply); err != nil {
|
||||||
m.client.Log.Err(err).Msg("failed to send message")
|
logger.Error("failed to send message", slog.String("err", err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
m.client.Log.Info().Str("message", fmt.Sprintf("%+v", formattedReply.Body)).Msg("Sent reply")
|
if len(reply) > 30 {
|
||||||
|
reply = reply[:30] + "..."
|
||||||
|
}
|
||||||
|
logger.Info("sent reply", slog.String("parent_id", eventID.String()), slog.String("content", reply))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
4
main.go
4
main.go
|
@ -28,8 +28,8 @@ func main() {
|
||||||
}
|
}
|
||||||
go matrixClient.Run()
|
go matrixClient.Run()
|
||||||
|
|
||||||
matrixClient.AddEventHandler(matrixClient.InviteHandler())
|
matrixClient.AddEventHandler(matrixClient.InviteHandler(logger))
|
||||||
matrixClient.AddEventHandler(matrixClient.ResponseHandler())
|
matrixClient.AddEventHandler(matrixClient.ResponseHandler(logger))
|
||||||
|
|
||||||
done := make(chan os.Signal)
|
done := make(chan os.Signal)
|
||||||
signal.Notify(done, os.Interrupt)
|
signal.Notify(done, os.Interrupt)
|
||||||
|
|
Loading…
Reference in New Issue