commit c868f76681356d25659eafdbc77f41a59ee138b6 Author: Txrpe Date: Mon Nov 18 00:27:56 2024 +0100 init commit with sending hello world xmpp message diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2bb745e --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# ---> Go +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +config.ini diff --git a/chatbot/chatbot.go b/chatbot/chatbot.go new file mode 100644 index 0000000..6f27df9 --- /dev/null +++ b/chatbot/chatbot.go @@ -0,0 +1,64 @@ +package chatbot + +import ( + "log" + "time" + + "github.com/xmppo/go-xmpp" + "gopkg.in/ini.v1" +) + +type EventBot struct { + cl *xmpp.Client + host string + user string + mcuJid string + nick string +} + +func NewEventBot() (*EventBot, error) { + cfg, err := ini.Load("config.ini") + if err != nil { + log.Fatalf("Failed to load INI file: %v", err) + } + + host := cfg.Section("credentials").Key("HOST").String() + user := cfg.Section("credentials").Key("JID").String() + passwd := cfg.Section("credentials").Key("PASSWORD").String() + mcuJid := cfg.Section("credentials").Key("ROOM").String() + nick := cfg.Section("credentials").Key("NICK").String() + + cl, err := xmpp.NewClientNoTLS(host, user, passwd, false) + if err != nil { + return &EventBot{}, err + } + + n, err := cl.JoinMUCNoHistory(mcuJid, nick) + if err != nil { + return &EventBot{}, err + } + + _ = n + + return &EventBot{cl: cl, + host: host, + user: user, + mcuJid: mcuJid, + nick: nick}, nil +} + +func (nkbot *EventBot) SendMessage(ans string) { + chat := xmpp.Chat{ + Remote: "dmztest@chat.hookipa.net", + Type: "groupchat", + Text: ans, + Stamp: time.Now(), + } + + n, err := nkbot.cl.Send(chat) + if err != nil { + log.Print(err) + } + + _ = n +} diff --git a/config.ini.example b/config.ini.example new file mode 100644 index 0000000..d8a5488 --- /dev/null +++ b/config.ini.example @@ -0,0 +1,6 @@ +[credentials] +JID = botusername@example.org +PASSWORD = bot_password +NICK = chatbot +ROOM = room_jid@example.org +HOST = example.org diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c85ee7b --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module gitea.dmz.rs/txrpe/event-bot + +go 1.23.1 + +require github.com/xmppo/go-xmpp v0.2.6 + +require github.com/apognu/gocal v0.9.1 + +require gopkg.in/ini.v1 v1.67.0 + +require ( + github.com/ChannelMeter/iso8601duration v0.0.0-20150204201828-8da3af7a2a61 // indirect + golang.org/x/crypto v0.29.0 // indirect + golang.org/x/net v0.31.0 // indirect +) diff --git a/main.go b/main.go new file mode 100644 index 0000000..da32cd7 --- /dev/null +++ b/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "gitea.dmz.rs/txrpe/event-bot/chatbot" + "log" +) + +func main() { + nkbot, err := chatbot.NewEventBot() + if err != nil { + log.Fatal(err) + } + nkbot.SendMessage("Cao svete") +}