Merge pull request #33 from zhulik/master

Some small helpers added to Message and Bot, tests
bot-api-6.1
Syfaro 2015-12-16 07:56:30 -06:00
commit f219f3e9db
4 changed files with 99 additions and 2 deletions

View File

@ -184,6 +184,8 @@ func TestSendWithNewAudio(t *testing.T) {
msg.Title = "TEST" msg.Title = "TEST"
msg.Duration = 10 msg.Duration = 10
msg.Performer = "TEST" msg.Performer = "TEST"
msg.MimeType = "audio/mpeg"
msg.FileSize = 688
_, err := bot.Send(msg) _, err := bot.Send(msg)
if err != nil { if err != nil {

View File

@ -93,6 +93,8 @@ type BaseFile struct {
File interface{} File interface{}
FileID string FileID string
UseExisting bool UseExisting bool
MimeType string
FileSize int
} }
// Params returns map[string]string representation of BaseFile // Params returns map[string]string representation of BaseFile
@ -118,6 +120,14 @@ func (file BaseFile) Params() (map[string]string, error) {
params["reply_markup"] = string(data) params["reply_markup"] = string(data)
} }
if len(file.MimeType) > 0 {
params["mime_type"] = file.MimeType
}
if file.FileSize > 0 {
params["file_size"] = strconv.Itoa(file.FileSize)
}
return params, nil return params, nil
} }

View File

@ -127,9 +127,25 @@ func (m *Message) IsCommand() bool {
return m.Text != "" && m.Text[0] == '/' return m.Text != "" && m.Text[0] == '/'
} }
// Command returns first word from message // Command if message is command returns first word from message(entire command)
// otherwise returns empty string
func (m *Message) Command() string { func (m *Message) Command() string {
return strings.Split(m.Text, " ")[0] if m.IsCommand() {
return strings.SplitN(m.Text, " ", 2)[0]
}
return ""
}
// CommandArguments if message is command, returns all text after command, excluding the command itself
// otherwise returns empty string
func (m *Message) CommandArguments() string {
if m.IsCommand() {
split := strings.SplitN(m.Text, " ", 2)
if len(split) == 2 {
return strings.SplitN(m.Text, " ", 2)[1]
}
}
return ""
} }
// PhotoSize contains information about photos, including ID and Width and Height. // PhotoSize contains information about photos, including ID and Width and Height.

View File

@ -31,6 +31,75 @@ func TestMessageTime(t *testing.T) {
} }
} }
func TestMessageIsCommandWithCommand(t *testing.T) {
message := tgbotapi.Message{Text: "/command"}
if message.IsCommand() != true {
t.Fail()
}
}
func TestIsCommandWithText(t *testing.T) {
message := tgbotapi.Message{Text: "some text"}
if message.IsCommand() != false {
t.Fail()
}
}
func TestIsCommandWithEmptyText(t *testing.T) {
message := tgbotapi.Message{Text: ""}
if message.IsCommand() != false {
t.Fail()
}
}
func TestCommandWithCommand(t *testing.T) {
message := tgbotapi.Message{Text: "/command"}
if message.Command() != "/command" {
t.Fail()
}
}
func TestCommandWithEmptyText(t *testing.T) {
message := tgbotapi.Message{Text: ""}
if message.Command() != "" {
t.Fail()
}
}
func TestCommandWithNonCommand(t *testing.T) {
message := tgbotapi.Message{Text: "test text"}
if message.Command() != "" {
t.Fail()
}
}
func TestMessageCommandArgumentsWithArguments(t *testing.T) {
message := tgbotapi.Message{Text: "/command with arguments"}
if message.CommandArguments() != "with arguments" {
t.Fail()
}
}
func TestMessageCommandArgumentsWithoutArguments(t *testing.T) {
message := tgbotapi.Message{Text: "/command"}
if message.CommandArguments() != "" {
t.Fail()
}
}
func TestMessageCommandArgumentsForNonCommand(t *testing.T) {
message := tgbotapi.Message{Text: "test text"}
if message.CommandArguments() != "" {
t.Fail()
}
}
func TestChatIsPrivate(t *testing.T) { func TestChatIsPrivate(t *testing.T) {
chat := tgbotapi.Chat{ID: 10, Type: "private"} chat := tgbotapi.Chat{ID: 10, Type: "private"}