65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
|
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
|
||
|
}
|