Skip to main content

Overview

This example shows how to create different types of keyboards for your bot, including inline buttons, grid layouts, and custom keyboard arrangements.

Code

package examples

import (
	"github.com/amarnathcjd/gogram/telegram"
)

const (
	appID    = 6
	appHash  = "YOUR_APP_HASH"
	botToken = "YOUR_BOT_TOKEN"
)

func main() {
	// Create a new client object
	client, _ := telegram.NewClient(telegram.ClientConfig{
		AppID:    appID,
		AppHash:  appHash,
		LogLevel: telegram.LogInfo,
	})

	// Authenticate as a bot
	client.LoginBot(botToken)

	// Send a message with inline keyboard
	client.SendMessage("username", "Hello, This is ReplyMarkup Example", &telegram.SendOptions{
		ReplyMarkup: telegram.NewKeyboard().AddRow( // Add a row of buttons
			telegram.Button.Data("Help", "help"),
			telegram.Button.URL("Google", "https://www.google.com"),
		).Build(), // .AddRow() can be chained to add more rows
	})

	// Handle /start command with grid layout
	client.On("message:/start", func(message *telegram.NewMessage) error {
		message.Reply("Hello:>", telegram.SendOptions{
			// Create a 2x3 grid (2 rows, 3 columns)
			ReplyMarkup: telegram.NewKeyboard().NewGrid(2, 3, // 2 rows, 3 columns format
				telegram.Button.URL("Google", "https://www.google.com"),
				telegram.Button.Data("Help", "help"),
				telegram.Button.Data("Main Menu", "main"),
				telegram.Button.Data("Source", "source"),
				telegram.Button.SwitchInline("goinline", true, "query"),
				telegram.Button.Data("Back", "back")).Build(),
		})
		return nil
	})

	// Example: 2 column layout
	telegram.NewKeyboard().NewColumn(2, // 2 columns format
		telegram.Button.URL("Google", "https://www.google.com"),
		telegram.Button.URL("Yahoo", "https://www.yahoo.com"),
		telegram.Button.URL("Bing", "https://www.bing.com"),
		telegram.Button.URL("DuckDuckGo", "https://www.duckduckgo.com"),
	).Build() // Buttons equally divided into 2 columns

	// Example: 3 row layout
	telegram.NewKeyboard().NewRow(3, // 3 rows format
		telegram.Button.URL("Google", "https://www.google.com"),
		telegram.Button.URL("Yahoo", "https://www.yahoo.com"),
		telegram.Button.URL("Bing", "https://www.bing.com"),
		telegram.Button.URL("DuckDuckGo", "https://www.duckduckgo.com"),
	).Build() // Buttons equally divided into 3 rows

	// Handle callback from help button
	client.On("callback:help", func(callback *telegram.CallbackQuery) error {
		callback.Answer("Help is on the way!")
		return nil
	})

	// Keep bot running
	client.Idle()
}

How It Works

  1. Create Keyboard: Use telegram.NewKeyboard() to start building
  2. Add Buttons: Chain .AddRow() or use .NewGrid(), .NewColumn(), .NewRow()
  3. Build: Call .Build() to finalize the keyboard
  4. Attach: Pass keyboard to SendOptions.ReplyMarkup

Button Types

  • Data Button
  • URL Button
  • Inline Switch
  • Login Button
  • WebApp Button
// Callback data button (triggers callback query)
keyboard := telegram.NewKeyboard().AddRow(
	telegram.Button.Data("Click Me", "button_data"),
	telegram.Button.Data("Settings", "settings_menu"),
).Build()

// Handle callback
client.On("callback:button_data", func(c *telegram.CallbackQuery) error {
	c.Answer("Button clicked!")
	return nil
})

Keyboard Layouts

  • Manual Rows
  • Grid Layout
  • Column Layout
  • Row Layout
// Add buttons row by row
keyboard := telegram.NewKeyboard().
	AddRow(
		telegram.Button.Data("Button 1", "btn1"),
		telegram.Button.Data("Button 2", "btn2"),
	).
	AddRow(
		telegram.Button.Data("Button 3", "btn3"),
	).
	AddRow(
		telegram.Button.URL("Google", "https://google.com"),
		telegram.Button.URL("GitHub", "https://github.com"),
	).
	Build()

