tdjson

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2018 License: MIT Imports: 8 Imported by: 1

README

Golang bindings for TDLib (Telegram MTProto library)

GoDoc

In addition to the built-in methods:

  • Destroy()
  • Execute()
  • Receive()
  • Send()
  • SetFilePath()
  • SetLogVerbosityLevel()

It also has two interesting methods:

  • Auth()
  • SendAndCatch()

Linking statically against TDLib

I recommend you to link it statically if you don't want compile TDLib on production (don't forget that it requires at least 8GB of RAM).
To do that, just build your source with tag tdjson_static: go build -tags tdjson_static
For more details read this issue: https://github.com/tdlib/td/issues/8

Example

package main

import (
	"github.com/L11R/go-tdjson"
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	tdjson.SetLogVerbosityLevel(1)
	tdjson.SetFilePath("./errors.txt")

	var params []tdjson.Option = []tdjson.Option{
		tdjson.WithMessageDatabase(),
		tdjson.WithStorageOptimizer(),
	}

	// Get API_ID and API_HASH from env vars
	apiId := os.Getenv("API_ID")
	if apiId == "" {
		log.Fatal("API_ID env variable not specified")
	}
	params = append(params, tdjson.WithID(apiId))

	apiHash := os.Getenv("API_HASH")
	if apiHash == "" {
		log.Fatal("API_HASH env variable not specified")
	}
	params = append(params, tdjson.WithHash(apiHash))

	// Create new instance of client
	client := tdjson.NewClient(params...)

	// Handle Ctrl+C
	ch := make(chan os.Signal, 2)
	signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
	go func() {
		<-ch
		client.Destroy()
		os.Exit(1)
	}()

	// Main loop
	for update := range client.Updates {
		// Show all updates
		fmt.Println(update)

		// Authorization block
		if update["@type"].(string) == "updateAuthorizationState" {
			if authorizationState, ok := update["authorization_state"].(tdjson.Update)["@type"].(string); ok {
				res, err := client.Auth(authorizationState)
				if err != nil {
					log.Println(err)
				}
				log.Println(res)
			}
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetFilePath

func SetFilePath(path string)

Sets the path to the file to where the internal TDLib log will be written. By default TDLib writes logs to stderr or an OS specific log. Use this method to write the log to a file instead.

func SetLogVerbosityLevel

func SetLogVerbosityLevel(level int)

Sets the verbosity level of the internal logging of TDLib. By default the TDLib uses a verbosity level of 5 for logging.

Types

type Client

type Client struct {
	Client  unsafe.Pointer
	Updates chan Update
	// contains filtered or unexported fields
}

func NewClient

func NewClient(params ...Option) *Client

Creates a new instance of TDLib. Has two public fields: Client itself and Updates channel

func (*Client) Auth

func (c *Client) Auth(authorizationState string) (Update, error)

Method for interactive authorizations process, just provide it authorization state from updates and api credentials.

func (*Client) Destroy

func (c *Client) Destroy()

Destroys the TDLib client instance. After this is called the client instance shouldn't be used anymore.

func (*Client) Execute

func (c *Client) Execute(jsonQuery interface{}) Update

Synchronously executes TDLib request. Only a few requests can be executed synchronously.

func (*Client) Receive

func (c *Client) Receive(timeout float64) Update

Receives incoming updates and request responses from the TDLib client.

func (*Client) Send

func (c *Client) Send(jsonQuery interface{})

Sends request to the TDLib client. You can provide string or Update.

func (*Client) SendAndCatch

func (c *Client) SendAndCatch(jsonQuery interface{}) (Update, error)

Sends request to the TDLib client and catches the result in updates channel. You can provide string or Update.

type Option

type Option func(*options)

Changes parameters which will be used during execution Auth method with state authorizationStateWaitTdlibParameters.

func WithApplicationVersion

func WithApplicationVersion(version string) Option

Application version

func WithChatInfoDatabase

func WithChatInfoDatabase() Option

If set to true, the library will maintain a cache of users, basic groups, supergroups, channels and secret chats. Implies use WithFileDatabase()

func WithDatabaseDir

func WithDatabaseDir(path string) Option

The path to the directory for the persistent database; if empty, the current working directory will be used

func WithDeviceModel

func WithDeviceModel(model string) Option

Model of the device the application is being run on

func WithFileDatabase

func WithFileDatabase() Option

If set to true, information about downloaded and uploaded files will be saved between application restarts

func WithFilesDir

func WithFilesDir(path string) Option

The path to the directory for storing files; if empty, database_directory will be used

func WithHash

func WithHash(hash string) Option

Application identifier hash for Telegram API access, which can be obtained at https://my.telegram.org

func WithID

func WithID(id string) Option

Application identifier for Telegram API access, which can be obtained at https://my.telegram.org

func WithIgnoreFileNames

func WithIgnoreFileNames() Option

If set to true, original file names will be ignored. Otherwise, downloaded files will be saved under names as close as possible to the original name

func WithMessageDatabase

func WithMessageDatabase() Option

If set to true, the library will maintain a cache of chats and messages. Implies use WithChatInfoDatabase()

func WithPhone

func WithPhone(phone string) Option

Sets phone number for authorization

func WithSecretChats

func WithSecretChats() Option

If set to true, support for secret chats will be enabled

func WithStorageOptimizer

func WithStorageOptimizer() Option

If set to true, old files will automatically be deleted

func WithSystemLanguage

func WithSystemLanguage(lang string) Option

IETF language tag of the user's operating system language

func WithSystemVersion

func WithSystemVersion(system string) Option

Version of the operating system the application is being run on

func WithTestDC

func WithTestDC() Option

The Telegram test environment will be used instead of the production environment

type Update

type Update = map[string]interface{}

Jump to

Keyboard shortcuts

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