-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintents.go
124 lines (107 loc) · 3 KB
/
intents.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package groupbot
import (
"log"
"regexp"
"strings"
"github.com/grokify/mogo/encoding/jsonutil"
hum "github.com/grokify/mogo/net/http/httputilmore"
"github.com/grokify/mogo/type/stringsutil"
)
/*
type EventResponse struct {
StatusCode int `json:"statusCode,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Message string `json:"message,omitempty"`
}
func (er *EventResponse) ToJson() []byte {
if len(er.Message) == 0 {
er.Message = ""
}
msgJson, err := json.Marshal(er)
if err != nil {
return []byte(`{"statusCode":500,"message":"Cannot Marshal to JSON"}`)
}
return msgJson
}
*/
type IntentRouter struct {
Intents []Intent
}
func NewIntentRouter() IntentRouter {
return IntentRouter{Intents: []Intent{}}
}
func (ir *IntentRouter) ProcessRequest(bot *Groupbot, glipPostEventInfo *GlipPostEventInfo) (*hum.ResponseInfo, error) {
tryCmdsNotMatched := []string{}
intentResponses := []*hum.ResponseInfo{}
regexps := []*regexp.Regexp{
regexp.MustCompile(`[^a-zA-Z0-9\-]+`),
regexp.MustCompile(`\s+`)}
tryCmdsLc := stringsutil.SliceCondenseRegexps(
glipPostEventInfo.TryCommandsLc,
regexps,
" ",
)
for _, tryCmdLc := range tryCmdsLc {
matched := false
for _, intent := range ir.Intents {
if intent.Type == MatchStringLowerCase {
for _, try := range intent.Strings {
if try == tryCmdLc {
matched = true
evtResp, err := intent.HandleIntent(bot, glipPostEventInfo)
if err == nil {
intentResponses = append(intentResponses, evtResp)
}
}
}
}
}
if !matched {
tryCmdsNotMatched = append(tryCmdsNotMatched, tryCmdLc)
}
}
tryCmdsNotMatched = stringsutil.SliceCondenseRegexps(
tryCmdsNotMatched,
regexps,
" ",
)
if len(tryCmdsNotMatched) > 0 {
log.Println("TRY_CMDS_NOT_MATCHED " + jsonutil.MustMarshalString(tryCmdsNotMatched, true))
glipPostEventInfo.TryCommandsLc = tryCmdsNotMatched
for _, intent := range ir.Intents {
if intent.Type == MatchAny {
return intent.HandleIntent(bot, glipPostEventInfo)
}
}
}
return &hum.ResponseInfo{}, nil
}
func (ir *IntentRouter) ProcessRequestSingle(bot *Groupbot, textNoBotMention string, glipPostEventInfo *GlipPostEventInfo) (*hum.ResponseInfo, error) {
textNoBotMention = strings.TrimSpace(textNoBotMention)
textNoBotMentionLc := strings.ToLower(textNoBotMention)
for _, intent := range ir.Intents {
if intent.Type == MatchStringLowerCase {
for _, try := range intent.Strings {
if try == textNoBotMentionLc {
return intent.HandleIntent(bot, glipPostEventInfo)
}
}
} else if intent.Type == MatchAny {
return intent.HandleIntent(bot, glipPostEventInfo)
}
}
return &hum.ResponseInfo{}, nil
}
type IntentType int
const (
MatchString IntentType = iota
MatchStringLowerCase
MatchRegexp
MatchAny
)
type Intent struct {
Type IntentType
Strings []string
Regexps []*regexp.Regexp
HandleIntent func(bot *Groupbot, glipPostEventInfo *GlipPostEventInfo) (*hum.ResponseInfo, error)
}