go-tdlib
December 10, 2023 ยท View on GitHub
Golang Telegram Database library Wrapper
Requirement:
- Build TDLib for Golang and your operating system. See a TDLib build instructions generator for detailed instructions on how to build TDLib
Install:
go get -u github.com/aliforever/go-tdlib
Important:
The library is ported from td_json_client_create to td_create_client_id. As the old interface "will be removed in TDLib 2.0.0"
In the new design there's a Manager struct that holds a map of channel for each client id. So you can have multiple clients in your application.
Usage:
package main
import (
"context"
"github.com/aliforever/go-tdlib"
"github.com/aliforever/go-tdlib/config"
"fmt"
)
func main() {
managerHandlers := tdlib.NewManagerHandlers().
SetRawIncomingEventHandler(func(eventBytes []byte) {
fmt.Println(string(eventBytes))
})
managerOptions := tdlib.NewManagerOptions().
SetLogVerbosityLevel(6).
SetLogPath("logs.txt")
// Or you can pass nil for both handlers and options
m := tdlib.NewManager(context.Background(), managerHandlers, managerOptions)
// NewClientOptions
cfg := config.New().
SetFilesDirectory("./tdlib/tdlib-files").
SetDatabaseDirectory("./tdlib/tdlib-db")
h := tdlib.NewHandlers().
SetRawIncomingEventHandler(func(eventBytes []byte) {
fmt.Println(string(eventBytes))
})
apiID := int64(123456)
apiHash := "a1234b1234c1234d1234f345667"
client := m.NewClient(apiID, apiHash, h, cfg, nil)
err := client.ReceiveUpdates(context.Background())
if err != nil {
panic(err)
}
}
AuthorizationFlow for phone numbers:
- Wait for
authorizationStateWaitTdlibParametersand then callSetTdlibParameters - Wait for
authorizationStateWaitPhoneNumberand then callSetAuthenticationPhoneNumber - Wait for
authorizationStateWaitCodeand then callCheckAuthenticationCode - (If Already registered and 2factor is set) Wait for
authorizationStateWaitPasswordand then callCheckAuthenticationPassword - (If not registered) Wait for
authorizationStateWaitRegistrationand then callRegisterUser
AuthorizationFlow for bot tokens:
- Wait for
authorizationStateWaitTdlibParametersand then callSetTdlibParameters - Wait for
authorizationStateWaitPhoneNumberand then callCheckAuthenticationBotToken
If the authorization flow is successful, the client will receive updateAuthorizationState with authorizationStateReady and then you can start using the client.
To register a handler for authorization state updates, pass your handler to SetOnUpdateAuthorizationStateEventHandler method of Handlers struct.
func authorizationHandler(update entities.AuthorizationStateType) {
switch update.AuthorizationState.GetAuthorizationStateEnum() {
case entities.AuthorizationStateTypeAwaitingTdlibParameters:
// ...
case entities.AuthorizationStateTypeAwaitingPhoneNumber:
// ...
case entities.AuthorizationStateTypeAwaitingCode:
// ...
case entities.AuthorizationStateTypeAwaitingPassword:
// ...
case entities.AuthorizationStateTypeAwaitingRegistration:
// ...
case entities.AuthorizationStateTypeReady:
// ...
}
}
Or you can use SetAuthorizationHandler to register your custom interface which implements following:
type AuthorizationHandler interface {
Process(client *TDLib, update entities.AuthorizationStateType)
}
Checkout 2 pre-defined handlers in authorizationhandler.go file.
UserAuthorizationHandler
BotAuthorizationHandler
Notes:
-
Build project by specifying installation path for the built TDLib as following:
go build -ldflags="-r /usr/local/lib" -
You can use
Dockerfile-Ubuntuin the repo to build an image for ubuntu:20.04 and golang v1.19 and then use that to deploy your code