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
55 changes: 55 additions & 0 deletions examples/inline_keyboard_builder/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"context"
"fmt"
"os"
"os/signal"

"github.com/go-telegram/bot"
"github.com/go-telegram/bot/keyboards"
"github.com/go-telegram/bot/models"
)

func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

opts := []bot.Option{
bot.WithDefaultHandler(handler),
bot.WithCallbackQueryDataHandler("button", bot.MatchTypePrefix, callbackHandler),
}

b, err := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
if nil != err {
panic(err)
}

b.Start(ctx)
}

func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
kb := keyboards.NewInlineKeyboard()

kb.Row(
keyboards.InlineKeyboardButton("Button 1", "button_1"),
keyboards.InlineKeyboardButton("Button 2", "button_2"),
)
kb.Row(
keyboards.InlineKeyboardURL("Google", "https://google.com"),
)

b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: "Here is your keyboard",
ReplyMarkup: kb.Build(),
})
}

func callbackHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
b.AnswerCallbackQuery(ctx, &bot.AnswerCallbackQueryParams{
CallbackQueryID: update.CallbackQuery.ID,
ShowAlert: true,
Text: fmt.Sprintf("You clicked %s", update.CallbackQuery.Data),
})
}
92 changes: 92 additions & 0 deletions keyboards/inline_keyboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package keyboards

import "github.com/go-telegram/bot/models"

type InlineKeyboard struct {
rows [][]models.InlineKeyboardButton
}

func NewInlineKeyboard() *InlineKeyboard {
return &InlineKeyboard{}
}

func (b *InlineKeyboard) Row(btns ...models.InlineKeyboardButton) *InlineKeyboard {
b.rows = append(b.rows, btns)
return b
}

func (b *InlineKeyboard) Build() models.InlineKeyboardMarkup {
return models.InlineKeyboardMarkup{
InlineKeyboard: b.rows,
}
}

func InlineKeyboardButton(text, data string) models.InlineKeyboardButton {
return models.InlineKeyboardButton{
Text: text,
CallbackData: data,
}
}

func InlineKeyboardURL(text, url string) models.InlineKeyboardButton {
return models.InlineKeyboardButton{
Text: text,
URL: url,
}
}

func InlineKeyboardWebApp(text string, webApp models.WebAppInfo) models.InlineKeyboardButton {
return models.InlineKeyboardButton{
Text: text,
WebApp: &webApp,
}
}

func InlineKeyboardLoginURL(text string, loginURL models.LoginURL) models.InlineKeyboardButton {
return models.InlineKeyboardButton{
Text: text,
LoginURL: &loginURL,
}
}

func InlineKeyboardSwitchInlineQuery(text, query string) models.InlineKeyboardButton {
return models.InlineKeyboardButton{
Text: text,
SwitchInlineQuery: query,
}
}

func InlineKeyboardSwitchInlineQueryCurrentChat(text, query string) models.InlineKeyboardButton {
return models.InlineKeyboardButton{
Text: text,
SwitchInlineQueryCurrentChat: query,
}
}

func InlineKeyboardSwitchInlineQueryChosenChat(text string, chosenChat models.SwitchInlineQueryChosenChat) models.InlineKeyboardButton {
return models.InlineKeyboardButton{
Text: text,
SwitchInlineQueryChosenChat: &chosenChat,
}
}

func InlineKeyboardCopyText(text string, copyText models.CopyTextButton) models.InlineKeyboardButton {
return models.InlineKeyboardButton{
Text: text,
CopyText: copyText,
}
}

func InlineKeyboardCallbackGame(text string, callbackGame models.CallbackGame) models.InlineKeyboardButton {
return models.InlineKeyboardButton{
Text: text,
CallbackGame: &callbackGame,
}
}

func InlineKeyboardPay(text string) models.InlineKeyboardButton {
return models.InlineKeyboardButton{
Text: text,
Pay: true,
}
}