aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2020-04-11 22:53:52 +0700
committerShulhan <m.shulhan@gmail.com>2020-04-11 22:53:52 +0700
commit7ae26f68d684d1400d56bd3091eb8642adecabfa (patch)
tree6ca83f3c55cc2649ec122b7c1341723c66b922e0 /api
parent5378abd1e1f45288639bc45f692589a07af49e0b (diff)
downloadpakakeh.go-7ae26f68d684d1400d56bd3091eb8642adecabfa.tar.xz
telegram/bot: fix parsing command and arguments on parseCommandArgs
Diffstat (limited to 'api')
-rw-r--r--api/telegram/bot/message.go8
-rw-r--r--api/telegram/bot/message_test.go47
2 files changed, 52 insertions, 3 deletions
diff --git a/api/telegram/bot/message.go b/api/telegram/bot/message.go
index 85bfccc9..69e406d7 100644
--- a/api/telegram/bot/message.go
+++ b/api/telegram/bot/message.go
@@ -183,9 +183,11 @@ func (msg *Message) parseCommandArgs() bool {
return false
}
- msg.Command = msg.Text[cmdEntity.Offset+1 : cmdEntity.Length]
- start := cmdEntity.Offset + cmdEntity.Length
- msg.CommandArgs = strings.TrimSpace(msg.Text[start:])
+ start := cmdEntity.Offset
+ end := start + cmdEntity.Length
+
+ msg.Command = strings.TrimPrefix(msg.Text[start:end], "/")
+ msg.CommandArgs = strings.TrimSpace(msg.Text[end:])
return true
}
diff --git a/api/telegram/bot/message_test.go b/api/telegram/bot/message_test.go
new file mode 100644
index 00000000..ee0dceb8
--- /dev/null
+++ b/api/telegram/bot/message_test.go
@@ -0,0 +1,47 @@
+// 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 (
+ "testing"
+
+ "github.com/shuLhan/share/lib/test"
+)
+
+func TestMessage_parseCommandArgs(t *testing.T) {
+ cases := []struct {
+ msg Message
+ expCommand string
+ expArgs string
+ }{{
+ msg: Message{
+ Text: "Definisi /analisis",
+ Entities: []MessageEntity{{
+ Type: EntityTypeBotCommand,
+ Offset: 9,
+ Length: 9,
+ }},
+ },
+ expCommand: "analisis",
+ }, {
+ msg: Message{
+ Text: "/definisi analisis",
+ Entities: []MessageEntity{{
+ Type: EntityTypeBotCommand,
+ Offset: 0,
+ Length: 9,
+ }},
+ },
+ expCommand: "definisi",
+ expArgs: "analisis",
+ }}
+
+ for _, c := range cases {
+ c.msg.parseCommandArgs()
+
+ test.Assert(t, "Command", c.expCommand, c.msg.Command, true)
+ test.Assert(t, "Command", c.expArgs, c.msg.CommandArgs, true)
+ }
+}