Giter Site home page Giter Site logo

go-tdjson's Introduction

⚠️ DEPRECATION NOTICE

Please, consider this small library as early proof of concept. I wrote it right after TDLib was released. There are more mature libraries currently, I personally can highly recommend this one from @zelenin. I use it in my private projects and can definitely say that it's battle-ready.

Golang bindings for TDLib (Telegram MTProto library)

GoDoc

In addition to the built-in methods:

  • Destroy()
  • Execute()
  • Receive()
  • Send()
  • SetFilePath()
  • SetLogVerbosityLevel()

It also has two interesting methods:

  • Auth()
  • SendAndCatch()

Linking statically against TDLib

I recommend you to link it statically if you don't want compile TDLib on production (don't forget that it requires at least 8GB of RAM).
To do that, just build your source with tag tdjson_static: go build -tags tdjson_static
For more details read this issue: tdlib/td#8

Example

package main

import (
	"github.com/L11R/go-tdjson"
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"
)

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

	var params []tdjson.Option = []tdjson.Option{
		tdjson.WithMessageDatabase(),
		tdjson.WithStorageOptimizer(),
	}

	// Get API_ID and API_HASH from env vars
	apiId := os.Getenv("API_ID")
	if apiId == "" {
		log.Fatal("API_ID env variable not specified")
	}
	params = append(params, tdjson.WithID(apiId))

	apiHash := os.Getenv("API_HASH")
	if apiHash == "" {
		log.Fatal("API_HASH env variable not specified")
	}
	params = append(params, tdjson.WithHash(apiHash))

	// Create new instance of client
	client := tdjson.NewClient(params...)

	// Handle Ctrl+C
	ch := make(chan os.Signal, 2)
	signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
	go func() {
		<-ch
		client.Destroy()
		os.Exit(1)
	}()

	// Main loop
	for update := range client.Updates {
		// Show all updates
		fmt.Println(update)

		// Authorization block
		if update["@type"].(string) == "updateAuthorizationState" {
			if authorizationState, ok := update["authorization_state"].(tdjson.Update)["@type"].(string); ok {
				res, err := client.Auth(authorizationState)
				if err != nil {
					log.Println(err)
				}
				log.Println(res)
			}
		}
	}
}

go-tdjson's People

Contributors

savely-krasovsky avatar shelomentsevd avatar rutsky avatar

Stargazers

Arthur Petukhovsky avatar bagher sohrabi avatar lfoder avatar  avatar  avatar  avatar Vasiliy Bukharev avatar Ashish Kumar avatar Vadim Kulibaba avatar  avatar RememberTheAir avatar  avatar Sasha Savchuk avatar Arman Safikhani avatar Aleksandr Murin avatar Dmytro Chornyi avatar ɥnsʞʎ avatar  avatar Ahmadreza Zibaei avatar Anna Prosvetova avatar Victor avatar  avatar

Watchers

James Cloos avatar  avatar ɥnsʞʎ avatar  avatar

go-tdjson's Issues

error while loading shared libraries: libtdjson.so.1.4.0

Hi!
I try to build my simple example using your lib.
But I get this error:
/tmp/___client: error while loading shared libraries: libtdjson.so.1.4.0: cannot open shared object file: No such file or directory
I've installed tdLib into /usr/local by this instruction https://tdlib.github.io/td/build.html?language=Go

this is my /usr/local/lib/:

