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