-
Notifications
You must be signed in to change notification settings - Fork 25
/
commands.go
104 lines (89 loc) · 2.85 KB
/
commands.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
package botsfw
import (
"fmt"
"net/url"
)
// CommandAction defines an action bot can perform in response to a command
type CommandAction func(whc WebhookContext) (m MessageFromBot, err error)
// CallbackAction defines a callback action bot can perform in response to a callback command
type CallbackAction func(whc WebhookContext, callbackUrl *url.URL) (m MessageFromBot, err error)
// CommandMatcher returns true if action is matched to user input
type CommandMatcher func(Command, WebhookContext) bool
// DefaultTitle key
const DefaultTitle = "" //
// ShortTitle key
const ShortTitle = "short_title"
//const LongTitle = "long_title"
// Command defines command metadata and action
type Command struct {
InputTypes []WebhookInputType // Instant match if != WebhookInputUnknown && == whc.InputTypes()
Icon string
Replies []Command
Code string
Title string
Titles map[string]string
ExactMatch string
Commands []string
Matcher CommandMatcher
Action CommandAction
CallbackAction CallbackAction
}
//goland:noinspection GoUnusedExportedFunction
func NewInlineQueryCommand(code string, action CommandAction) Command {
return Command{
Code: code,
InputTypes: []WebhookInputType{WebhookInputInlineQuery},
Action: action,
}
}
// NewCallbackCommand create a definition of a callback command
//
//goland:noinspection GoUnusedExportedFunction
func NewCallbackCommand(code string, action CallbackAction) Command {
return Command{
Code: code,
Commands: []string{"/" + code},
CallbackAction: action,
}
}
func (c Command) String() string {
return fmt.Sprintf("Command{Code: '%v', InputTypes: %v, Icon: '%v', Title: '%v', ExactMatch: '%v', len(Command): %v, len(Replies): %v}", c.Code, c.InputTypes, c.Icon, c.Title, c.ExactMatch, len(c.Commands), len(c.Replies))
}
// CommandTextNoTrans returns a title for a command (pre-translated)
func CommandTextNoTrans(title, icon string) string {
if title == "" && icon != "" {
return icon
} else if title != "" && icon == "" {
return title
} else if title != "" && icon != "" {
return title + " " + icon
}
return "<NO_TITLE_OR_ICON>"
}
// DefaultTitle returns a default title for a command in current Locale
func (c Command) DefaultTitle(whc WebhookContext) string {
return c.TitleByKey(DefaultTitle, whc)
}
// TitleByKey returns a short/long title for a command in current Locale
func (c Command) TitleByKey(key string, whc WebhookContext) string {
var title string
if key == DefaultTitle && c.Title != "" {
title = c.Title
} else if val, ok := c.Titles[key]; ok {
title = val
}
if c.Icon == "" {
if title == "" {
title = c.Code
} else {
title = whc.Translate(title)
}
} else {
if title == "" {
title = CommandTextNoTrans("", c.Icon)
} else {
title = whc.CommandText(title, c.Icon)
}
}
return title
}