vs@vs-pc:~$ ls -l /usr/local/lib/
total 377700
drwxr-xr-x 10 root root       4096 авг 12 11:39 cmake
lrwxrwxrwx  1 root root         16 янв 25  2019 libbson-1.0.so -> libbson-1.0.so.0
lrwxrwxrwx  1 root root         20 янв 25  2019 libbson-1.0.so.0 -> libbson-1.0.so.0.0.0
-rw-r--r--  1 root root     254176 янв 25  2019 libbson-1.0.so.0.0.0
-rw-r--r--  1 root root     236384 янв 25  2019 libbsoncxx-static.a
-rw-r--r--  1 root root     399366 янв 25  2019 libbson-static-1.0.a
lrwxrwxrwx  1 root root         18 янв 25  2019 libmongoc-1.0.so -> libmongoc-1.0.so.0
lrwxrwxrwx  1 root root         22 янв 25  2019 libmongoc-1.0.so.0 -> libmongoc-1.0.so.0.0.0
-rw-r--r--  1 root root     646496 янв 25  2019 libmongoc-1.0.so.0.0.0
-rw-r--r--  1 root root    1189848 янв 25  2019 libmongoc-static-1.0.a
-rw-r--r--  1 root root    1387920 янв 25  2019 libmongocxx-static.a
-rw-r--r--  1 root root   82941288 мая  6 12:51 libprotobuf.a
-rwxr-xr-x  1 root root        976 мая  6 12:51 libprotobuf.la
-rw-r--r--  1 root root   13223020 мая  6 12:51 libprotobuf-lite.a
-rwxr-xr-x  1 root root       1011 мая  6 12:51 libprotobuf-lite.la
lrwxrwxrwx  1 root root         26 мая  6 12:51 libprotobuf-lite.so -> libprotobuf-lite.so.18.0.1
lrwxrwxrwx  1 root root         26 мая  6 12:51 libprotobuf-lite.so.18 -> libprotobuf-lite.so.18.0.1
-rwxr-xr-x  1 root root    5178240 мая  6 12:51 libprotobuf-lite.so.18.0.1
lrwxrwxrwx  1 root root         21 мая  6 12:51 libprotobuf.so -> libprotobuf.so.18.0.1
lrwxrwxrwx  1 root root         21 мая  6 12:51 libprotobuf.so.18 -> libprotobuf.so.18.0.1
-rwxr-xr-x  1 root root   32565952 мая  6 12:51 libprotobuf.so.18.0.1
-rw-r--r--  1 root root  123348848 мая  6 12:51 libprotoc.a
-rwxr-xr-x  1 root root        992 мая  6 12:51 libprotoc.la
lrwxrwxrwx  1 root root         19 мая  6 12:51 libprotoc.so -> libprotoc.so.18.0.1
lrwxrwxrwx  1 root root         19 мая  6 12:51 libprotoc.so.18 -> libprotoc.so.18.0.1
-rwxr-xr-x  1 root root   42379352 мая  6 12:51 libprotoc.so.18.0.1
-rw-r--r--  1 root root     320774 авг 12 11:17 libtdactor.a
-rw-r--r--  1 root root     209306 авг 12 11:27 libtdclient.a
-rw-r--r--  1 root root   51701210 авг 12 11:27 libtdcore.a
-rw-r--r--  1 root root     702368 авг 12 11:17 libtddb.a
-rw-r--r--  1 root root    4929100 авг 12 11:28 libtdjson_private.a
lrwxrwxrwx  1 root root         18 авг 12 11:39 libtdjson.so -> libtdjson.so.1.4.0
-rw-r--r--  1 root root   19893432 авг 12 11:28 libtdjson.so.1.4.0
-rw-r--r--  1 root root       8336 авг 12 11:28 libtdjson_static.a
-rw-r--r--  1 root root    1420440 авг 12 11:18 libtdnet.a
-rw-r--r--  1 root root    1788116 авг 12 11:16 libtdsqlite.a
-rw-r--r--  1 root root    1957448 авг 12 11:17 libtdutils.a
drwxr-xr-x  3 root root       4096 окт  1  2018 node_modules
drwxr-xr-x  2 root root       4096 мая  6 12:51 pkgconfig
drwxrwsr-x  4 root staff      4096 июн 14 09:20 python2.7
drwxrwsr-x  3 root staff      4096 апр 27  2018 python3.6

I have no idea how to fix this problem

Documentation with examples?

Hi, Possible to add some documentation along with example code on how to do basic operations like:

  • login
  • register
  • send message
  • read updates

I am a beginner with Go and documentation of tdlib is also hard to decipher coming from just PHP/JS background. I understand JSON interface is the only option to interact with it but its hard to understand that without an HTTP context, given my background.

There is no documentation on how tdlib behaves, where authorized sessions are maintained etc either so I don't even know where to began to put both of the projects together to create something meaningful. Trying to read source code to understand alone but the situation feels hopeless. Any pointers to what I should be learning would be appreciated :)

Error while installing the library

go get github.com/L11R/go-tdjson

# github.com/L11R/go-tdjson
In file included from ../../../.go/src/github.com/L11R/go-tdjson/tdjson.go:6:0:
./td/telegram/td_log.h:61:1: error: unknown type name 'using'; did you mean 'uint'?
 using td_log_fatal_error_callback_ptr = void (*)(const char *error_message);
 ^~~~~
 uint
./td/telegram/td_log.h:61:41: error: expected expression before 'void'
 using td_log_fatal_error_callback_ptr = void (*)(const char *error_message);
                                         ^~~~
./td/telegram/td_log.h:72:52: error: expected declaration specifiers or '...' before 'td_log_fatal_error_callback_ptr'
 TDJSON_EXPORT void td_set_log_fatal_error_callback(td_log_fatal_error_callback_ptr callback);
                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

With the latest version of tdlib, builded following the instraction from the README and installed with make install

Not working, "exit status 1"

Hi @L11R

I'm building the td libraries using this docker file

FROM ubuntu as builder
LABEL Name=tgcli-builer Version=0.0.1

RUN apt-get update
RUN apt-get install build-essential -y
RUN apt-get install libssl-dev -y
RUN apt-get install cmake -y
RUN apt-get install gperf -y
RUN apt-get install zlib1g-dev -y

WORKDIR /
COPY ./td /td

RUN ls
WORKDIR /td
RUN mkdir build
WORKDIR build
RUN cmake ..
RUN cmake --build .
RUN cmake --build . --target install

FROM ubuntu
LABEL Name=tdcli Version=0.0.1

RUN apt-get update
RUN apt-get install libssl1.0.0 -y
RUN apt-get install gperf -y
RUN apt-get install zlib1g -y

