ChosenInlineResult Handler added

pull/40/head
Abhinav Dahiya 9 years ago
parent e41d538006
commit df7ef74346
  1. 35
      bot.go
  2. 24
      types.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)

@ -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)
}