Golang Telegram Database library Wrapper
- Build TDLib for Golang and your operating system. See a TDLib build instructions generator for detailed instructions on how to build TDLib
go get -u github.com/aliforever/go-tdlib
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.
package main
import (
"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(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()
if err != nil {
panic(err)
}
}
- Wait for
authorizationStateWaitTdlibParameters
and then callSetTdlibParameters
- Wait for
authorizationStateWaitPhoneNumber
and then callSetAuthenticationPhoneNumber
- Wait for
authorizationStateWaitCode
and then callCheckAuthenticationCode
- (If Already registered and 2factor is set) Wait for
authorizationStateWaitPassword
and then callCheckAuthenticationPassword
- (If not registered) Wait for
authorizationStateWaitRegistration
and then callRegisterUser
- Wait for
authorizationStateWaitTdlibParameters
and then callSetTdlibParameters
- Wait for
authorizationStateWaitPhoneNumber
and 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
-
Build project by specifying installation path for the built TDLib as following:
go build -ldflags="-r /usr/local/lib"
-
You can use
Dockerfile-Ubuntu
in the repo to build an image for ubuntu:20.04 and golang v1.19 and then use that to deploy your code