-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
31 lines (27 loc) · 758 Bytes
/
Copy pathcommand.go
File metadata and controls
31 lines (27 loc) · 758 Bytes
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
package tgbot
import "strings"
// ParseCommand parses Telegram-style command text like "/start arg".
func ParseCommand(text string) (command string, args string, ok bool) {
trimmed := strings.TrimSpace(text)
if !strings.HasPrefix(trimmed, "/") {
return "", "", false
}
token := trimmed
args = ""
if split := strings.IndexAny(trimmed, " \t\n\r"); split >= 0 {
token = trimmed[:split]
args = strings.TrimSpace(trimmed[split+1:])
}
token = strings.TrimPrefix(strings.TrimSpace(token), "/")
if token == "" {
return "", "", false
}
if mention := strings.Index(token, "@"); mention >= 0 {
token = token[:mention]
}
token = strings.ToLower(strings.TrimSpace(token))
if token == "" {
return "", "", false
}
return token, args, true
}