Home

Awesome

go-tdlib

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->

All Contributors

<!-- ALL-CONTRIBUTORS-BADGE:END -->

Golang Telegram TdLib JSON bindings

Introduction

Telegram Tdlib is a complete library for creating telegram clients, it also has a simple tdjson ready-to-use library to ease the integration with different programming languages and platforms.

go-tdlib is a complete tdlib-tdjson binding package to help you create your own Telegram clients.

NOTE: basic tdjson-golang binding is inspired from this package: go-tdjson

All the classes and functions declared in Tdlib TypeLanguage schema file have been exported using the autogenerate tool tl-parser. So you can use every single type and method in Tdlib.

Key features:

Installation

First of all you need to clone the Tdlib repo and build it:

git clone git@github.com:tdlib/td.git --depth 1
cd td
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . -- -j5
make install

# -j5 refers to number of your cpu cores + 1 for multi-threaded build.

If hit any build errors, refer to Tdlib build instructions I'm using static linking against tdlib so it won't require to build the whole tdlib source files.

Docker

You can use the prebuilt tdlib image and Go image of your liking:

FROM golang:1.15-alpine AS golang

COPY --from=wcsiu/tdlib:1.7-alpine /usr/local/include/td /usr/local/include/td
COPY --from=wcsiu/tdlib:1.7-alpine /usr/local/lib/libtd* /usr/local/lib/
COPY --from=wcsiu/tdlib:1.7-alpine /usr/lib/libssl.a /usr/local/lib/libssl.a
COPY --from=wcsiu/tdlib:1.7-alpine /usr/lib/libcrypto.a /usr/local/lib/libcrypto.a
COPY --from=wcsiu/tdlib:1.7-alpine /lib/libz.a /usr/local/lib/libz.a
RUN apk add build-base

WORKDIR /myApp

COPY . .

RUN go build --ldflags "-extldflags '-static -L/usr/local/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdactor -ltddb -ltdsqlite -ltdnet -ltdutils -ldl -lm -lssl -lcrypto -lstdc++ -lz'" -o /tmp/getChats getChats.go

FROM gcr.io/distroless/base:latest
COPY --from=golang /tmp/getChats /getChats
ENTRYPOINT [ "/getChats" ]
$ docker build -fDockerfile -ttelegram-client .

Example

Here is a simple example for authorization and fetching updates:

package main

import (
	"fmt"

	"github.com/Arman92/go-tdlib"
)

func main() {
	tdlib.SetLogVerbosityLevel(1)
	tdlib.SetFilePath("./errors.txt")

	// Create new instance of client
	client := tdlib.NewClient(tdlib.Config{
		APIID:               "187786",
		APIHash:             "e782045df67ba48e441ccb105da8fc85",
		SystemLanguageCode:  "en",
		DeviceModel:         "Server",
		SystemVersion:       "1.0.0",
		ApplicationVersion:  "1.0.0",
		UseMessageDatabase:  true,
		UseFileDatabase:     true,
		UseChatInfoDatabase: true,
		UseTestDataCenter:   false,
		DatabaseDirectory:   "./tdlib-db",
		FileDirectory:       "./tdlib-files",
		IgnoreFileNames:     false,
	})

	for {
		currentState, _ := client.Authorize()
		if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPhoneNumberType {
			fmt.Print("Enter phone: ")
			var number string
			fmt.Scanln(&number)
			_, err := client.SendPhoneNumber(number)
			if err != nil {
				fmt.Printf("Error sending phone number: %v", err)
			}
		} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitCodeType {
			fmt.Print("Enter code: ")
			var code string
			fmt.Scanln(&code)
			_, err := client.SendAuthCode(code)
			if err != nil {
				fmt.Printf("Error sending auth code : %v", err)
			}
		} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPasswordType {
			fmt.Print("Enter Password: ")
			var password string
			fmt.Scanln(&password)
			_, err := client.SendAuthPassword(password)
			if err != nil {
				fmt.Printf("Error sending auth password: %v", err)
			}
		} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateReadyType {
			fmt.Println("Authorization Ready! Let's rock")
			break
		}
	}

	// Main loop
	rawUpdates := client.GetRawUpdatesChannel(100)
        for update := range rawUpdates {
		// Show all updates
		fmt.Println(update.Data)
		fmt.Print("\n\n")
	}

}

