Skip to content

Golang bindings for TDLib (Telegram MTProto library)

License

Notifications You must be signed in to change notification settings

rutsky/go-tdjson

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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: tdlib/td#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)
			}
		}
	}
}

About

Golang bindings for TDLib (Telegram MTProto library)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%