move bot config to file
This commit is contained in:
parent
988951970b
commit
815b7e0808
|
@ -1,2 +1,3 @@
|
|||
.idea
|
||||
*.db
|
||||
*.db
|
||||
*.toml
|
28
bot/bot.go
28
bot/bot.go
|
@ -13,20 +13,29 @@ import (
|
|||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
type ConfigOpenAI struct {
|
||||
APIKey string
|
||||
}
|
||||
|
||||
type ConfigBot struct {
|
||||
DBPath string
|
||||
Pickle string
|
||||
Homeserver string
|
||||
UserID string
|
||||
UserAccessKey string
|
||||
UserPassword string
|
||||
UserDisplayName string
|
||||
DBPath string
|
||||
Pickle string
|
||||
OpenAIKey string
|
||||
SystemPrompt string
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
OpenAI ConfigOpenAI `toml:"openai"`
|
||||
Bots []ConfigBot `toml:"bot"`
|
||||
}
|
||||
|
||||
type Bot struct {
|
||||
config Config
|
||||
openaiKey string
|
||||
config ConfigBot
|
||||
client *mautrix.Client
|
||||
cryptoHelper *cryptohelper.CryptoHelper
|
||||
characters []Character
|
||||
|
@ -35,10 +44,11 @@ type Bot struct {
|
|||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func New(cfg Config, logger *slog.Logger) *Bot {
|
||||
func New(openaiKey string, cfg ConfigBot, logger *slog.Logger) *Bot {
|
||||
return &Bot{
|
||||
config: cfg,
|
||||
logger: logger,
|
||||
openaiKey: openaiKey,
|
||||
config: cfg,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,7 +73,7 @@ func (m *Bot) Init() error {
|
|||
return err
|
||||
}
|
||||
m.client.Crypto = m.cryptoHelper
|
||||
m.gptClient = NewGPT(m.config.OpenAIKey)
|
||||
m.gptClient = NewGPT(m.openaiKey)
|
||||
m.conversations = make(Conversations, 0)
|
||||
m.AddEventHandler(m.InviteHandler())
|
||||
m.AddEventHandler(m.ResponseHandler())
|
||||
|
|
1
go.mod
1
go.mod
|
@ -12,6 +12,7 @@ require (
|
|||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/tidwall/gjson v1.14.4 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -1,3 +1,5 @@
|
|||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
|
||||
github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM=
|
||||
github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ=
|
||||
|
|
25
main.go
25
main.go
|
@ -5,39 +5,36 @@ import (
|
|||
"os/signal"
|
||||
|
||||
"ewintr.nl/matrix-bots/bot"
|
||||
"github.com/BurntSushi/toml"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"golang.org/x/exp/slog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
|
||||
botConfigs := []bot.Config{{
|
||||
Homeserver: getParam("MATRIX_HOMESERVER", "http://localhost"),
|
||||
UserID: getParam("MATRIX_USER_ID", "@bot:localhost"),
|
||||
UserPassword: getParam("MATRIX_PASSWORD", "secret"),
|
||||
UserAccessKey: getParam("MATRIX_ACCESS_KEY", "secret"),
|
||||
UserDisplayName: getParam("MATRIX_DISPLAY_NAME", "Bot"),
|
||||
DBPath: getParam("BOT_DB_PATH", "bot.db"),
|
||||
Pickle: getParam("BOT_PICKLE", "scrambled"),
|
||||
OpenAIKey: getParam("OPENAI_API_KEY", "no key"),
|
||||
SystemPrompt: "You are a chatbot that helps people by responding to their questions with short messages.",
|
||||
}}
|
||||
var config bot.Config
|
||||
if _, err := toml.DecodeFile(getParam("CONFIG_PATH", "conf.toml"), &config); err != nil {
|
||||
logger.Error(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
logger.Info("loaded config", slog.Int("bots", len(config.Bots)))
|
||||
|
||||
for _, bc := range botConfigs {
|
||||
b := bot.New(bc, logger)
|
||||
for _, bc := range config.Bots {
|
||||
b := bot.New(config.OpenAI.APIKey, bc, logger)
|
||||
if err := b.Init(); err != nil {
|
||||
logger.Error(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
go b.Run()
|
||||
logger.Info("started bot", slog.String("name", bc.UserDisplayName))
|
||||
}
|
||||
|
||||
done := make(chan os.Signal)
|
||||
signal.Notify(done, os.Interrupt)
|
||||
<-done
|
||||
|
||||
logger.Info("service stopped")
|
||||
}
|
||||
|
||||
func getParam(name, def string) string {
|
||||
|
|
Loading…
Reference in New Issue