matrix-gptzoo/main.go

56 lines
1.3 KiB
Go
Raw Normal View History

2023-05-12 13:59:20 +02:00
package main
import (
2023-05-16 20:07:05 +02:00
"os"
"os/signal"
"ewintr.nl/matrix-bots/matrix"
2023-05-15 12:28:12 +02:00
_ "github.com/mattn/go-sqlite3"
2023-05-12 13:59:20 +02:00
"github.com/sashabaranov/go-openai"
"golang.org/x/exp/slog"
)
func main() {
2023-05-15 12:28:12 +02:00
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
2023-05-16 20:07:05 +02:00
matrixClient := matrix.New(matrix.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"),
DBPath: getParam("BOT_DB_PATH", "bot.db"),
Pickle: getParam("BOT_PICKLE", "scrambled"),
})
2023-05-12 13:59:20 +02:00
OpenaiAPIKey, ok := os.LookupEnv("OPENAI_API_KEY")
if !ok {
2023-05-15 12:28:12 +02:00
logger.Error("OPENAI_API_KEY is not set")
2023-05-12 13:59:20 +02:00
os.Exit(1)
}
// Create new OpenAI client
openaiClient := openai.NewClient(OpenaiAPIKey)
2023-05-16 20:07:05 +02:00
if err := matrixClient.Init(); err != nil {
logger.Error(err.Error())
os.Exit(1)
2023-05-15 12:28:12 +02:00
}
2023-05-16 20:07:05 +02:00
go matrixClient.Run()
2023-05-12 15:07:16 +02:00
2023-05-16 20:07:05 +02:00
matrixClient.AddEventHandler(matrixClient.InviteHandler())
matrixClient.AddEventHandler(matrixClient.RespondHandler(openaiClient))
2023-05-12 15:07:16 +02:00
done := make(chan os.Signal)
signal.Notify(done, os.Interrupt)
<-done
2023-05-16 20:07:05 +02:00
}
func getParam(name, def string) string {
val, ok := os.LookupEnv(name)
if !ok {
return def
2023-05-15 12:28:12 +02:00
}
2023-05-16 20:07:05 +02:00
return val
2023-05-12 13:59:20 +02:00
}