Complete Example

func HandleMenu(m *telegram.NewMessage) error {
	// Main menu with multiple button types
	keyboard := telegram.NewKeyboard().
		AddRow(
			telegram.Button.Data("📊 Statistics", "stats"),
			telegram.Button.Data("⚙️ Settings", "settings"),
		).
		AddRow(
			telegram.Button.Data("❓ Help", "help"),
			telegram.Button.URL("📖 Docs", "https://docs.example.com"),
		).
		AddRow(
			telegram.Button.SwitchInline("🔍 Search", false, ""),
		).
		Build()
	
	m.Reply("Main Menu", telegram.SendOptions{
		ReplyMarkup: keyboard,
	})
	
	return nil
}

// Handle all callbacks
func HandleCallbacks(c *telegram.CallbackQuery) error {
	switch c.Data {
	case "stats":
		c.Edit("Statistics: ...")
	case "settings":
		// Show settings keyboard
		settingsKb := telegram.NewKeyboard().
			AddRow(telegram.Button.Data("🔔 Notifications", "notif")).
			AddRow(telegram.Button.Data("🌐 Language", "lang")).
			AddRow(telegram.Button.Data("⬅️ Back", "main")).
			Build()
		
		c.Edit("Settings", &telegram.EditOptions{
			ReplyMarkup: settingsKb,
		})
	case "help":
		c.Answer("Help information here", &telegram.AnswerOptions{
			Alert: true, // Show as alert
		})
	case "main":
		HandleMenu(c.Message.(*telegram.NewMessage))
	}
	
	return nil
}

Pagination Example

Create paginated keyboards for navigation:
func CreatePagination(page, totalPages int) telegram.ReplyMarkup {
	keyboard := telegram.NewKeyboard()
	
	// Add navigation buttons
	var buttons []telegram.Button
	
	if page > 1 {
		buttons = append(buttons, telegram.Button.Data("◀️ Prev", fmt.Sprintf("page_%d", page-1)))
	}
	
	buttons = append(buttons, telegram.Button.Data(fmt.Sprintf("%d/%d", page, totalPages), "current"))
	
	if page < totalPages {
		buttons = append(buttons, telegram.Button.Data("Next ▶️", fmt.Sprintf("page_%d", page+1)))
	}
	
	keyboard.AddRow(buttons...)
	return keyboard.Build()
}

// Usage
func ShowPage(c *telegram.CallbackQuery, page int) error {
	keyboard := CreatePagination(page, 10)
	c.Edit(fmt.Sprintf("Page %d content", page), &telegram.EditOptions{
		ReplyMarkup: keyboard,
	})
	return nil
}

Reply Keyboards

Create custom reply keyboards (bottom keyboard):
// Reply keyboard (replaces user's keyboard)
replyKeyboard := telegram.NewReplyKeyboard(
	[]telegram.KeyboardButtonRow{
		{
			Buttons: []telegram.KeyboardButton{
				{Text: "Option 1"},
				{Text: "Option 2"},
			},
		},
		{
			Buttons: []telegram.KeyboardButton{
				{Text: "Option 3"},
			},
		},
	},
	false, // OneTime - hide after use
	false, // Resize keyboard
	false, // Selective - show to specific users
)

client.SendMessage("user", "Choose an option:", &telegram.SendOptions{
	ReplyMarkup: replyKeyboard,
})

// Remove keyboard
client.SendMessage("user", "Keyboard removed", &telegram.SendOptions{
	ReplyMarkup: telegram.NewReplyKeyboardHide(false),
})

Running the Example

  1. Replace YOUR_APP_HASH and YOUR_BOT_TOKEN with your credentials
  2. Replace "username" with a target username or chat ID
  3. Run the program
  4. Send /start to your bot to see the keyboard
Use descriptive callback data to easily identify button actions. Consider using prefixes like menu:, page:, action: to organize callbacks.

Next Steps