diff --git a/examples/inline_mode/main.go b/examples/inline_mode/main.go index 7f0418a1..cef0cce0 100644 --- a/examples/inline_mode/main.go +++ b/examples/inline_mode/main.go @@ -15,9 +15,7 @@ func main() { ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) defer cancel() - opts := []bot.Option{ - bot.WithDefaultHandler(handler), - } + opts := []bot.Option{} b, err := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...) if nil != err { @@ -25,15 +23,12 @@ func main() { // you should handle this error properly in your code. panic(err) } - + // register inline query handler + b.RegisterInlineQueryHandler(handler) b.Start(ctx) } func handler(ctx context.Context, b *bot.Bot, update *models.Update) { - if update.InlineQuery == nil { - return - } - results := []models.InlineQueryResult{ &models.InlineQueryResultArticle{ID: "1", Title: "Foo 1", InputMessageContent: &models.InputTextMessageContent{MessageText: "foo 1"}}, &models.InlineQueryResultArticle{ID: "2", Title: "Foo 2", InputMessageContent: &models.InputTextMessageContent{MessageText: "foo 2"}}, diff --git a/handlers.go b/handlers.go index 85457810..7bb20d17 100644 --- a/handlers.go +++ b/handlers.go @@ -14,6 +14,7 @@ const ( HandlerTypeCallbackQueryData HandlerTypeCallbackQueryGameShortName HandlerTypePhotoCaption + HandlerTypeInlineQuery ) type MatchType int @@ -71,6 +72,12 @@ func (h handler) match(update *models.Update) bool { } data = update.Message.Caption entities = update.Message.CaptionEntities + case HandlerTypeInlineQuery: + if update.InlineQuery == nil { + return false + } + return true + } if h.matchType == MatchTypeExact { @@ -143,6 +150,23 @@ func (b *Bot) RegisterHandlerRegexp(handlerType HandlerType, re *regexp.Regexp, return id } +func (b *Bot) RegisterInlineQueryHandler(f HandlerFunc, m ...Middleware) string { + b.handlersMx.Lock() + defer b.handlersMx.Unlock() + + id := RandomString(16) + + h := handler{ + id: id, + handlerType: HandlerTypeInlineQuery, + handler: applyMiddlewares(f, m...), + } + + b.handlers = append(b.handlers, h) + + return id +} + func (b *Bot) RegisterHandler(handlerType HandlerType, pattern string, matchType MatchType, f HandlerFunc, m ...Middleware) string { b.handlersMx.Lock() defer b.handlersMx.Unlock()