twa

package module
v0.0.0-...-7cb2e57 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2022 License: MIT Imports: 11 Imported by: 0

README

Golang Telegram Web Apps Init Data

Telegram Web Apps init data GoLang library implementation. For more information about init data, visit this link.

Contribution

Any contribution is appreciated. Please, use already existed examples to create new one.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAuthDateMissing  = errors.New("auth_date is missing")
	ErrSignMissing      = errors.New("sign is missing")
	ErrSignInvalid      = errors.New("sign is invalid")
	ErrUnexpectedFormat = errors.New("init data has unexpected format")
	ErrExpired          = errors.New("init data is expired")
)

Functions

func Validate

func Validate(initData, token string, expIn time.Duration) (bool, error)

Validate validates passed init data. This method expects initData to be passed in the exact raw format as it could be found in window.Telegram.WebApp.initData. Returns true in case init data is signed correctly, and it is allowed to trust it.

Current code is implementation of algorithmic code described in official docs: https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app

initData - init data passed from application; token - TWA bot secret token which was used to create init data; expIn - maximum init data lifetime. It is strongly recommended using this parameter. In case, zero duration is less than 0, function does not check if parameters are expired.

Types

type Chat

type Chat struct {
	Id       int64  `json:"id"`
	Type     string `json:"type"`
	Title    string `json:"title"`
	Username string `json:"username"`
	PhotoUrl string `json:"photo_url"`
}

Chat describes chat information: https://core.telegram.org/bots/webapps#webappchat

type InitData

type InitData struct {
	QueryId         string `json:"query_id"`
	User            *User  `json:"user"`
	Receiver        *User  `json:"receiver"`
	Chat            *Chat  `json:"chat"`
	StartParam      string `json:"start_param"`
	CanSendAfterRaw int    `json:"can_send_after"`
	AuthDateRaw     int    `json:"auth_date"`
	Hash            string `json:"hash"`
}

InitData describes parsed initial data sent from TWA application. You can find specification for all the parameters in the official documentation: https://core.telegram.org/bots/webapps#webappinitdata

func Parse

func Parse(initData string) (*InitData, error)

Parse converts passed init data presented as query string to InitData object.

func (*InitData) AuthDate

func (d *InitData) AuthDate() time.Time

AuthDate returns AuthDateRaw as time.Time..

func (*InitData) CanSendAfter

func (d *InitData) CanSendAfter() time.Duration

CanSendAfter returns CanSendAfterRaw as time.Duration.

type User

type User struct {
	Id           int64  `json:"id"`
	IsBot        bool   `json:"is_bot"`
	FirstName    string `json:"first_name"`
	LastName     string `json:"last_name"`
	Username     string `json:"username"`
	LanguageCode string `json:"language_code"`
	IsPremium    bool   `json:"is_premium"`
	PhotoUrl     string `json:"photo_url"`
}

User describes user information: https://core.telegram.org/bots/webapps#webappuser

Jump to

Keyboard shortcuts

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