add example

This commit is contained in:
Aleksandr Zelenin 2023-03-24 05:29:41 +03:00
parent f44b2ff7f7
commit c54167dc40
5 changed files with 179 additions and 0 deletions

View file

@ -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

85
example/Dockerfile Normal file
View file

@ -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"]

83
example/demo.go Normal file
View file

@ -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)
}()
}

5
example/go.mod Normal file
View file

@ -0,0 +1,5 @@
module go-tdlib-demo
go 1.20
require github.com/zelenin/go-tdlib v0.6.0 // indirect

2
example/go.sum Normal file
View file

@ -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=