init commit with sending hello world xmpp message

This commit is contained in:
Txrpe 2024-11-18 00:27:56 +01:00
commit c868f76681
5 changed files with 123 additions and 0 deletions

24
.gitignore vendored Normal file
View File

@ -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

64
chatbot/chatbot.go Normal file
View File

@ -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
}

6
config.ini.example Normal file
View File

@ -0,0 +1,6 @@
[credentials]
JID = botusername@example.org
PASSWORD = bot_password
NICK = chatbot
ROOM = room_jid@example.org
HOST = example.org

15
go.mod Normal file
View File

@ -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
)

14
main.go Normal file
View File

@ -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")
}