gotgproto

package module
v1.0.0-my-beta14 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: GPL-3.0 Imports: 22 Imported by: 0

README

GoTGProto

GoTGProto is a helper package for gotd library, It aims to make td's raw functions easy-to-use with the help of features like using session strings, custom helper functions, storing peers and extracting chat or user ids through it etc.

We have an outstanding userbot project going on with GoTGProto, you can check it out by clicking here.

You can use this package to create bots and userbots with Telegram MTProto easily in golang, for any futher help you can check out the documentations or reach us through the following:

  • Updates Channel: Channel
  • Support Chat: Chat

Go Reference GPLv3 license

Note: This library is in the beta stage yet and may not be stable for every case.

Installation

You can download the library with the help of standard go get command.

go get github.com/celestix/gotgproto

Usage

You can find various examples in the examples' directory, one of them i.e. authorizing as a user is as follows:

package main

import (
	"log"
	
	"github.com/celestix/gotgproto"
	"github.com/celestix/gotgproto/sessionMaker"
)

func main() {
	clientType := gotgproto.ClientType{
		Phone: "PHONE_NUMBER_HERE",
	}
	client, err := gotgproto.NewClient(
		// Get AppID from https://my.telegram.org/apps
		123456,
		// Get ApiHash from https://my.telegram.org/apps
		"API_HASH_HERE",
		// ClientType, as we defined above
		clientType,
		// Optional parameters of client
		&gotgproto.ClientOpts{
			Session: sessionMaker.NewSession("echobot", sessionMaker.Session),
		},
	)
	if err != nil {
		log.Fatalln("failed to start client:", err)
	}
	client.Idle()
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update the examples as appropriate.

License

GPLv3
Licensed Under GNU General Public License v3

Documentation

Index

Constants

View Source
const VERSION = "v1.0.0-beta13"

Variables

This section is empty.

Functions

func IfAuthNecessary

func IfAuthNecessary(c *auth.Client, ctx context.Context, flow Flow) error

Types

type Client

type Client struct {
	// Dispatcher handlers the incoming updates and execute mapped handlers. It is recommended to use dispatcher.MakeDispatcher function for this field.
	Dispatcher dispatcher.Dispatcher
	// PublicKeys of telegram.
	//
	// If not provided, embedded public keys will be used.
	PublicKeys []telegram.PublicKey
	// DC ID to connect.
	//
	// If not provided, 2 will be used by default.
	DC int
	// DCList is initial list of addresses to connect.
	DCList dcs.List
	// Resolver to use.
	Resolver dcs.Resolver
	// Whether to show the copyright line in console or no.
	DisableCopyright bool
	// Logger is instance of zap.Logger. No logs by default.
	Logger *zap.Logger

	// Self contains details of logged in user in the form of *tg.User.
	Self *tg.User

	// Code for the language used on the device's OS, ISO 639-1 standard.
	SystemLangCode string
	// Code for the language used on the client, ISO 639-1 standard.
	ClientLangCode string

	*telegram.Client
	// contains filtered or unexported fields
}

func NewClient

func NewClient(appId int, apiHash string, cType ClientType, opts *ClientOpts) (*Client, error)

NewClient creates a new gotgproto client and logs in to telegram.

func (*Client) CreateContext

func (c *Client) CreateContext() *ext.Context

CreateContext creates a new pseudo updates context. A context retrieved from this method should be reused.

func (*Client) ExportStringSession

func (c *Client) ExportStringSession() (string, error)

ExportStringSession EncodeSessionToString encodes the client session to a string in base64.

Note: You must not share this string with anyone, it contains auth details for your logged in account.

func (*Client) Idle

func (c *Client) Idle() error

Idle keeps the current goroutined blocked until the client is stopped.

func (*Client) RefreshContext

func (c *Client) RefreshContext(ctx *ext.Context)

RefreshContext casts the new context.Context and telegram session to ext.Context (It may be used after doing Stop and Start calls respectively.)

func (*Client) Start

func (c *Client) Start(opts *ClientOpts) error

Start connects the client to telegram servers and logins. It will return error if the client is already running.

func (*Client) Stop

func (c *Client) Stop()

Stop cancels the context.Context being used for the client and stops it.

Notes:

1.) Client.Idle() will exit if this method is called.

2.) You can call Client.Start() to start the client again if it was stopped using this method.

type ClientOpts

type ClientOpts struct {
	// Logger is instance of zap.Logger. No logs by default.
	Logger *zap.Logger
	// PublicKeys of telegram.
	//
	// If not provided, embedded public keys will be used.
	PublicKeys []telegram.PublicKey
	// DC ID to connect.
	//
	// If not provided, 2 will be used by default.
	DC int
	// DCList is initial list of addresses to connect.
	DCList dcs.List
	// Resolver to use.
	Resolver dcs.Resolver
	// Whether to show the copyright line in console or no.
	DisableCopyright bool
	// Session info of the authenticated user, use sessionMaker.NewSession function to fill this field.
	Session *sessionMaker.SessionName
	// Setting this field to true will lead to automatically fetch the reply_to_message for a new message update.
	//
	// Set to `false` by default.
	AutoFetchReply bool
	// Code for the language used on the device's OS, ISO 639-1 standard.
	SystemLangCode string
	// Code for the language used on the client, ISO 639-1 standard.
	ClientLangCode string
	// Custom client device
	Device *telegram.DeviceConfig
	// Panic handles all the panics that occur during handler execution.
	PanicHandler dispatcher.PanicHandler
	// Error handles all the unknown errors which are returned by the handler callback functions.
	ErrorHandler dispatcher.ErrorHandler
	// Custom middlewares
	Middlewares []telegram.Middleware
	// Custom context(if you need to stop the client from running externally)
	Ctx func() (ctx context.Context, cancel context.CancelFunc)
}

type ClientType

type ClientType struct {
	// BotToken is the unique API Token for the bot you're trying to authorize, get it from @BotFather.
	BotToken string
	// Mobile number of the authenticating user.
	Phone string
}

Type of client to login to, can be of 2 types: 1.) Bot (Fill BotToken in this case) 2.) User (Fill Phone in this case)

type Flow

type Flow auth.Flow

Jump to

Keyboard shortcuts

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