gomatrix example
This commit is contained in:
parent
0021a0b55e
commit
363d72c560
|
@ -0,0 +1,10 @@
|
||||||
|
module ewintr.nl/matrix-bots
|
||||||
|
|
||||||
|
go 1.20
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0
|
||||||
|
github.com/sashabaranov/go-openai v1.9.4
|
||||||
|
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea
|
||||||
|
)
|
|
@ -0,0 +1,8 @@
|
||||||
|
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 h1:kHKxCOLcHH8r4Fzarl4+Y3K5hjothkVW5z7T1dUM11U=
|
||||||
|
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
|
github.com/sashabaranov/go-openai v1.9.4 h1:KanoCEoowAI45jVXlenMCckutSRr39qOmSi9MyPBfZM=
|
||||||
|
github.com/sashabaranov/go-openai v1.9.4/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
||||||
|
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea h1:vLCWI/yYrdEHyN2JzIzPO3aaQJHQdp89IZBA/+azVC4=
|
||||||
|
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
|
|
@ -0,0 +1,89 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/matrix-org/gomatrix"
|
||||||
|
"github.com/russross/blackfriday/v2"
|
||||||
|
"github.com/sashabaranov/go-openai"
|
||||||
|
"golang.org/x/exp/slog"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
MatrixHomeserver, ok := os.LookupEnv("MATRIX_HOMESERVER")
|
||||||
|
if !ok {
|
||||||
|
slog.Error("MATRIX_HOME_SERVER is not set")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
MatrixUserID, ok := os.LookupEnv("MATRIX_USER_ID")
|
||||||
|
if !ok {
|
||||||
|
slog.Error("MATRIX_USER_ID is not set")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
MatrixAccessKey, ok := os.LookupEnv("MATRIX_ACCESS_KEY")
|
||||||
|
if !ok {
|
||||||
|
slog.Error("MATRIX_ACCESS_KEY is not set")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
OpenaiAPIKey, ok := os.LookupEnv("OPENAI_API_KEY")
|
||||||
|
if !ok {
|
||||||
|
slog.Error("OPENAI_API_KEY is not set")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new OpenAI client
|
||||||
|
openaiClient := openai.NewClient(OpenaiAPIKey)
|
||||||
|
|
||||||
|
matrixClient, _ := gomatrix.NewClient(MatrixHomeserver, MatrixUserID, MatrixAccessKey)
|
||||||
|
|
||||||
|
syncer := matrixClient.Syncer.(*gomatrix.DefaultSyncer)
|
||||||
|
syncer.OnEventType("m.room.member", func(ev *gomatrix.Event) {
|
||||||
|
if ev.Content["membership"] == "invite" && *ev.StateKey == MatrixUserID {
|
||||||
|
_, err := matrixClient.JoinRoom(ev.RoomID, "", nil)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failed to join room:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
syncer.OnEventType("m.room.message", func(ev *gomatrix.Event) {
|
||||||
|
if ev.Sender != MatrixUserID {
|
||||||
|
msgBody, _ := ev.Body()
|
||||||
|
|
||||||
|
// Generate a message with OpenAI API
|
||||||
|
openAiResp, err := openaiClient.CreateChatCompletion(
|
||||||
|
context.Background(),
|
||||||
|
openai.ChatCompletionRequest{
|
||||||
|
Model: openai.GPT4,
|
||||||
|
Messages: []openai.ChatCompletionMessage{
|
||||||
|
{
|
||||||
|
Role: openai.ChatMessageRoleSystem,
|
||||||
|
Content: "You are a chatbot that helps people by responding to their questions with short messages.",
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
Role: openai.ChatMessageRoleUser,
|
||||||
|
Content: msgBody,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("OpenAI API returned with ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the OpenAI response back to the chat
|
||||||
|
responseText := openAiResp.Choices[len(openAiResp.Choices)-1].Message.Content
|
||||||
|
formattedResponse := blackfriday.Run([]byte(responseText))
|
||||||
|
_, _ = matrixClient.SendFormattedText(ev.RoomID, responseText, string(formattedResponse))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
for {
|
||||||
|
if err := matrixClient.Sync(); err != nil {
|
||||||
|
fmt.Println("Sync() returned with ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue