From c54167dc400da7659b3c7c2b2fa85f02b93e8c42 Mon Sep 17 00:00:00 2001 From: Aleksandr Zelenin Date: Fri, 24 Mar 2023 05:29:41 +0300 Subject: [PATCH] add example --- README.md | 4 +++ example/Dockerfile | 85 ++++++++++++++++++++++++++++++++++++++++++++++ example/demo.go | 83 ++++++++++++++++++++++++++++++++++++++++++++ example/go.mod | 5 +++ example/go.sum | 2 ++ 5 files changed, 179 insertions(+) create mode 100644 example/Dockerfile create mode 100644 example/demo.go create mode 100644 example/go.mod create mode 100644 example/go.sum diff --git a/README.md b/README.md index 7941c6d..dcec17e 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,10 @@ tdlibClient, err := client.NewClient(authorizer, proxy) ``` +## Example + +[Example application](https://github.com/zelenin/go-tdlib/tree/master/example) + ## Notes * WIP. Library API can be changed in the future diff --git a/example/Dockerfile b/example/Dockerfile new file mode 100644 index 0000000..4fd7154 --- /dev/null +++ b/example/Dockerfile @@ -0,0 +1,85 @@ +FROM alpine:3.17 as tdlib-builder + +ENV LANG en_US.UTF-8 +ENV TZ UTC + +ARG TD_TAG + +RUN apk update && \ + apk upgrade && \ + apk add --update \ + build-base \ + ca-certificates \ + ccache \ + cmake \ + git \ + gperf \ + linux-headers \ + openssl-dev \ + php \ + php-ctype \ + readline-dev \ + zlib-dev && \ + git clone -b "${TD_TAG}" "https://github.com/tdlib/td.git" /src && \ + mkdir /src/build && \ + cd /src/build && \ + cmake \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_INSTALL_PREFIX:PATH=/usr/local \ + .. && \ + cmake --build . --target prepare_cross_compiling && \ + cd .. && \ + php SplitSource.php && \ + cd build && \ + cmake --build . --target install && \ + ls -lah /usr/local + + +FROM golang:1.20.2-alpine3.17 as go-builder + +ENV LANG en_US.UTF-8 +ENV TZ UTC + +RUN set -eux && \ + apk update && \ + apk upgrade && \ + apk add \ + bash \ + build-base \ + ca-certificates \ + curl \ + git \ + linux-headers \ + openssl-dev \ + zlib-dev + +WORKDIR /src + +COPY --from=tdlib-builder /usr/local/include/td /usr/local/include/td/ +COPY --from=tdlib-builder /usr/local/lib/libtd* /usr/local/lib/ +COPY . /src + +RUN go build \ + -a \ + -trimpath \ + -ldflags "-s -w" \ + -o app \ + "./demo.go" && \ + ls -lah + + +FROM alpine:3.17 + +ENV LANG en_US.UTF-8 +ENV TZ UTC + +RUN apk upgrade --no-cache && \ + apk add --no-cache \ + ca-certificates \ + libstdc++ + +WORKDIR /app + +COPY --from=go-builder /src/app . + +CMD ["./app"] diff --git a/example/demo.go b/example/demo.go new file mode 100644 index 0000000..472fbeb --- /dev/null +++ b/example/demo.go @@ -0,0 +1,83 @@ +package main + +import ( + "log" + "os" + "os/signal" + "path/filepath" + "strconv" + "syscall" + + "github.com/zelenin/go-tdlib/client" +) + +func main() { + authorizer := client.ClientAuthorizer() + go client.CliInteractor(authorizer) + + var ( + apiIdRaw = os.Getenv("API_ID") + apiHash = os.Getenv("API_HASH") + ) + + apiId64, err := strconv.ParseInt(apiIdRaw, 10, 32) + if err != nil { + log.Fatalf("strconv.Atoi error: %s", err) + } + + apiId := int32(apiId64) + + 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, + } + + _, err = client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{ + NewVerbosityLevel: 1, + }) + if err != nil { + log.Fatalf("SetLogVerbosityLevel error: %s", err) + } + + tdlibClient, err := client.NewClient(authorizer) + if err != nil { + log.Fatalf("NewClient error: %s", err) + } + + 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) + + 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) + + ch := make(chan os.Signal, 2) + signal.Notify(ch, os.Interrupt, syscall.SIGTERM) + go func() { + <-ch + tdlibClient.Stop() + os.Exit(1) + }() +} diff --git a/example/go.mod b/example/go.mod new file mode 100644 index 0000000..6e21b65 --- /dev/null +++ b/example/go.mod @@ -0,0 +1,5 @@ +module go-tdlib-demo + +go 1.20 + +require github.com/zelenin/go-tdlib v0.6.0 // indirect diff --git a/example/go.sum b/example/go.sum new file mode 100644 index 0000000..b00f97d --- /dev/null +++ b/example/go.sum @@ -0,0 +1,2 @@ +github.com/zelenin/go-tdlib v0.6.0 h1:dmdaPYcluNPiVuagM7D2FPT/nLYftTmqeKndz30vSDM= +github.com/zelenin/go-tdlib v0.6.0/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=