• Stars
    star
    380
  • Rank 112,229 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created about 6 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go wrapper for TDLib (Telegram Database Library)

go-tdlib

Go wrapper for TDLib (Telegram Database Library) with full support of TDLib v1.8.0

TDLib installation

Use TDLib build instructions with checkmarked Install built TDLib to /usr/local instead of placing the files to td/tdlib.

Note: Compatible with TDLib v1.8.0 only!

Windows

Build with environment variables:

CGO_CFLAGS=-IC:/path/to/tdlib/build/tdlib/include
CGO_LDFLAGS=-LC:/path/to/tdlib/build/tdlib/bin -ltdjson

Example for PowerShell:

$env:CGO_CFLAGS="-IC:/td/tdlib/include"; $env:CGO_LDFLAGS="-LC:/td/tdlib/bin -ltdjson"; go build -trimpath -ldflags="-s -w" -o demo.exe .\cmd\demo.go

Usage

Client

Register an application to obtain an api_id and api_hash

package main

import (
    "log"
    "path/filepath"

    "github.com/zelenin/go-tdlib/client"
)

func main() {
    // client authorizer
    authorizer := client.ClientAuthorizer()
    go client.CliInteractor(authorizer)

    // or bot authorizer
    // botToken := "000000000:gsVCGG5YbikxYHC7bP5vRvmBqJ7Xz6vG6td"
    // authorizer := client.BotAuthorizer(botToken)

    const (
        apiId   = 00000
        apiHash = "8pu9yg32qkuukj83ozaqo5zzjwhkxhnk"
    )

    authorizer.TdlibParameters <- &client.TdlibParameters{
        UseTestDc:              false,
        DatabaseDirectory:      filepath.Join(".tdlib", "database"),
        FilesDirectory:         filepath.Join(".tdlib", "files"),
        UseFileDatabase:        true,
        UseChatInfoDatabase:    true,
        UseMessageDatabase:     true,
        UseSecretChats:         false,
        ApiId:                  apiId,
        ApiHash:                apiHash,
        SystemLanguageCode:     "en",
        DeviceModel:            "Server",
        SystemVersion:          "1.0.0",
        ApplicationVersion:     "1.0.0",
        EnableStorageOptimizer: true,
        IgnoreFileNames:        false,
    }

	_, err := client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{
		NewVerbosityLevel: 1,
	})
	if err != nil {
		log.Fatalf("SetLogVerbosityLevel error: %s", err)
	}
	
    tdlibClient, err := client.NewClient(authorizer)
    if err != nil {
        log.Fatalf("NewClient error: %s", err)
    }

    optionValue, err := tdlibClient.GetOption(&client.GetOptionRequest{
        Name: "version",
    })
    if err != nil {
        log.Fatalf("GetOption error: %s", err)
    }

    log.Printf("TDLib version: %s", optionValue.(*client.OptionValueString).Value)

    me, err := tdlibClient.GetMe()
    if err != nil {
        log.Fatalf("GetMe error: %s", err)
    }

    log.Printf("Me: %s %s [%s]", me.FirstName, me.LastName, me.Username)
}

Receive updates

tdlibClient, err := client.NewClient(authorizer)
if err != nil {
    log.Fatalf("NewClient error: %s", err)
}

listener := tdlibClient.GetListener()
defer listener.Close()
 
for update := range listener.Updates {
    if update.GetClass() == client.ClassUpdate {
        log.Printf("%#v", update)
    }
}

Proxy support

proxy := client.WithProxy(&client.AddProxyRequest{
    Server: "1.1.1.1",
    Port:   1080,
    Enable: true,
    Type: &client.ProxyTypeSocks5{
        Username: "username",
        Password: "password",
    },
})

tdlibClient, err := client.NewClient(authorizer, proxy)

Example

Example application

cd example
docker build --network host --build-arg TD_TAG=v1.8.0 --tag tdlib-test .
docker run --rm -it -e "API_ID=00000" -e "API_HASH=abcdef0123456789" tdlib-test ash
./app

Notes

  • WIP. Library API can be changed in the future
  • The package includes a .tl-parser and generated json-schema for creating libraries in other languages

Author

Aleksandr Zelenin, e-mail: [email protected]

More Repositories

1

yii2-semantic-ui

Semantic UI extension for Yii2
PHP
112
star
2

yii2-i18n-module

Yii2 i18n (internalization) module makes the translation of your application so simple
PHP
85
star
3

sms_ru

PHP-класс для работы с api сервиса sms.ru
PHP
85
star
4

telegram-bot-api

Telegram Bot API Client
PHP
59
star
5

yii2-slug-behavior

Yii2 slug behavior
PHP
51
star
6

yii2-rss

Yii2 RSS extension adds RSS-feed to your site
PHP
43
star
7

http-client

PSR-18 compatible HTTP client with middleware support
PHP
33
star
8

ddd-core

Core for Domain-driven design concepts
PHP
26
star
9

RSS-Generator

PHP class for generating of RSS feeds. Full support of [RSSboard specification](http://www.rssboard.org/rss-specification)
PHP
26
star
10

elo

A PHP implementation of Elo rating system
PHP
25
star
11

yii2-rbac-module

Yii2 RBAC module with generating assignments to DB from RBAC data store file rbac.php
PHP
24
star
12

go-mediainfo

Go bindings for MediaInfo
Go
15
star
13

go-glicko2

A Go implementation of Glicko2 rating system
Go
11
star
14

value-object

Collection of Value Objects
PHP
10
star
15

glicko2

A PHP implementation of Glicko2 rating system
PHP
8
star
16

zend-expressive-config

Zend Expressive config manager
PHP
7
star
17

yii2-semantic-ui-demo

Semantic UI extension for Yii2 demo
PHP
6
star
18

string

String transformers collection
PHP
5
star
19

grabot

Go wrapper for Telegram Bot API
Go
4
star
20

go-router

Deadly simple router
Go
3
star
21

hydrator

Hydrator/extractor
PHP
3
star
22

tdlib

Dockerfile
2
star
23

tdlib-alpine

Shell
2
star
24

message-bus

Lightweight message bus
PHP
2
star
25

go-actions

Dockerfile
1
star
26

go-worker-pool

Deadly simple worker pool
Go
1
star
27

go-mod-proxy

Go modules proxy boilerplate
Go
1
star