Go wrapper for TDLib (Telegram Database Library)
Go to file
Aleksandr Zelenin 894d79eed3 options fix
2018-12-24 00:45:16 +03:00
client options fix 2018-12-24 00:45:16 +03:00
cmd go fmt formatted project 2018-10-23 15:49:10 +03:00
codegen go fmt formatted project 2018-10-23 15:49:10 +03:00
data schema improvement 2018-10-19 21:06:07 +03:00
tlparser go fmt formatted project 2018-10-23 15:49:10 +03:00
.gitignore init 2018-08-30 17:55:42 +03:00
go.mod init 2018-08-30 17:55:42 +03:00
Makefile Makefile fix 2018-10-23 15:58:00 +03:00
README.md Fedora instructions added 2018-11-08 10:52:13 +03:00

go-tdlib

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

TDLib installation

Ubuntu 18.04 / Debian 9

Precompiled

Debian:

su
apt update
apt install -y apt-transport-https curl gnupg
curl "https://repo.zelenin.pw/gpg.key" | apt-key add -
echo "deb [arch=amd64] https://repo.zelenin.pw common contrib" | tee "/etc/apt/sources.list.d/tdlib.list"
apt update
apt install -y tdlib-dev

Ubuntu:

sudo apt update
sudo apt install -y apt-transport-https curl gnupg
curl "https://repo.zelenin.pw/gpg.key" | sudo apt-key add -
echo "deb [arch=amd64] https://repo.zelenin.pw common contrib" | sudo tee "/etc/apt/sources.list.d/tdlib.list"
sudo apt update
sudo apt install -y tdlib-dev

Fedora:

sudo dnf update
sudo dnf install tdlib-static

Manual compilation

apt-get update -y
apt-get install -y \
    build-essential \
    ca-certificates \
    ccache \
    cmake \
    git \
    gperf \
    libssl-dev \
    libreadline-dev \
    zlib1g-dev
git clone --depth 1 -b "v1.3.0" "https://github.com/tdlib/td.git" ./tdlib-src
mkdir ./tdlib-src/build
cd ./tdlib-src/build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build .
make install
rm -rf ./../../tdlib-src

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.SetLogVerbosityLevel(1)
    
    // 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,
    }

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

    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)
    }
}

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: aleksandr@zelenin.me