From df7ef7434608b077dc8e6d335be4137fb14b0e60 Mon Sep 17 00:00:00 2001 From: Abhinav Dahiya Date: Mon, 14 Mar 2016 15:15:23 +0530 Subject: [PATCH] ChosenInlineResult Handler added --- bot.go | 35 ++++++++++++++++++++++++++--------- types.go | 24 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/bot.go b/bot.go index 58c496f..72b5849 100644 --- a/bot.go +++ b/bot.go @@ -21,13 +21,14 @@ import ( // BotAPI allows you to interact with the Telegram Bot API. type BotAPI struct { - Token string `json:"token"` - Debug bool `json:"debug"` - Self User `json:"-"` - Client *http.Client `json:"-"` - CommandHandlers map[string]MessageHandlerFunc - TextHandler MessageHandlerFunc - InlineHandler InlineHandlerFunc + Token string `json:"token"` + Debug bool `json:"debug"` + Self User `json:"-"` + Client *http.Client `json:"-"` + CommandHandlers map[string]MessageHandlerFunc + TextHandler MessageHandlerFunc + InlineHandler InlineHandlerFunc + InlineResultHandler InlineResultHandlerFunc } // NewBotAPI creates a new BotAPI instance. @@ -60,7 +61,7 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { // ProcessUpdate calls corresponding handler for update func (bot *BotAPI) ProcessUpdate(update Update) { - if !update.IsInlineQuery() { + if update.IsMessage() { msg := update.Message if msg.IsCommand() { cmd := msg.Command() @@ -76,13 +77,22 @@ func (bot *BotAPI) ProcessUpdate(update Update) { log.Fatal("No Text Handler defined") } } - } else { + } else if update.IsInlineQuery() { query := update.InlineQuery if f := bot.InlineHandler; f != nil { f.Serve(bot, query) } else { log.Fatal("No Inline Query Handler defined") } + } else if update.IsChosenInlineResult() { + rt := update.ChosenInlineResult + if f := bot.InlineResultHandler; f != nil { + f.Serve(bot, rt) + } else { + log.Fatal("No Inline Result Handler defined") + } + } else { + log.Printf("Invalid Update") } } @@ -107,6 +117,13 @@ func (bot *BotAPI) AddInlineHandler(f InlineHandlerFunc) { bot.InlineHandler = f } +// AddInlineResultHandler adds a InlineResultHandlerFunc for chosen inline result +// +// +func (bot *BotAPI) AddInlineResultHandler(f InlineResultHandlerFunc) { + bot.InlineResultHandler = f +} + // MakeRequest makes a request to a specific endpoint with our token. func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) { method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) diff --git a/types.go b/types.go index 668b056..5dce32c 100644 --- a/types.go +++ b/types.go @@ -33,6 +33,22 @@ func (u Update) IsInlineQuery() bool { return false } +// Checks if Update is Message +func (u Update) IsMessage() bool { + if u.Message.MessageID != 0 { + return true + } + return false +} + +// Checks if Update is ChosenInlineResult +func (u Update) IsChosenInlineResult() bool { + if u.ChosenInlineResult.ResultID != "" { + return true + } + return false +} + // User is a user on Telegram. type User struct { ID int `json:"id"` @@ -393,3 +409,11 @@ type InlineHandlerFunc func(*BotAPI, InlineQuery) func (f InlineHandlerFunc) Serve(bot *BotAPI, q InlineQuery) { f(bot, q) } + +// InlineResultHandlerFunc for handling Inline Query +// Something similar in line to http.HandlerFunc +type InlineResultHandlerFunc func(*BotAPI, ChosenInlineResult) + +func (f InlineResultHandlerFunc) Serve(bot *BotAPI, q ChosenInlineResult) { + f(bot, q) +}