Giter VIP home page Giter VIP logo

go-tdlib's Introduction

go-tdlib

Golang Telegram Database library Wrapper

Requirement:

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

AuthorizationFlow for phone numbers:

  1. Wait for authorizationStateWaitTdlibParameters and then call SetTdlibParameters
  2. Wait for authorizationStateWaitPhoneNumber and then call SetAuthenticationPhoneNumber
  3. Wait for authorizationStateWaitCode and then call CheckAuthenticationCode
  4. (If Already registered and 2factor is set) Wait for authorizationStateWaitPassword and then call CheckAuthenticationPassword
  5. (If not registered) Wait for authorizationStateWaitRegistration and then call RegisterUser

AuthorizationFlow for bot tokens:

  1. Wait for authorizationStateWaitTdlibParameters and then call SetTdlibParameters
  2. Wait for authorizationStateWaitPhoneNumber and then call CheckAuthenticationBotToken

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-Ubuntu in the repo to build an image for ubuntu:20.04 and golang v1.19 and then use that to deploy your code

go-tdlib's People

Contributors

aliforever avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.