Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions examples/inline_mode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,20 @@ 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 {
// panics for the sake of simplicity.
// 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"}},
Expand Down
24 changes: 24 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
HandlerTypeCallbackQueryData
HandlerTypeCallbackQueryGameShortName
HandlerTypePhotoCaption
HandlerTypeInlineQuery
)

type MatchType int
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down