tgwebapp

package
v0.14.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2026 License: Apache-2.0, MIT Imports: 10 Imported by: 0

README

tgwebapp — Telegram Web App (Mini App) init-data validation

The tgwebapp package validates the initData received from a Telegram Web App (Mini App) and parses it into a typed InitData struct.

Installation

go get github.com/bots-go-framework/bots-api-telegram/tgwebapp

Usage

Call AuthenticateTelegramWebApp from your HTTP handler. It reads the POST body, validates the HMAC signature, and invokes complete with the parsed InitData. On any error it writes the appropriate HTTP status and calls complete with an empty InitData.

import "github.com/bots-go-framework/bots-api-telegram/tgwebapp"

http.HandleFunc("/webapp", func(w http.ResponseWriter, r *http.Request) {
    tgwebapp.AuthenticateTelegramWebApp(w, r,
        func(bot string) string {
            return os.Getenv("TELEGRAM_BOT_TOKEN")
        },
        func(initData *tgwebapp.InitData) {
            if initData.QueryID == "" {
                return // authentication failed; HTTP error already written
            }
            // initData is verified — handle the request
            fmt.Fprintf(w, "Hello, auth_date=%d", initData.AuthDate)
        },
    )
})

Exported API

AuthenticateTelegramWebApp
func AuthenticateTelegramWebApp(
    w          http.ResponseWriter,
    r          *http.Request,
    getToken   func(bot string) string,
    complete   func(initData *InitData),
)

Validates the request and calls complete with the parsed init data. The bot parameter passed to getToken comes from the ?bot= query string of the request URL, allowing a single handler to serve multiple bots.

InitData

Parsed representation of the WebAppInitData object:

type InitData struct {
    QueryID      string `json:"query_id"`
    ChatType     string `json:"chat_type,omitempty"`
    ChatInstance string `json:"chat_instance,omitempty"`
    StartParam   string `json:"start_param,omitempty"`
    CanSendAfter int    `json:"can_send_after,omitempty"`
    AuthDate     int    `json:"auth_date"`
    Hash         string `json:"hash"`
}
NewInitDataFromUrlValues
func NewInitDataFromUrlValues(values url.Values) InitData

Parses a url.Values map (e.g. from a POST body) into an InitData struct.

Telegram documentation

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthenticateTelegramWebApp

func AuthenticateTelegramWebApp(
	w http.ResponseWriter, r *http.Request,
	getToken func(bot string) string,
	complete func(initData *InitData),
)

AuthenticateTelegramWebApp validates Telegram web app init data https://core.telegram.org/bots/webapps#webappinitdata TODO: Move some of it into Telegram FW module?

Types

type InitData

type InitData struct {
	// QueryID - A unique identifier for the Web App session, required for sending messages via the answerWebAppQuery method.
	QueryID string `json:"query_id"`

	// ChatType - Type of the chat from which the Web App was opened.
	// Can be either “sender” for a private chat with the user opening the link,
	// “private”, “group”, “supergroup”, or “channel”.
	//Returned only for Web Apps launched from direct links.
	ChatType string `json:"chat_type,omitempty"`

	// ChatInstance - Global identifier, uniquely corresponding to the chat from which the Web App was opened.
	// Returned only for Web Apps launched from a direct link.
	ChatInstance string `json:"chat_instance,omitempty"`

	// StartParam - The value of the startattach parameter, passed via link. Only returned for Web Apps when launched from the attachment menu via link.
	StartParam string `json:"start_param,omitempty"`

	// CanSendAfter - Time in seconds, after which a message can be sent via the answerWebAppQuery method.
	CanSendAfter int `json:"can_send_after,omitempty"`

	// AuthDate - Unix time when the form was opened.
	AuthDate int `json:"auth_date"`

	// Hash of all passed parameters, which the bot server can use to check their validity.
	Hash string `json:"hash"`
}

InitData describes Telegram web app init data https://core.telegram.org/bots/webapps#webappinitdata

func NewInitDataFromUrlValues added in v0.4.0

func NewInitDataFromUrlValues(values url.Values) InitData

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL