Create a Telegram client in Go using TDLib with Docker
After this tutorial, you will be able to build a telegram client with API to retrieve chat list from it in Go. This tutorial assume you have a basic understanding of Docker, how it works and most importantly having it installed in you machine. If you don’t, it would be great for you go learn some basic concepts about it first.
Before we start doing any programming, we need to create an app on Telegram first. For details, click here. After this, you should have your app_id and app_hash.
2 - The Client application
To build a telegram client, we need to use a library called TDLib. It is library written in C++ and so we can leverage cgo to write our client in Go.
go-tdlib is the goto Go library for TDLib. It is being actively maintained. This tutorial will be using it.
Coding part:
First, we need a TDLib client.
You will have to fill in your app_id and app_hash you get from the previous section. The rest we can leave it as default.
Then, we need to authorize our client.
The phone number should include country code.The above authorization code snippet allows you to authorize through password or security code in your client.
To get all the chat names, we need to get the list of chat IDs and get chat details one by one.
Finally, we add a HTTP router and HTTP handler for GET /getChats.
Combine them all the source code would be like this.
3 - Dockerfile
To build our Go application with TDLib, we would need all the dependencies. To save your time, I have already prepared the base image and the Dockerfile is here.
First, for base images, we need the dependencies and Go compiler.
Go Compiler:
Then, we put the dependencies in places as Arman92/go-tdlib wants them to be.
Dependencies:
We build the Go application.
You might be wondering why I am adding such a long value for ldflags. It is linker flag for C compiler. The flag provides locations of the C libraries we need to cgo to compile the application.
-static is a very important flag here. It tells the compiler to include the c libraries into the final Go executable instead of just linking the Go executable to them. It makes the executable can run without extra dependencies and our next step on minimizing the image size possible.
We move our client executable into a very minimal base image, distroless. With this extra stage, our image size shrink significantly.
We then expose port 3000 of the container for the HTTP router to be able to receive external call.
Final step, run the executable.
The complete Dockerfile:
We can then try to build the image and let’s call the image telegram-client-demo.
4 - Docker Compose
The main reason we use docker-compose here is just to keep the configuration in code.
However, I do want to talk about these two settings. They allow us to docker attach the running container to input through stdin for authorization.
The full docker-compose.yml.
To run the application, we can just this command.
5 - Not done yet but almost!
You may find yourself stuck.
You need to docker attach to the running container to do your first time authorization. After that, your credentials would in the ./dev/.
Just enter your account phone number with country code and press enter. Then follow the instructions until it is set.
We can now try to make a GET request to our container exposed port 3000.
6 - Debug
Besides the docker logs, there is error log file in ./dev/ with name errors.txt.
END
Congrats. You got a telegram client which can fetch all you chats.
If you find anything I can improve on this writting, feel free to leave your comments. I am still on my learning journey!