-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
73 lines (51 loc) · 1.52 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package tgbot
import (
"regexp"
"gopkg.in/devalecs/tgo.v1"
)
type Util struct{}
func (u *Util) MessageTextMatch(update *tgo.Update, pattern string) bool {
if update.Message == nil {
return false
}
match, _ := regexp.MatchString(pattern, update.Message.Text)
return match
}
func (u *Util) InlineQueryMatch(update *tgo.Update, pattern string) bool {
if update.InlineQuery == nil {
return false
}
match, _ := regexp.MatchString(pattern, update.InlineQuery.Query)
return match
}
func (u *Util) MessageTextMatchParams(update *tgo.Update, pattern string) (bool, []interface{}) {
var submatch []interface{}
if update.Message == nil {
return false, submatch
}
r := regexp.MustCompile(pattern)
if !r.MatchString(update.Message.Text) {
return false, submatch
}
match := r.FindStringSubmatch(update.Message.Text)
return true, u.stringToInterfaceSlice(match[1:])
}
func (u *Util) InlineQueryMatchParams(update *tgo.Update, pattern string) (bool, []interface{}) {
var submatch []interface{}
if update.InlineQuery == nil {
return false, submatch
}
r := regexp.MustCompile(pattern)
if !r.MatchString(update.InlineQuery.Query) {
return false, submatch
}
match := r.FindStringSubmatch(update.InlineQuery.Query)
return true, u.stringToInterfaceSlice(match[1:])
}
func (u *Util) stringToInterfaceSlice(stringSlices []string) []interface{} {
var interfaceSlices []interface{}
for _, stringSlice := range stringSlices {
interfaceSlices = append(interfaceSlices, interface{}(stringSlice))
}
return interfaceSlices
}