initial prototype, only responds with tokens
This commit is contained in:
parent
d2fd718f22
commit
0ab510adef
5 changed files with 128 additions and 0 deletions
7
go.mod
Normal file
7
go.mod
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
module mechanus.net/pgbot
|
||||||
|
|
||||||
|
go 1.23.3
|
||||||
|
|
||||||
|
require gopkg.in/telebot.v4 v4.0.0-beta.4
|
||||||
|
|
||||||
|
require gopkg.in/yaml.v3 v3.0.1 // indirect
|
4
go.sum
Normal file
4
go.sum
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
gopkg.in/telebot.v4 v4.0.0-beta.4 h1:9O3elrJ1GYJhNBpi7WDlBOaM/KQPvr5xpFPUEbA+dpk=
|
||||||
|
gopkg.in/telebot.v4 v4.0.0-beta.4/go.mod h1:jhcQjM/176jZm/s9Up/MzV5VFGPjyI8oiJhWvCMxayI=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
31
main.go
Normal file
31
main.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"mechanus.net/pgobot/matcher"
|
||||||
|
"mechanus.net/pgobot/responder"
|
||||||
|
|
||||||
|
"gopkg.in/telebot.v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pref := telebot.Settings{
|
||||||
|
Token: os.Getenv("TOKEN"),
|
||||||
|
Poller: &telebot.LongPoller{Timeout: 10 * time.Second},
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenMatcher := matcher.InitMatcher("tokens.yml")
|
||||||
|
|
||||||
|
bot, err := telebot.NewBot(pref)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
responder.InitResponder(bot, tokenMatcher)
|
||||||
|
|
||||||
|
bot.Start()
|
||||||
|
}
|
70
matcher/matcher.go
Normal file
70
matcher/matcher.go
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
package matcher
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Matcher struct {
|
||||||
|
tokens []TokenCompiled
|
||||||
|
}
|
||||||
|
|
||||||
|
type TokenCompiled struct {
|
||||||
|
token string
|
||||||
|
regex []*regexp.Regexp
|
||||||
|
}
|
||||||
|
|
||||||
|
func createToken(token string, regexList []string) TokenCompiled {
|
||||||
|
result := new(TokenCompiled)
|
||||||
|
result.token = token
|
||||||
|
result.regex = make([]*regexp.Regexp, len(regexList))
|
||||||
|
for i, regex := range regexList {
|
||||||
|
result.regex[i] = regexp.MustCompile(regex)
|
||||||
|
}
|
||||||
|
return *result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t TokenCompiled) match(input string) (bool, string) {
|
||||||
|
for _, regex := range t.regex {
|
||||||
|
if regex.MatchString(input) {
|
||||||
|
return true, t.token
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Matcher) Tokenize(input string) []string {
|
||||||
|
result := make([]string, 0, 10)
|
||||||
|
for _, token := range m.tokens {
|
||||||
|
ok, matchedToken := token.match(input)
|
||||||
|
if ok {
|
||||||
|
result = append(result, matchedToken)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitMatcher(config string) *Matcher {
|
||||||
|
configData, err := os.ReadFile(config)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
var configMap map[string][]string
|
||||||
|
|
||||||
|
err = yaml.Unmarshal(configData, &configMap)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tokens := make([]TokenCompiled, 0, len(configMap))
|
||||||
|
|
||||||
|
for k, v := range configMap {
|
||||||
|
tokensCompiled := createToken(k, v)
|
||||||
|
tokens = append(tokens, tokensCompiled)
|
||||||
|
}
|
||||||
|
result := new(Matcher)
|
||||||
|
result.tokens = tokens
|
||||||
|
return result
|
||||||
|
}
|
16
responder/responder.go
Normal file
16
responder/responder.go
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
package responder
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"mechanus.net/pgobot/matcher"
|
||||||
|
|
||||||
|
"gopkg.in/telebot.v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitResponder(bot *telebot.Bot, tokenMatcher *matcher.Matcher) {
|
||||||
|
bot.Handle(telebot.OnText, func(c telebot.Context) error {
|
||||||
|
tokens := tokenMatcher.Tokenize(c.Message().Text)
|
||||||
|
return c.Send(strings.Join(tokens, ","))
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in a new issue