From 94f3da603fbeaf0cf7159b8cc3578da44f7e64ba Mon Sep 17 00:00:00 2001 From: Vincent Heins Date: Mon, 2 Jul 2018 08:56:30 +0200 Subject: [PATCH 1/5] add method to write custom webhook http funcs --- bot.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/bot.go b/bot.go index 8fb6200..8402d7f 100644 --- a/bot.go +++ b/bot.go @@ -427,7 +427,7 @@ func (bot *BotAPI) GetUpdates(config UpdateConfig) ([]Update, error) { return updates, nil } -// RemoveWebhook unsets the webhook. +// Remove unsets the webhook. func (bot *BotAPI) RemoveWebhook() (APIResponse, error) { return bot.MakeRequest("setWebhook", url.Values{}) } @@ -505,17 +505,25 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) { return ch, nil } +//GetWebhookUpdate gathers webhook information from a http.Request +func GetWebhookUpdate(r *http.Request) (*Update, error) { + b, err := ioutil.ReadAll(r.Body) + if err != nil { + return nil, err + } + + var update Update + err = json.Unmarshal(b, &update) + return &update, err +} + // ListenForWebhook registers a http handler for a webhook. func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel { ch := make(chan Update, bot.Buffer) http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { - bytes, _ := ioutil.ReadAll(r.Body) - - var update Update - json.Unmarshal(bytes, &update) - - ch <- update + update, _ := GetWebhookUpdate(r) + ch <- *update }) return ch From 99bd28b4af1b9a60fe80a87e0e4535644ba14638 Mon Sep 17 00:00:00 2001 From: Vincent Heins Date: Mon, 2 Jul 2018 08:59:51 +0200 Subject: [PATCH 2/5] Update bot.go --- bot.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.go b/bot.go index 8402d7f..c0c1d55 100644 --- a/bot.go +++ b/bot.go @@ -427,7 +427,7 @@ func (bot *BotAPI) GetUpdates(config UpdateConfig) ([]Update, error) { return updates, nil } -// Remove unsets the webhook. +// RemoveWebhook unsets the webhook. func (bot *BotAPI) RemoveWebhook() (APIResponse, error) { return bot.MakeRequest("setWebhook", url.Values{}) } From 6d7df38037743e33fb2bcb4c1d889a31c0994949 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 2 Jul 2018 23:03:05 +0200 Subject: [PATCH 3/5] add command function --- types.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/types.go b/types.go index 0843ab9..0c790ec 100644 --- a/types.go +++ b/types.go @@ -184,6 +184,52 @@ func (m *Message) IsCommand() bool { return entity.Offset == 0 && entity.Type == "bot_command" } +type Command struct { + Name string + Arguments []string +} + +type Commands []Command + +func (m *Message) GetCommands() (*Commands, error) { + var botCmdsEntries []MessageEntity + for _, e := range *m.Entities { + if e.Type == "bot_command" { + botCmdsEntries = append(botCmdsEntries, e) + } + } + + var cmds Commands + text := []rune(m.Text) + for i := 0; i < len(botCmdsEntries); i++ { + e := botCmdsEntries[i] + nOff := len(text) + if i+1 >= len(botCmdsEntries) { + nOff = (botCmdsEntries[i+1]).Offset + } + cmd := Command{ + Name: string(text[e.Offset : e.Offset+e.Length]), + Arguments: strings.Split(string(text[e.Offset+e.Length:nOff]), " "), + } + } + + return &cmds, nil +} + +func (m *Message) CountCommands() int { + var c int + for _, e := range *m.Entities { + if e.Type == "bot_command" { + c++ + } + } + return c +} + +func (m *Message) nextCommand(currentOffset int) *MessageEntity { + +} + // Command checks if the message was a command and if it was, returns the // command. If the Message was not a command, it returns an empty string. // From ab0aca70ed3d283c6e3f1df498b30a629c18bcaf Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 2 Jul 2018 23:10:06 +0200 Subject: [PATCH 4/5] update comments --- types.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/types.go b/types.go index 0c790ec..caa5ccd 100644 --- a/types.go +++ b/types.go @@ -184,13 +184,16 @@ func (m *Message) IsCommand() bool { return entity.Offset == 0 && entity.Type == "bot_command" } +//Command represents a command contained in a message type Command struct { Name string Arguments []string } +//Commands represents a slice of Command type Commands []Command +//GetCommands returns all commands func (m *Message) GetCommands() (*Commands, error) { var botCmdsEntries []MessageEntity for _, e := range *m.Entities { @@ -211,11 +214,13 @@ func (m *Message) GetCommands() (*Commands, error) { Name: string(text[e.Offset : e.Offset+e.Length]), Arguments: strings.Split(string(text[e.Offset+e.Length:nOff]), " "), } + cmds = append(cmds, cmd) } return &cmds, nil } +//CountCommands counts all commands in a message func (m *Message) CountCommands() int { var c int for _, e := range *m.Entities { @@ -226,10 +231,6 @@ func (m *Message) CountCommands() int { return c } -func (m *Message) nextCommand(currentOffset int) *MessageEntity { - -} - // Command checks if the message was a command and if it was, returns the // command. If the Message was not a command, it returns an empty string. // From d0ecb3c9f3ea2e30ebc81f823d9020849377d154 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 2 Jul 2018 23:43:07 +0200 Subject: [PATCH 5/5] add GetCommands() test --- bot_test.go | 2 +- helpers_test.go | 3 ++- types.go | 9 +++++---- types_test.go | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/bot_test.go b/bot_test.go index e7aa7ac..abea6d1 100644 --- a/bot_test.go +++ b/bot_test.go @@ -8,7 +8,7 @@ import ( "testing" "time" - "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/TheMysteriousVincent/telegram-bot-api" ) const ( diff --git a/helpers_test.go b/helpers_test.go index 9542f02..4f29427 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -1,8 +1,9 @@ package tgbotapi_test import ( - "github.com/go-telegram-bot-api/telegram-bot-api" "testing" + + "github.com/TheMysteriousVincent/telegram-bot-api" ) func TestNewInlineQueryResultArticle(t *testing.T) { diff --git a/types.go b/types.go index caa5ccd..dd46738 100644 --- a/types.go +++ b/types.go @@ -204,15 +204,16 @@ func (m *Message) GetCommands() (*Commands, error) { var cmds Commands text := []rune(m.Text) + textLen := len(text) for i := 0; i < len(botCmdsEntries); i++ { e := botCmdsEntries[i] - nOff := len(text) - if i+1 >= len(botCmdsEntries) { + nOff := textLen + if i+1 < len(botCmdsEntries) { nOff = (botCmdsEntries[i+1]).Offset } cmd := Command{ - Name: string(text[e.Offset : e.Offset+e.Length]), - Arguments: strings.Split(string(text[e.Offset+e.Length:nOff]), " "), + Name: string(text[e.Offset+1 : e.Offset+e.Length]), + Arguments: strings.Fields(string(text[e.Offset+e.Length : nOff])), } cmds = append(cmds, cmd) } diff --git a/types_test.go b/types_test.go index bb7bb64..c18860f 100644 --- a/types_test.go +++ b/types_test.go @@ -1,10 +1,11 @@ package tgbotapi_test import ( + "fmt" "testing" "time" - "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/TheMysteriousVincent/telegram-bot-api" ) func TestUserStringWith(t *testing.T) { @@ -45,6 +46,38 @@ func TestMessageTime(t *testing.T) { } } +func TestGetCommands(t *testing.T) { + message := tgbotapi.Message{Text: "/test t\n/testCommandsWith2Arguments TestArgument1 TestArgument2"} + message.Entities = &[]tgbotapi.MessageEntity{ + { + Type: "bot_command", + Offset: 0, + Length: 5, + }, + { + Type: "bot_command", + Offset: 8, + Length: 27, + }, + } + + cmds, _ := message.GetCommands() + fmt.Println(*cmds) + + if len(*cmds) != 2 { + t.Fatal("there have to be exactly two entries") + } + + cmd := (*cmds)[0] + if cmd.Name != "test" { + t.Fatal("name of cmd 1 is '", cmd.Name, "' (test)") + } + cmd = (*cmds)[1] + if cmd.Name != "testCommandsWith2Arguments" { + t.Fatal("name of cmd 1 is '", cmd.Name, "' (testCommandsWith2Arguments)") + } +} + func TestMessageIsCommandWithCommand(t *testing.T) { message := tgbotapi.Message{Text: "/command"} message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}