prepare for multiple bots
This commit is contained in:
parent
ae6d0532bd
commit
988951970b
18
bot/bot.go
18
bot/bot.go
|
@ -3,10 +3,7 @@ package bot
|
|||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/chzyer/readline"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/sashabaranov/go-openai"
|
||||
"golang.org/x/exp/slog"
|
||||
"maunium.net/go/mautrix"
|
||||
|
@ -25,11 +22,11 @@ type Config struct {
|
|||
DBPath string
|
||||
Pickle string
|
||||
OpenAIKey string
|
||||
SystemPrompt string
|
||||
}
|
||||
|
||||
type Bot struct {
|
||||
config Config
|
||||
readline *readline.Instance
|
||||
client *mautrix.Client
|
||||
cryptoHelper *cryptohelper.CryptoHelper
|
||||
characters []Character
|
||||
|
@ -53,10 +50,6 @@ func (m *Bot) Init() error {
|
|||
var oei mautrix.OldEventIgnorer
|
||||
oei.Register(client.Syncer.(mautrix.ExtensibleSyncer))
|
||||
m.client = client
|
||||
m.client.Log = zerolog.New(zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
|
||||
w.TimeFormat = time.Stamp
|
||||
})).With().Timestamp().Logger().Level(zerolog.InfoLevel)
|
||||
|
||||
m.cryptoHelper, err = cryptohelper.NewCryptoHelper(client, []byte(m.config.Pickle), m.config.DBPath)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -70,11 +63,8 @@ func (m *Bot) Init() error {
|
|||
return err
|
||||
}
|
||||
m.client.Crypto = m.cryptoHelper
|
||||
|
||||
m.gptClient = NewGPT(m.config.OpenAIKey)
|
||||
|
||||
m.conversations = make(Conversations, 0)
|
||||
|
||||
m.AddEventHandler(m.InviteHandler())
|
||||
m.AddEventHandler(m.ResponseHandler())
|
||||
|
||||
|
@ -114,7 +104,7 @@ func (m *Bot) InviteHandler() (event.Type, mautrix.EventHandler) {
|
|||
return
|
||||
}
|
||||
|
||||
m.logger.Info("Joined room after invite", slog.String("room_id", evt.RoomID.String()), slog.String("inviter", evt.Sender.String()))
|
||||
m.logger.Info("joined room after invite", slog.String("room_id", evt.RoomID.String()), slog.String("inviter", evt.Sender.String()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -156,11 +146,11 @@ func (m *Bot) ResponseHandler() (event.Type, mautrix.EventHandler) {
|
|||
}
|
||||
}
|
||||
|
||||
// find out if message is addressed to the bot
|
||||
// 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))) {
|
||||
m.logger.Info("message is addressed to bot", slog.String("event_id", eventID.String()))
|
||||
conv = NewConversation(eventID, content.Body)
|
||||
conv = NewConversation(eventID, m.config.SystemPrompt, content.Body)
|
||||
m.conversations = append(m.conversations, conv)
|
||||
}
|
||||
|
||||
|
|
|
@ -6,8 +6,6 @@ import (
|
|||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
const systemPrompt = "You are a chatbot that helps people by responding to their questions with short messages."
|
||||
|
||||
type Character struct {
|
||||
UserID string
|
||||
Password string
|
||||
|
@ -27,7 +25,7 @@ type Conversation struct {
|
|||
Messages []Message
|
||||
}
|
||||
|
||||
func NewConversation(id id.EventID, question string) *Conversation {
|
||||
func NewConversation(id id.EventID, systemPrompt, question string) *Conversation {
|
||||
return &Conversation{
|
||||
Messages: []Message{
|
||||
{
|
||||
|
|
12
main.go
12
main.go
|
@ -13,7 +13,7 @@ func main() {
|
|||
|
||||
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
|
||||
matrixClient := bot.New(bot.Config{
|
||||
botConfigs := []bot.Config{{
|
||||
Homeserver: getParam("MATRIX_HOMESERVER", "http://localhost"),
|
||||
UserID: getParam("MATRIX_USER_ID", "@bot:localhost"),
|
||||
UserPassword: getParam("MATRIX_PASSWORD", "secret"),
|
||||
|
@ -22,13 +22,17 @@ func main() {
|
|||
DBPath: getParam("BOT_DB_PATH", "bot.db"),
|
||||
Pickle: getParam("BOT_PICKLE", "scrambled"),
|
||||
OpenAIKey: getParam("OPENAI_API_KEY", "no key"),
|
||||
}, logger)
|
||||
SystemPrompt: "You are a chatbot that helps people by responding to their questions with short messages.",
|
||||
}}
|
||||
|
||||
if err := matrixClient.Init(); err != nil {
|
||||
for _, bc := range botConfigs {
|
||||
b := bot.New(bc, logger)
|
||||
if err := b.Init(); err != nil {
|
||||
logger.Error(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
go matrixClient.Run()
|
||||
go b.Run()
|
||||
}
|
||||
|
||||
done := make(chan os.Signal)
|
||||
signal.Notify(done, os.Interrupt)
|
||||
|
|
Loading…
Reference in New Issue