summaryrefslogtreecommitdiffstats
path: root/internal/bot/command.go
diff options
context:
space:
mode:
authorMurilo Ijanc <[email protected]>2026-07-26 15:18:21 -0300
committerMurilo Ijanc <[email protected]>2026-07-26 15:18:21 -0300
commit3ca1d2c4370e8e79aba3940d5c8fe115960b75a8 (patch)
treeaa964675c588b80e4f81182d34130ba2d7d984e1 /internal/bot/command.go
downloadtelegram-507b9f5009ff8720e0ea9a43c2f6bf888fd53ddb.tar.gz
Initial importv0.1.0
Diffstat (limited to 'internal/bot/command.go')
-rw-r--r--internal/bot/command.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/internal/bot/command.go b/internal/bot/command.go
new file mode 100644
index 0000000..9e13060
--- /dev/null
+++ b/internal/bot/command.go
@@ -0,0 +1,54 @@
+package bot
+
+import "strings"
+
+// Command is a parsed "/name@target argument" line.
+type Command struct {
+ // Name is the command without the leading slash, lowercased.
+ Name string
+
+ // Target is the bot username the command was addressed to, without the
+ // "@", or "" when the user did not address anyone.
+ Target string
+
+ // Argument is everything after the command, trimmed.
+ Argument string
+}
+
+// ParseCommand splits a message that starts with a slash.
+//
+// The "@username" suffix is not optional to handle. Telegram appends it
+// automatically whenever more than one bot is in a chat, so in exactly the busy
+// groups where the bot is most visible, every command arrives as
+// "/cep@thebotname 01001000". A bot that only matches the bare form looks broken
+// there.
+//
+// ok is false when the text is not a command at all.
+func ParseCommand(text string) (Command, bool) {
+ trimmed := strings.TrimSpace(text)
+ if !strings.HasPrefix(trimmed, "/") || len(trimmed) == 1 {
+ return Command{}, false
+ }
+
+ head, argument, _ := strings.Cut(trimmed[1:], " ")
+ name, target, _ := strings.Cut(head, "@")
+ if name == "" {
+ return Command{}, false
+ }
+
+ return Command{
+ Name: strings.ToLower(name),
+ Target: target,
+ Argument: strings.TrimSpace(argument),
+ }, true
+}
+
+// AddressedTo reports whether the command is for the bot with this username.
+//
+// An unaddressed command counts as addressed: in a private chat nobody types
+// the suffix, and in a group with a single bot Telegram does not add it either.
+// A command addressed to some other bot is not ours, and answering it would put
+// two replies under one message.
+func (c Command) AddressedTo(username string) bool {
+ return c.Target == "" || strings.EqualFold(c.Target, username)
+}