initial commit

This commit is contained in:
marko
2024-10-03 15:52:06 +02:00
parent b9656c91bb
commit d928a681f9
8 changed files with 328 additions and 0 deletions

114
chatbot/chatbot.go Normal file
View File

@@ -0,0 +1,114 @@
package chatbot
import (
"fmt"
"log"
"regexp"
"strings"
"time"
"github.com/xmppo/go-xmpp"
"gopkg.in/ini.v1"
"gitea.dmz.rs/bauljamic123arlijam/neko-u-krovu-bot/ping"
)
type NekoUKrovuBot struct {
cl *xmpp.Client
host string
user string
mcuJid string
nick string
}
func NewNekoUKrovuBot() (*NekoUKrovuBot, 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 &NekoUKrovuBot{}, err
}
n, err := cl.JoinMUCNoHistory(mcuJid, nick)
if err != nil {
return &NekoUKrovuBot{}, err
}
_ = n
return &NekoUKrovuBot{cl: cl,
host: host,
user: user,
mcuJid: mcuJid,
nick: nick}, nil
}
func (nkbot *NekoUKrovuBot) Listen() {
for {
msg, err := nkbot.cl.Recv()
if err != nil {
log.Print(err)
continue
}
switch m := msg.(type) {
case xmpp.Chat:
nkbot.handleChat(&m)
default:
continue
}
}
}
func (nkbot *NekoUKrovuBot) handleChat(ch *xmpp.Chat) {
src := ch.Remote
txt := ch.Text
if src == "sdsads" {
return
}
if nkbot.checkForJelNekoUKrovu(txt) {
n := ping.Run()
nkbot.answer(fmt.Sprintf("%v uredjaja povezano", n))
}
}
func (nkbot *NekoUKrovuBot) checkForJelNekoUKrovu(txt string) bool {
normalizedText := strings.ToLower(txt)
pattern := `(?i)^(?:\S+\s+){2,}\b(jel|el|ima li|ima|neko|koga|nekoga|je l)?\s*(neko|koga|nekoga)?\s*(u)?\s*(krovu|krov|vkro)\b`
matched, err := regexp.MatchString(pattern, normalizedText)
if err != nil {
fmt.Println("Error compiling regex:", err)
return false
}
return matched
}
func (nkbot *NekoUKrovuBot) answer(ans string) {
chat := xmpp.Chat{
Remote: "chatbottest@conference.dmz.rs",
Type: "groupchat",
Text: ans,
Stamp: time.Now(),
}
n, err := nkbot.cl.Send(chat)
if err != nil {
log.Print(err)
}
_ = n
}

96
chatbot/chatbot_test.go Normal file
View File

@@ -0,0 +1,96 @@
package chatbot
import (
"testing"
)
func TestNekoUKrovuBot_checkForJelNekoUKrovu(t *testing.T) {
type args struct {
txt string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "matches 'jel neko u krovu'",
args: args{
txt: "jel neko u krovu",
},
want: true,
},
{
name: "matches 'ima li koga na krovu'",
args: args{
txt: "ima li koga na krovu",
},
want: true,
},
{
name: "does not match 'nema nikoga'",
args: args{
txt: "nema nikoga",
},
want: false,
},
{
name: "matches 'koga ima na krovu'",
args: args{
txt: "koga ima na krovu",
},
want: true,
},
{
name: "matches 'neko u krov'",
args: args{
txt: "neko u krov",
},
want: true,
},
{
name: "case insensitive match 'EL NEKO krov'",
args: args{
txt: "EL NEKO krov",
},
want: true,
},
{
name: "case insensitive match 'jel neko jebeno u krovu'",
args: args{
txt: "jel neko jebeno u krovu",
},
want: true,
},
{
name: "case insensitive match 'buraz jel neko jebeno u krovu'",
args: args{
txt: "buraz jel neko jebeno u krovu",
},
want: true,
},
{
name: "case insensitive match 'neko u krovu?'",
args: args{
txt: "neko u krovu?",
},
want: true,
},
{
name: "case insensitive match 'u krovu?'",
args: args{
txt: "u krovu?",
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nkbot := &NekoUKrovuBot{
}
if got := nkbot.checkForJelNekoUKrovu(tt.args.txt); got != tt.want {
t.Errorf("NekoUKrovuBot.checkForJelNekoUKrovu() = %v, want %v", got, tt.want)
}
})
}
}