logmania/bot/main.go

55 lines
1.2 KiB
Go
Raw Normal View History

package bot
import (
"fmt"
"strings"
"dev.sum7.eu/genofire/logmania/database"
)
type Bot struct {
2018-04-15 01:52:08 +02:00
db *database.DB
commandsMap map[string]commandFunc
commands []string
}
func NewBot(db *database.DB) *Bot {
b := &Bot{
db: db,
}
2018-04-15 01:52:08 +02:00
b.commandsMap = map[string]commandFunc{
"help": b.help,
2018-04-15 01:52:08 +02:00
"send-add": b.addSend,
"send-list": b.listSend,
"send-del": b.delSend,
"hostname-set": b.addHostname,
"hostname-list": b.listHostname,
2018-04-15 01:52:08 +02:00
"hostname-del": b.delHostname,
"filter-set": b.setMaxfilter,
"filter-list": b.listMaxfilter,
"regex-add": b.addRegex,
"regex-list": b.listRegex,
2018-04-15 01:52:08 +02:00
"regex-del": b.delRegex,
2018-05-18 11:28:01 +02:00
"replace-add": b.addRegexReplace,
"replace-list": b.listRegexReplace,
"replace-del": b.delRegexReplace,
2018-04-15 01:52:08 +02:00
}
2018-04-18 14:32:28 +02:00
for k := range b.commandsMap {
2018-04-15 01:52:08 +02:00
b.commands = append(b.commands, k)
}
return b
}
func (b *Bot) Handle(answer func(string), from, msg string) {
msgParts := strings.Split(msg, " ")
2017-08-11 19:59:19 +02:00
if len(msgParts[0]) <= 0 || msgParts[0][0] != '!' {
return
}
cmdName := msgParts[0][1:]
2018-04-15 01:52:08 +02:00
if cmd, ok := b.commandsMap[cmdName]; ok {
cmd(answer, from, msgParts[1:])
} else {
answer(fmt.Sprintf("not found command: !%s", cmdName))
}
}