Skip to content

Commit f219f3e

Browse files
author
Syfaro
committed
Merge pull request #33 from zhulik/master
Some small helpers added to Message and Bot, tests
2 parents a274ce7 + d40f7f9 commit f219f3e

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

bot_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ func TestSendWithNewAudio(t *testing.T) {
184184
msg.Title = "TEST"
185185
msg.Duration = 10
186186
msg.Performer = "TEST"
187+
msg.MimeType = "audio/mpeg"
188+
msg.FileSize = 688
187189
_, err := bot.Send(msg)
188190

189191
if err != nil {

configs.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ type BaseFile struct {
9393
File interface{}
9494
FileID string
9595
UseExisting bool
96+
MimeType string
97+
FileSize int
9698
}
9799

98100
// Params returns map[string]string representation of BaseFile
@@ -118,6 +120,14 @@ func (file BaseFile) Params() (map[string]string, error) {
118120
params["reply_markup"] = string(data)
119121
}
120122

123+
if len(file.MimeType) > 0 {
124+
params["mime_type"] = file.MimeType
125+
}
126+
127+
if file.FileSize > 0 {
128+
params["file_size"] = strconv.Itoa(file.FileSize)
129+
}
130+
121131
return params, nil
122132
}
123133

types.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,25 @@ func (m *Message) IsCommand() bool {
127127
return m.Text != "" && m.Text[0] == '/'
128128
}
129129

130-
// Command returns first word from message
130+
// Command if message is command returns first word from message(entire command)
131+
// otherwise returns empty string
131132
func (m *Message) Command() string {
132-
return strings.Split(m.Text, " ")[0]
133+
if m.IsCommand() {
134+
return strings.SplitN(m.Text, " ", 2)[0]
135+
}
136+
return ""
137+
}
138+
139+
// CommandArguments if message is command, returns all text after command, excluding the command itself
140+
// otherwise returns empty string
141+
func (m *Message) CommandArguments() string {
142+
if m.IsCommand() {
143+
split := strings.SplitN(m.Text, " ", 2)
144+
if len(split) == 2 {
145+
return strings.SplitN(m.Text, " ", 2)[1]
146+
}
147+
}
148+
return ""
133149
}
134150

135151
// PhotoSize contains information about photos, including ID and Width and Height.

types_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,75 @@ func TestMessageTime(t *testing.T) {
3131
}
3232
}
3333

34+
func TestMessageIsCommandWithCommand(t *testing.T) {
35+
message := tgbotapi.Message{Text: "/command"}
36+
37+
if message.IsCommand() != true {
38+
t.Fail()
39+
}
40+
}
41+
42+
func TestIsCommandWithText(t *testing.T) {
43+
message := tgbotapi.Message{Text: "some text"}
44+
45+
if message.IsCommand() != false {
46+
t.Fail()
47+
}
48+
}
49+
50+
func TestIsCommandWithEmptyText(t *testing.T) {
51+
message := tgbotapi.Message{Text: ""}
52+
53+
if message.IsCommand() != false {
54+
t.Fail()
55+
}
56+
}
57+
58+
func TestCommandWithCommand(t *testing.T) {
59+
message := tgbotapi.Message{Text: "/command"}
60+
61+
if message.Command() != "/command" {
62+
t.Fail()
63+
}
64+
}
65+
66+
func TestCommandWithEmptyText(t *testing.T) {
67+
message := tgbotapi.Message{Text: ""}
68+
69+
if message.Command() != "" {
70+
t.Fail()
71+
}
72+
}
73+
74+
func TestCommandWithNonCommand(t *testing.T) {
75+
message := tgbotapi.Message{Text: "test text"}
76+
77+
if message.Command() != "" {
78+
t.Fail()
79+
}
80+
}
81+
82+
func TestMessageCommandArgumentsWithArguments(t *testing.T) {
83+
message := tgbotapi.Message{Text: "/command with arguments"}
84+
if message.CommandArguments() != "with arguments" {
85+
t.Fail()
86+
}
87+
}
88+
89+
func TestMessageCommandArgumentsWithoutArguments(t *testing.T) {
90+
message := tgbotapi.Message{Text: "/command"}
91+
if message.CommandArguments() != "" {
92+
t.Fail()
93+
}
94+
}
95+
96+
func TestMessageCommandArgumentsForNonCommand(t *testing.T) {
97+
message := tgbotapi.Message{Text: "test text"}
98+
if message.CommandArguments() != "" {
99+
t.Fail()
100+
}
101+
}
102+
34103
func TestChatIsPrivate(t *testing.T) {
35104
chat := tgbotapi.Chat{ID: 10, Type: "private"}
36105

0 commit comments

Comments
 (0)