go-tdlib/README.md

150 lines
3.7 KiB
Markdown
Raw Normal View History

2018-08-30 14:55:42 +00:00
# go-tdlib
2021-12-30 05:55:02 +00:00
Go wrapper for [TDLib (Telegram Database Library)](https://github.com/tdlib/td) with full support of TDLib v1.8.0
2018-08-30 14:55:42 +00:00
## TDLib installation
2022-01-04 05:16:52 +00:00
Use [TDLib build instructions](https://tdlib.github.io/td/build.html) with checkmarked `Install built TDLib to /usr/local instead of placing the files to td/tdlib`.
2022-01-04 04:59:32 +00:00
### 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:
```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
```
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 authorizer
authorizer := client.ClientAuthorizer()
2018-10-10 13:35:28 +00:00
go client.CliInteractor(authorizer)
2019-09-10 12:36:54 +00:00
2018-08-30 14:55:42 +00:00
// or bot authorizer
2019-09-10 12:36:54 +00:00
// botToken := "000000000:gsVCGG5YbikxYHC7bP5vRvmBqJ7Xz6vG6td"
// authorizer := client.BotAuthorizer(botToken)
2018-08-30 14:55:42 +00:00
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,
}
2022-01-27 05:59:01 +00:00
_, err := client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{
NewVerbosityLevel: 1,
})
if err != nil {
log.Fatalf("SetLogVerbosityLevel error: %s", err)
}
tdlibClient, err := client.NewClient(authorizer)
2018-08-30 14:55:42 +00:00
if err != nil {
log.Fatalf("NewClient error: %s", err)
}
2019-09-10 12:36:54 +00:00
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)
2018-08-30 14:55:42 +00:00
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
2019-09-10 12:36:54 +00:00
proxy := client.WithProxy(&client.AddProxyRequest{
2018-12-23 21:55:29 +00:00
Server: "1.1.1.1",
Port: 1080,
Enable: true,
Type: &client.ProxyTypeSocks5{
Username: "username",
Password: "password",
},
})
2019-09-10 12:36:54 +00:00
tdlibClient, err := client.NewClient(authorizer, proxy)
2018-12-23 21:55:29 +00:00
```
2023-03-24 02:29:41 +00:00
## Example
[Example application](https://github.com/zelenin/go-tdlib/tree/master/example)
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)