Methods

Bot methods

The bot-only surface: registering commands, customising the menu button, answering inline queries, handling Web App data. The rest of the bot toolkit (banning, pinning, restricting) lives on the channel methods.

Setting commands

_, err := client.SetBotCommands(
	[]*telegram.BotCommand{
		{Command: "start",  Description: "Begin a chat"},
		{Command: "help",   Description: "Show help"},
		{Command: "search", Description: "Search the index"},
	},
	&telegram.BotCommandScope{
		Type: telegram.BotCommandScopeDefault,
	},
	"en",
)
_, err := client.SetBotCommands(
	[]*telegram.BotCommand{
		{Command: "start",  Description: "Begin a chat"},
		{Command: "help",   Description: "Show help"},
		{Command: "search", Description: "Search the index"},
	},
	&telegram.BotCommandScope{
		Type: telegram.BotCommandScopeDefault,
	},
	"en",
)

Commands populate the in-app command menu and the autocomplete that appears when a user types /. The list replaces any previously-registered set for the same scope and language.

Command scopes

The scope decides where the commands are visible. Pass a BotCommandScope with the right Type; some scope types take additional fields.

  • BotCommandScopeDefault — everywhere unless overridden.
  • BotCommandScopeUsers — all private chats.
  • BotCommandScopeChats — all group chats.
  • BotCommandScopeChatAdmins — admins of every group the bot is in.
  • BotCommandScopePeer — one specific chat.
  • BotCommandScopePeerAdmins — admins of one specific chat.
  • BotCommandScopePeerUser — one user inside one chat.
_, _ = client.SetBotCommands(commands, &telegram.BotCommandScope{
	Type: telegram.BotCommandScopeAllPrivateChats,
}, "en")

// per-chat:
_, _ = client.SetBotCommands(commands, &telegram.BotCommandScope{
	Type: telegram.BotCommandScopePeer,
	Peer: chatID,
}, "en")

// per-user-in-chat:
_, _ = client.SetBotCommands(commands, &telegram.BotCommandScope{
	Type:   telegram.BotCommandScopePeerUser,
	Peer:   chatID,
	UserID: userID,
}, "en")
_, _ = client.SetBotCommands(commands, &telegram.BotCommandScope{
	Type: telegram.BotCommandScopeAllPrivateChats,
}, "en")

// per-chat:
_, _ = client.SetBotCommands(commands, &telegram.BotCommandScope{
	Type: telegram.BotCommandScopePeer,
	Peer: chatID,
}, "en")

// per-user-in-chat:
_, _ = client.SetBotCommands(commands, &telegram.BotCommandScope{
	Type:   telegram.BotCommandScopePeerUser,
	Peer:   chatID,
	UserID: userID,
}, "en")

The third argument is a language code (ISO 639-1). Pass an empty string for "language-agnostic." Each language can have a different command list; the client picks the one matching the user's language with a fallback to the empty-code set.

The blue button next to the input box in private chats. The default behaviour is "open command menu." Replace it with a Web App URL via the bots.setBotMenuButton raw method. For a global default across every user and chat:

_, _ = client.BotsSetBotMenuButton(&telegram.BotsSetBotMenuButtonParams{
	UserID: &telegram.InputUserEmpty{},
	Button: &telegram.BotMenuButtonObj{
		Text: "Open",
		URL:  "https://miniapp.example.com",
	},
})
_, _ = client.BotsSetBotMenuButton(&telegram.BotsSetBotMenuButtonParams{
	UserID: &telegram.InputUserEmpty{},
	Button: &telegram.BotMenuButtonObj{
		Text: "Open",
		URL:  "https://miniapp.example.com",
	},
})

Answering inline queries

client.On("inline:*", func(q *telegram.InlineQuery) error {
	return q.Answer([]telegram.InputBotInlineResult{
		&telegram.InputBotInlineResultObj{
			ID:    "1",
			Type:  "article",
			Title: "Hello",
			SendMessage: &telegram.InputBotInlineMessageText{
				Message: "Hello from inline mode",
			},
		},
	}, &telegram.InlineAnswerOptions{
		CacheTime: 60,
	})
})
client.On("inline:*", func(q *telegram.InlineQuery) error {
	return q.Answer([]telegram.InputBotInlineResult{
		&telegram.InputBotInlineResultObj{
			ID:    "1",
			Type:  "article",
			Title: "Hello",
			SendMessage: &telegram.InputBotInlineMessageText{
				Message: "Hello from inline mode",
			},
		},
	}, &telegram.InlineAnswerOptions{
		CacheTime: 60,
	})
})

Each InlineQuery has the query string, a query id, the sender, and an offset for pagination. Answer returns up to 50 results. CacheTime seconds tells the client how long to keep these results without re-querying. Set NextOffset on the answer options to support scroll-through pagination.

Web App data

When a user submits data from a Web App via Telegram.WebApp.sendData(...), the bot receives the payload on a normal message handler — the resulting service message carries MessageActionWebViewDataSentMe. Handle it via the "action" event:

client.On("action", func(m *telegram.NewMessage) error {
	if data, ok := m.Action.(*telegram.MessageActionWebViewDataSentMe); ok {
		processWebAppPayload(data.Data)
	}
	return nil
})
client.On("action", func(m *telegram.NewMessage) error {
	if data, ok := m.Action.(*telegram.MessageActionWebViewDataSentMe); ok {
		processWebAppPayload(data.Data)
	}
	return nil
})