aboutsummaryrefslogtreecommitdiff
path: root/api/telegram/bot/command.go
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2020-04-10 14:27:04 +0700
committerShulhan <m.shulhan@gmail.com>2020-04-11 03:50:26 +0700
commitdd21606afcd3bef6243d37592ec6ee47f1d03898 (patch)
tree3142930cb6364bd51daa2ff0762823b2d6196558 /api/telegram/bot/command.go
parent33b2a03dfbd90bf53e09fa2210aff8a84bf81fd8 (diff)
downloadpakakeh.go-dd21606afcd3bef6243d37592ec6ee47f1d03898.tar.xz
api/telegram/bot: Go package for Telegram API Bot
Diffstat (limited to 'api/telegram/bot/command.go')
-rw-r--r--api/telegram/bot/command.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/api/telegram/bot/command.go b/api/telegram/bot/command.go
new file mode 100644
index 00000000..6d80343a
--- /dev/null
+++ b/api/telegram/bot/command.go
@@ -0,0 +1,71 @@
+// Copyright 2020, Shulhan <m.shulhan@gmail.com>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package bot
+
+import (
+ "fmt"
+)
+
+//
+// Command represents a bot command.
+//
+type Command struct {
+ // Text of the command, 1-32 characters. Can contain only lowercase
+ // English letters, digits and underscores.
+ Command string `json:"command"`
+
+ // Description of the command, 3-256 characters.
+ Description string `json:"description"`
+
+ // Function that will be called when Bot receive the command.
+ // Handler can read command and its arguments through Message.Command
+ // and Message.CommandArgs.
+ Handler UpdateHandler `json:"-"`
+}
+
+//
+// validate will return an error if command is not valid.
+//
+func (cmd *Command) validate() error {
+ if len(cmd.Command) == 0 || len(cmd.Command) > 32 {
+ return errCommandLength(cmd.Command)
+ }
+ for x := 0; x < len(cmd.Command); x++ {
+ b := cmd.Command[x]
+ if b >= 'a' && b <= 'z' {
+ continue
+ }
+ if b >= '0' && b <= '9' {
+ continue
+ }
+ if b == '_' {
+ continue
+ }
+ return errCommandValue(cmd.Command)
+ }
+ if len(cmd.Description) < 3 || len(cmd.Description) > 256 {
+ return errDescLength(cmd.Command)
+ }
+ if cmd.Handler == nil {
+ return errHandlerNil(cmd.Command)
+ }
+ return nil
+}
+
+func errCommandLength(cmd string) error {
+ return fmt.Errorf("%q: the Command length must be between 1-32 characters", cmd)
+}
+
+func errCommandValue(cmd string) error {
+ return fmt.Errorf("%q: command can contain only lowercase English letter, digits, and underscores", cmd)
+}
+
+func errDescLength(cmd string) error {
+ return fmt.Errorf("%q: the Description length must be between 3-256 characters", cmd)
+}
+
+func errHandlerNil(cmd string) error {
+ return fmt.Errorf("%q: the Command's Handler is not set", cmd)
+}