2018-08-30 14:55:42 +00:00
# go-tdlib
2018-09-05 21:45:29 +00:00
Go wrapper for [TDLib (Telegram Database Library) ](https://github.com/tdlib/td ) with full support of TDLib v1.3.0
2018-08-30 14:55:42 +00:00
## TDLib installation
### Ubuntu 18.04 / Debian 9
2018-09-03 22:55:13 +00:00
#### Precompiled
2018-09-26 16:24:49 +00:00
Debian:
2018-09-03 22:55:13 +00:00
```bash
2018-09-26 16:24:49 +00:00
su
2018-10-23 04:10:34 +00:00
apt update
apt install -y apt-transport-https curl gnupg
2018-09-03 22:55:13 +00:00
curl "https://repo.zelenin.pw/gpg.key" | apt-key add -
2018-09-26 16:24:49 +00:00
echo "deb [arch=amd64] https://repo.zelenin.pw common contrib" | tee "/etc/apt/sources.list.d/tdlib.list"
apt update
2018-10-23 04:10:34 +00:00
apt install -y tdlib-dev
2018-09-26 16:24:49 +00:00
```
Ubuntu:
```bash
2018-10-23 04:10:34 +00:00
sudo apt update
sudo apt install -y apt-transport-https curl gnupg
2018-09-26 16:24:49 +00:00
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
2018-10-23 04:10:34 +00:00
sudo apt install -y tdlib-dev
2018-09-03 22:55:13 +00:00
```
2018-11-08 07:52:13 +00:00
Fedora:
```bash
sudo dnf update
sudo dnf install tdlib-static
```
2018-09-03 22:55:13 +00:00
#### Manual compilation
2018-08-30 14:57:13 +00:00
```bash
2018-08-30 14:55:42 +00:00
apt-get update -y
apt-get install -y \
build-essential \
ca-certificates \
ccache \
cmake \
git \
gperf \
libssl-dev \
libreadline-dev \
zlib1g-dev
2018-09-05 21:45:29 +00:00
git clone --depth 1 -b "v1.3.0" "https://github.com/tdlib/td.git" ./tdlib-src
2018-08-30 14:55:42 +00:00
mkdir ./tdlib-src/build
cd ./tdlib-src/build
2018-09-17 10:14:40 +00:00
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build .
make install
2018-08-30 14:55:42 +00:00
rm -rf ./../../tdlib-src
2018-08-30 14:57:13 +00:00
```
2018-08-30 14:55:42 +00:00
## Usage
### Client
[Register an application ](https://my.telegram.org/apps ) to obtain an api_id and api_hash
```go
package main
import (
"log"
2018-10-09 03:14:59 +00:00
"path/filepath"
2018-08-30 14:55:42 +00:00
"github.com/zelenin/go-tdlib/client"
)
func main() {
client.SetLogVerbosityLevel(1)
// client authorizer
authorizer := client.ClientAuthorizer()
2018-10-10 13:35:28 +00:00
go client.CliInteractor(authorizer)
2018-08-30 14:55:42 +00:00
// or bot authorizer
botToken := "000000000:gsVCGG5YbikxYHC7bP5vRvmBqJ7Xz6vG6td"
authorizer := client.BotAuthorizer(botToken)
const (
apiId = 00000
apiHash = "8pu9yg32qkuukj83ozaqo5zzjwhkxhnk"
)
authorizer.TdlibParameters < - & client . TdlibParameters {
UseTestDc: false,
2018-10-09 03:14:59 +00:00
DatabaseDirectory: filepath.Join(".tdlib", "database"),
FilesDirectory: filepath.Join(".tdlib", "files"),
2018-08-30 14:55:42 +00:00
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
```go
2018-09-10 22:30:14 +00:00
tdlibClient, err := client.NewClient(authorizer)
2018-08-30 14:55:42 +00:00
if err != nil {
log.Fatalf("NewClient error: %s", err)
}
2018-09-10 22:30:14 +00:00
listener := tdlibClient.GetListener()
defer listener.Close()
for update := range listener.Updates {
if update.GetClass() == client.ClassUpdate {
log.Printf("%#v", update)
2018-08-30 14:55:42 +00:00
}
}
```
2018-12-23 21:55:29 +00:00
### Proxy support
```go
proxyOption := 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, proxyOption)
```
2018-08-30 14:55:42 +00:00
## Notes
* WIP. Library API can be changed in the future
* The package includes a .tl-parser and generated [json-schema ](https://github.com/zelenin/go-tdlib/tree/master/data ) for creating libraries in other languages
## Author
[Aleksandr Zelenin ](https://github.com/zelenin/ ), e-mail: [aleksandr@zelenin.me ](mailto:aleksandr@zelenin.me )