From eb6b784a5a27882aa5c911db45bfacecc31dad98 Mon Sep 17 00:00:00 2001 From: Gleb Sinyavsky Date: Sun, 13 Dec 2015 20:00:20 +0300 Subject: [PATCH 1/5] mime_type and file_size added for file messages --- bot_test.go | 4 +++- configs.go | 10 ++++++++++ types_test.go | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/bot_test.go b/bot_test.go index 17cedac..cd9cdcf 100644 --- a/bot_test.go +++ b/bot_test.go @@ -1,7 +1,7 @@ package tgbotapi_test import ( - "github.com/Syfaro/telegram-bot-api" + "github.com/zhulik/telegram-bot-api" "io/ioutil" "log" "net/http" @@ -184,6 +184,8 @@ func TestSendWithNewAudio(t *testing.T) { msg.Title = "TEST" msg.Duration = 10 msg.Performer = "TEST" + msg.MimeType = "audio/mpeg" + msg.FileSize = 688 _, err := bot.Send(msg) if err != nil { diff --git a/configs.go b/configs.go index 16afef1..0b390f8 100644 --- a/configs.go +++ b/configs.go @@ -93,6 +93,8 @@ type BaseFile struct { File interface{} FileID string UseExisting bool + MimeType string + FileSize int } // 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) } + if len(file.MimeType) > 0 { + params["mime_type"] = file.MimeType + } + + if file.FileSize > 0 { + params["file_size"] = strconv.Itoa(file.FileSize) + } + return params, nil } diff --git a/types_test.go b/types_test.go index 68c57bb..055877f 100644 --- a/types_test.go +++ b/types_test.go @@ -1,7 +1,7 @@ package tgbotapi_test import ( - "github.com/Syfaro/telegram-bot-api" + "github.com/zhulik/telegram-bot-api" "testing" "time" ) From 1cede9cf458b162a92832af661b53b9b2f1ed221 Mon Sep 17 00:00:00 2001 From: Gleb Sinyavsky Date: Sun, 13 Dec 2015 20:31:59 +0300 Subject: [PATCH 2/5] Some helper methods added to Message. Some tests added. Small fixes --- configs.go | 4 +-- types.go | 20 +++++++++++++-- types_test.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 4 deletions(-) diff --git a/configs.go b/configs.go index 0b390f8..00c2830 100644 --- a/configs.go +++ b/configs.go @@ -93,8 +93,8 @@ type BaseFile struct { File interface{} FileID string UseExisting bool - MimeType string - FileSize int + MimeType string + FileSize int } // Params returns map[string]string representation of BaseFile diff --git a/types.go b/types.go index c0d828c..afa1e18 100644 --- a/types.go +++ b/types.go @@ -127,9 +127,25 @@ func (m *Message) IsCommand() bool { 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 { - 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. diff --git a/types_test.go b/types_test.go index 055877f..9e5610c 100644 --- a/types_test.go +++ b/types_test.go @@ -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) { chat := tgbotapi.Chat{ID: 10, Type: "private"} From 1a3e995b376de8e2cb9d01565ced80d8b103896b Mon Sep 17 00:00:00 2001 From: Gleb Sinyavsky Date: Sun, 13 Dec 2015 20:38:09 +0300 Subject: [PATCH 3/5] QuickSend method added to bot --- README.md | 2 ++ bot.go | 6 ++++++ bot_test.go | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/README.md b/README.md index 4e9c06a..d3a3377 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,8 @@ func main() { } ``` +For quickly send text message you can use BotAPI method `QuickSend(chatID, "your message")` + If you need, you may generate a self signed certficate, as this requires HTTPS / TLS. The above example tells Telegram that this is your certificate and that it should be trusted, even though it is not properly signed. openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes diff --git a/bot.go b/bot.go index 73f5ba1..586e2c7 100644 --- a/bot.go +++ b/bot.go @@ -220,6 +220,12 @@ func (bot *BotAPI) Send(c Chattable) (Message, error) { } } +// QuickSend will send message to selected chat +func (bot *BotAPI) QuickSend(chatID int, message string) (Message, error) { + msg := NewMessage(chatID, message) + return bot.Send(msg) +} + func (bot *BotAPI) debugLog(context string, v url.Values, message interface{}) { if bot.Debug { log.Printf("%s req : %+v\n", context, v) diff --git a/bot_test.go b/bot_test.go index cd9cdcf..25bb6b8 100644 --- a/bot_test.go +++ b/bot_test.go @@ -342,6 +342,16 @@ func TestSendChatConfig(t *testing.T) { } } +func TestQuickSend(t *testing.T) { + bot, _ := getBot(t) + + _, err := bot.QuickSend(ChatID, "test message") + + if err != nil { + t.Fail() + } +} + func TestGetUserProfilePhotos(t *testing.T) { bot, _ := getBot(t) From f8cc0d2aaa9960287f9eddc1f393bff3fbf7181e Mon Sep 17 00:00:00 2001 From: Gleb Sinyavsky Date: Wed, 16 Dec 2015 11:38:39 +0300 Subject: [PATCH 4/5] Bot::QuickSend removed --- README.md | 2 -- bot.go | 6 ------ bot_test.go | 10 ---------- 3 files changed, 18 deletions(-) diff --git a/README.md b/README.md index d3a3377..4e9c06a 100644 --- a/README.md +++ b/README.md @@ -85,8 +85,6 @@ func main() { } ``` -For quickly send text message you can use BotAPI method `QuickSend(chatID, "your message")` - If you need, you may generate a self signed certficate, as this requires HTTPS / TLS. The above example tells Telegram that this is your certificate and that it should be trusted, even though it is not properly signed. openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes diff --git a/bot.go b/bot.go index 586e2c7..73f5ba1 100644 --- a/bot.go +++ b/bot.go @@ -220,12 +220,6 @@ func (bot *BotAPI) Send(c Chattable) (Message, error) { } } -// QuickSend will send message to selected chat -func (bot *BotAPI) QuickSend(chatID int, message string) (Message, error) { - msg := NewMessage(chatID, message) - return bot.Send(msg) -} - func (bot *BotAPI) debugLog(context string, v url.Values, message interface{}) { if bot.Debug { log.Printf("%s req : %+v\n", context, v) diff --git a/bot_test.go b/bot_test.go index 25bb6b8..cd9cdcf 100644 --- a/bot_test.go +++ b/bot_test.go @@ -342,16 +342,6 @@ func TestSendChatConfig(t *testing.T) { } } -func TestQuickSend(t *testing.T) { - bot, _ := getBot(t) - - _, err := bot.QuickSend(ChatID, "test message") - - if err != nil { - t.Fail() - } -} - func TestGetUserProfilePhotos(t *testing.T) { bot, _ := getBot(t) From d40f7f97034c8473b3fabe6e3034512fa1d655c9 Mon Sep 17 00:00:00 2001 From: Gleb Sinyavsky Date: Wed, 16 Dec 2015 16:57:21 +0300 Subject: [PATCH 5/5] Package name changed --- bot_test.go | 2 +- types_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bot_test.go b/bot_test.go index cd9cdcf..0a3007c 100644 --- a/bot_test.go +++ b/bot_test.go @@ -1,7 +1,7 @@ package tgbotapi_test import ( - "github.com/zhulik/telegram-bot-api" + "github.com/Syfaro/telegram-bot-api" "io/ioutil" "log" "net/http" diff --git a/types_test.go b/types_test.go index 9e5610c..416f0cb 100644 --- a/types_test.go +++ b/types_test.go @@ -1,7 +1,7 @@ package tgbotapi_test import ( - "github.com/zhulik/telegram-bot-api" + "github.com/Syfaro/telegram-bot-api" "testing" "time" )