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() {
|
2022-05-03 00:39:04 +08:00
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
|
|
defer cancel()
|
|
|
|
|
2022-05-02 20:53:26 +08:00
|
|
|
opts := []bot.Option{
|
|
|
|
bot.WithDefaultHandler(handler),
|
|
|
|
}
|
|
|
|
|
2023-04-26 17:00:17 +08:00
|
|
|
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
|
|
|
|
2022-05-06 17:47:43 +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"}},
|
|
|
|
}
|
|
|
|
|
2022-05-06 17:47:43 +08:00
|
|
|
b.AnswerInlineQuery(ctx, &bot.AnswerInlineQueryParams{
|
2022-05-02 20:53:26 +08:00
|
|
|
InlineQueryID: update.InlineQuery.ID,
|
|
|
|
Results: results,
|
|
|
|
})
|
|
|
|
}
|