COPY --from=builder /usr/local/lib/libtdjson.so /usr/local/lib/libtdjson.so
COPY --from=builder /usr/local/lib/libtdjson_static.a /usr/local/lib/libtdjson_static.a
COPY --from=builder /usr/local/lib/libtdjson_private.a /usr/local/lib/libtdjson_private.a
COPY --from=builder /usr/local/lib/libtdclient.a /usr/local/lib/libtdclient.a
COPY --from=builder /usr/local/lib/libtdcore.a /usr/local/lib/libtdcore.a
COPY --from=builder /usr/local/lib/cmake/Td/TdTargets.cmake /usr/local/lib/cmake/Td/TdTargets.cmake
COPY --from=builder /usr/local/lib/cmake/Td/TdTargets-noconfig.cmake /usr/local/lib/cmake/Td/TdTargets-noconfig.cmake
COPY --from=builder /usr/local/include/td/telegram/td_json_client.h /usr/local/include/td/telegram/td_json_client.h
COPY --from=builder /usr/local/include/td/telegram/td_log.h /usr/local/include/td/telegram/td_log.h
COPY --from=builder /usr/local/include/td/telegram/tdjson_export.h /usr/local/include/td/telegram/tdjson_export.h
COPY --from=builder /usr/local/include/td/telegram/Client.h /usr/local/include/td/telegram/Client.h
COPY --from=builder /usr/local/include/td/telegram/Log.h /usr/local/include/td/telegram/Log.h
COPY --from=builder /usr/local/include/td/tl/TlObject.h /usr/local/include/td/tl/TlObject.h
COPY --from=builder /usr/local/include/td/telegram/td_api.h /usr/local/include/td/telegram/td_api.h
COPY --from=builder /usr/local/include/td/telegram/td_api.hpp /usr/local/include/td/telegram/td_api.hpp
COPY --from=builder /usr/local/lib/cmake/Td/TdConfig.cmake /usr/local/lib/cmake/Td/TdConfig.cmake
COPY --from=builder /usr/local/lib/cmake/Td/TdConfigVersion.cmake /usr/local/lib/cmake/Td/TdConfigVersion.cmake
COPY --from=builder /usr/local/lib/libtdutils.a /usr/local/lib/libtdutils.a
COPY --from=builder /usr/local/lib/libtdactor.a /usr/local/lib/libtdactor.a
COPY --from=builder /usr/local/lib/libtdnet.a /usr/local/lib/libtdnet.a
COPY --from=builder /usr/local/lib/libtdsqlite.a /usr/local/lib/libtdsqlite.a
COPY --from=builder /usr/local/lib/libtddb.a /usr/local/lib/libtddb.a

#Clean
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

The next steps are:

  • Copy libraries to my local machine
  • sudo ldconfig
  • Build go using my local ubuntu machine
  • Run Local FAIL "exit status 1"
  • Copy executable to docker dinamicaly
  • Run in docker FAIL "no output"

If I compile "hello-world" it works properly in both places. so the architecture is the same.

it's hard to figure out the problem with the only output is "exit status 1"
any idea?

Thanks!

Potential race condition between Receive and Execute

According to tdlib documentation td_json_client_receive() and td_json_client_execute() share control on returned string buffer memory allocation and deallocation:

Returned pointer will be deallocated by TDLib during next call to td_json_client_receive or td_json_client_execute in the same thread, so it can't be used after that.

Returned by those methods string must be consumed (e.g. copied) before next call to td_json_client_receive or td_json_client_execute.

Currently Receive and Execute go-tdjson's methods doesn't serialize calls to td_json_client_receive and td_json_client_execute methods, which may result in accessing already freed memory.

Here is an proof of concept that this may happen with cgo and go scheduler (I haven't found simple and clear doc stating that this behavior is possible/expected): https://gist.github.com/rutsky/69d7055e64a3080fbb6853b54dcf1cef#file-cgo-go

Output:

$ go version
go version go1.10.1 linux/amd64
$ go run cgo.go 
panic: Iteration 39068207 - receive: "y" != "\x00"

goroutine 5 [running]:
main.main.func1()
        /home/bob/stuff/go/playground/cgo.go:35 +0x156
created by main.main
        /home/bob/stuff/go/playground/cgo.go:28 +0x47
exit status 2
$ go run cgo.go 
panic: Iteration 44789915 - execute: "y" != "\xd0"

goroutine 1 [running]:
main.main()
        /home/bob/stuff/go/playground/cgo.go:46 +0x16e
exit status 2
$ go run cgo.go 
panic: Iteration 1963975 - receive: "y" != "\x00"

goroutine 5 [running]:
main.main.func1()
        /home/bob/stuff/go/playground/cgo.go:35 +0x156
created by main.main
        /home/bob/stuff/go/playground/cgo.go:28 +0x47
exit status 2
$ go run cgo.go 
panic: Iteration 56646123 - execute: "y" != "\xd0"

goroutine 1 [running]:
main.main()
        /home/bob/stuff/go/playground/cgo.go:46 +0x16e
exit status 2
$

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.