Skip to content

Latest commit

 

History

History
86 lines (67 loc) · 2.59 KB

File metadata and controls

86 lines (67 loc) · 2.59 KB

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