More examples can be found on examples folder

Contributors ✨

Thanks goes to these wonderful people (emoji key):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> <!-- prettier-ignore-start --> <!-- markdownlint-disable --> <table> <tr> <td align="center"><a href="https://github.com/zelenin"><img src="https://avatars0.githubusercontent.com/u/1427885?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Aleksandr Zelenin</b></sub></a><br /><a href="#example-zelenin" title="Examples">💡</a></td> <td align="center"><a href="https://github.com/for"><img src="https://avatars1.githubusercontent.com/u/7262123?v=4?s=100" width="100px;" alt=""/><br /><sub><b>for</b></sub></a><br /><a href="https://github.com/Arman92/go-tdlib/issues?q=author%3Afor" title="Bug reports">🐛</a></td> <td align="center"><a href="https://zibaei.net"><img src="https://avatars2.githubusercontent.com/u/16955684?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ahmadreza Zibaei</b></sub></a><br /><a href="https://github.com/Arman92/go-tdlib/commits?author=ahmdrz" title="Code">💻</a></td> <td align="center"><a href="https://github.com/aivel"><img src="https://avatars2.githubusercontent.com/u/3083784?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Max</b></sub></a><br /><a href="https://github.com/Arman92/go-tdlib/commits?author=aivel" title="Code">💻</a></td> <td align="center"><a href="https://rocketeer.be"><img src="https://avatars0.githubusercontent.com/u/42904?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ruben Vermeersch</b></sub></a><br /><a href="https://github.com/Arman92/go-tdlib/issues?q=author%3Arubenv" title="Bug reports">🐛</a></td> <td align="center"><a href="https://github.com/alxshelepenok"><img src="https://avatars2.githubusercontent.com/u/8599449?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Alexander Shelepenok</b></sub></a><br /><a href="https://github.com/Arman92/go-tdlib/commits?author=alxshelepenok" title="Code">💻</a> <a href="#maintenance-alxshelepenok" title="Maintenance">🚧</a> <a href="https://github.com/Arman92/go-tdlib/commits?author=alxshelepenok" title="Tests">⚠️</a></td> <td align="center"><a href="https://github.com/KarimNahas"><img src="https://avatars3.githubusercontent.com/u/15736459?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Karim Nahas</b></sub></a><br /><a href="https://github.com/Arman92/go-tdlib/commits?author=KarimNahas" title="Code">💻</a> <a href="#maintenance-KarimNahas" title="Maintenance">🚧</a> <a href="https://github.com/Arman92/go-tdlib/issues?q=author%3AKarimNahas" title="Bug reports">🐛</a></td> </tr> <tr> <td align="center"><a href="https://github.com/wcsiu"><img src="https://avatars0.githubusercontent.com/u/5212960?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Wachiu Siu</b></sub></a><br /><a href="#example-wcsiu" title="Examples">💡</a> <a href="https://github.com/Arman92/go-tdlib/issues?q=author%3Awcsiu" title="Bug reports">🐛</a> <a href="https://github.com/Arman92/go-tdlib/commits?author=wcsiu" title="Documentation">📖</a></td> <td align="center"><a href="https://github.com/motylkov"><img src="https://avatars1.githubusercontent.com/u/1659182?v=4?s=100" width="100px;" alt=""/><br /><sub><b>motylkov</b></sub></a><br /><a href="https://github.com/Arman92/go-tdlib/commits?author=motylkov" title="Code">💻</a></td> <td align="center"><a href="https://github.com/Juliia-b"><img src="https://avatars.githubusercontent.com/u/55066322?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Juliia-b</b></sub></a><br /><a href="https://github.com/Arman92/go-tdlib/issues?q=author%3AJuliia-b" title="Bug reports">🐛</a></td> </tr> </table> <!-- markdownlint-restore --> <!-- prettier-ignore-end --> <!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the all-contributors specification. Contributions of any kind welcome!