1
bot/examples/inline_mode/main.go

48 lines
1.2 KiB
Go
Raw Permalink Normal View History

2022-05-02 20:53:26 +08:00
package main
import (
"context"
"os"
"os/signal"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
// Use inline mode @botname some_text
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
2022-05-02 20:53:26 +08:00
opts := []bot.Option{
bot.WithDefaultHandler(handler),
}
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)
}
2022-05-02 20:53:26 +08:00
b.Start(ctx)
2022-05-02 20:53:26 +08:00
}
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"}},
&models.InlineQueryResultArticle{ID: "3", Title: "Foo 3", InputMessageContent: &models.InputTextMessageContent{MessageText: "foo 3"}},
}
b.AnswerInlineQuery(ctx, &bot.AnswerInlineQueryParams{
2022-05-02 20:53:26 +08:00
InlineQueryID: update.InlineQuery.ID,
Results: results,
})
}