commit
9c93ddf88a
12
README.md
12
README.md
|
@ -20,12 +20,12 @@ So you can use every single type and method in Tdlib.
|
|||
* Supports all tdjson functions: Send(), Execute(), Receive(), Destroy(), SetFilePath(), SetLogVerbosityLevel()
|
||||
* Supports all tdlib functions and types
|
||||
|
||||
## Installtion
|
||||
## Installation
|
||||
|
||||
First of all you need to clone the Tdlib repo and build it:
|
||||
```bash
|
||||
git clone git@github.com:tdlib/td.git
|
||||
cd tdlib
|
||||
cd td
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -DCMAKE_BUILD_TYPE=Release ..
|
||||
|
@ -38,6 +38,14 @@ make install
|
|||
If hit any build errors, refer to [Tdlib build instructions](https://github.com/tdlib/td#building)
|
||||
I'm using static linking against tdlib so it won't require to build the whole tdlib source files.
|
||||
|
||||
## Docker
|
||||
You can use prebuilt tdlib with following Docker image:
|
||||
|
||||
***Windows:***
|
||||
``` shell
|
||||
docker pull mihaildemidoff/tdlib-go
|
||||
```
|
||||
|
||||
## Example
|
||||
Here is a simple example for authorization and fetching updates:
|
||||
```golang
|
||||
|
|
|
@ -66,5 +66,4 @@ func main() {
|
|||
fmt.Println(update.Data)
|
||||
fmt.Print("\n\n")
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Arman92/go-tdlib"
|
||||
)
|
||||
|
||||
const botToken = "<your bot token>"
|
||||
|
||||
func main() {
|
||||
tdlib.SetLogVerbosityLevel(1)
|
||||
tdlib.SetFilePath("./errors.txt")
|
||||
|
||||
// Create new instance of client
|
||||
client := tdlib.NewClient(tdlib.Config{
|
||||
APIID: "187786",
|
||||
APIHash: "e782045df67ba48e441ccb105da8fc85",
|
||||
SystemLanguageCode: "en",
|
||||
DeviceModel: "Server",
|
||||
SystemVersion: "1.0.0",
|
||||
ApplicationVersion: "1.0.0",
|
||||
UseMessageDatabase: true,
|
||||
UseFileDatabase: true,
|
||||
UseChatInfoDatabase: true,
|
||||
UseTestDataCenter: false,
|
||||
DatabaseDirectory: "./tdlib-db",
|
||||
FileDirectory: "./tdlib-files",
|
||||
IgnoreFileNames: false,
|
||||
})
|
||||
|
||||
for {
|
||||
currentState, _ := client.Authorize()
|
||||
if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPhoneNumberType {
|
||||
_, err := client.CheckAuthenticationBotToken(botToken)
|
||||
if err != nil {
|
||||
fmt.Printf("Error check bot token: %v", err)
|
||||
return
|
||||
}
|
||||
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateReadyType {
|
||||
fmt.Println("Authorization Ready! Let's rock")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// rawUpdates gets all updates comming from tdlib
|
||||
rawUpdates := client.GetRawUpdatesChannel(100)
|
||||
for update := range rawUpdates {
|
||||
// Show all updates
|
||||
fmt.Println(update.Data)
|
||||
fmt.Print("\n\n")
|
||||
}
|
||||
}
|
|
@ -64,7 +64,7 @@ func main() {
|
|||
for newMsg := range receiver.Chan {
|
||||
fmt.Println(newMsg)
|
||||
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
|
||||
// We assume the message content is simple text: (should be more sophosticated for general use)
|
||||
// We assume the message content is simple text: (should be more sophisticated for general use)
|
||||
msgText := updateMsg.Message.Content.(*tdlib.MessageText)
|
||||
fmt.Println("MsgText: ", msgText.Text)
|
||||
fmt.Print("\n\n")
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/Arman92/go-tdlib"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tdlib.SetLogVerbosityLevel(1)
|
||||
tdlib.SetFilePath("./errors.txt")
|
||||
|
||||
// You can set username and password if needed
|
||||
socksProxy := tdlib.ProxySocks5{Server: "127.0.0.1", Port: 1112}
|
||||
|
||||
// Create new instance of client
|
||||
client := tdlib.NewClient(tdlib.Config{
|
||||
APIID: "187786",
|
||||
APIHash: "e782045df67ba48e441ccb105da8fc85",
|
||||
SystemLanguageCode: "en",
|
||||
DeviceModel: "Server",
|
||||
SystemVersion: "1.0.0",
|
||||
ApplicationVersion: "1.0.0",
|
||||
UseMessageDatabase: true,
|
||||
UseFileDatabase: true,
|
||||
UseChatInfoDatabase: true,
|
||||
UseTestDataCenter: false,
|
||||
DatabaseDirectory: "./tdlib-db",
|
||||
FileDirectory: "./tdlib-files",
|
||||
IgnoreFileNames: false,
|
||||
SocksProxy: &socksProxy, // Set the socks proxy
|
||||
})
|
||||
|
||||
// Handle Ctrl+C
|
||||
ch := make(chan os.Signal, 2)
|
||||
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
|
||||
go func() {
|
||||
<-ch
|
||||
client.DestroyInstance()
|
||||
os.Exit(1)
|
||||
}()
|
||||
|
||||
// Wait while we get AuthorizationReady!
|
||||
// Note: See authorization example for complete auhtorization sequence example
|
||||
currentState, _ := client.Authorize()
|
||||
for ; currentState.GetAuthorizationStateEnum() != tdlib.AuthorizationStateReadyType; currentState, _ = client.Authorize() {
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
}
|
||||
|
||||
// Send "/start" text every 5 seconds to Forsquare bot chat
|
||||
go func() {
|
||||
// Should get chatID somehow, check out "getChats" example
|
||||
chatID := int64(198529620) // Foursquare bot chat id
|
||||
|
||||
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText("/start", nil), true, true)
|
||||
client.SendMessage(chatID, 0, false, true, nil, inputMsgTxt)
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
}()
|
||||
|
||||
// rawUpdates gets all updates comming from tdlib
|
||||
rawUpdates := client.GetRawUpdatesChannel(100)
|
||||
for update := range rawUpdates {
|
||||
// Show all updates
|
||||
fmt.Println(update.Data)
|
||||
fmt.Print("\n\n")
|
||||
}
|
||||
|
||||
}
|
33
tdlib.go
33
tdlib.go
|
@ -1,8 +1,10 @@
|
|||
package tdlib
|
||||
|
||||
//#cgo linux CFLAGS: -I/usr/local/include
|
||||
//#cgo darwin CFLAGS: -I/usr/local/include
|
||||
//#cgo windows CFLAGS: -IC:/src/td -IC:/src/td/build
|
||||
//#cgo linux LDFLAGS: -L/usr/local/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdactor -ltddb -ltdsqlite -ltdnet -ltdutils -lstdc++ -lssl -lcrypto -ldl -lz -lm
|
||||
//#cgo darwin LDFLAGS: -L/usr/local/lib -L/usr/local/opt/openssl/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdactor -ltddb -ltdsqlite -ltdnet -ltdutils -lstdc++ -lssl -lcrypto -ldl -lz -lm
|
||||
//#cgo windows LDFLAGS: -LC:/src/td/build/Debug -ltdjson
|
||||
//#include <stdlib.h>
|
||||
//#include <td/telegram/td_json_client.h>
|
||||
|
@ -42,6 +44,7 @@ type EventReceiver struct {
|
|||
// Client is the Telegram TdLib client
|
||||
type Client struct {
|
||||
Client unsafe.Pointer
|
||||
Config Config
|
||||
rawUpdates chan UpdateMsg
|
||||
receivers []EventReceiver
|
||||
waiters sync.Map
|
||||
|
@ -57,15 +60,16 @@ type Config struct {
|
|||
SystemVersion string // Version of the operating system the application is being run on; must be non-empty.
|
||||
ApplicationVersion string // Application version; must be non-empty.
|
||||
// Optional fields
|
||||
UseTestDataCenter bool // if set to true, the Telegram test environment will be used instead of the production environment.
|
||||
DatabaseDirectory string // The path to the directory for the persistent database; if empty, the current working directory will be used.
|
||||
FileDirectory string // The path to the directory for storing files; if empty, database_directory will be used.
|
||||
UseFileDatabase bool // If set to true, information about downloaded and uploaded files will be saved between application restarts.
|
||||
UseChatInfoDatabase bool // If set to true, the library will maintain a cache of users, basic groups, supergroups, channels and secret chats. Implies use_file_database.
|
||||
UseMessageDatabase bool // If set to true, the library will maintain a cache of chats and messages. Implies use_chat_info_database.
|
||||
UseSecretChats bool // If set to true, support for secret chats will be enabled.
|
||||
EnableStorageOptimizer bool // If set to true, old files will automatically be deleted.
|
||||
IgnoreFileNames bool // If set to true, original file names will be ignored. Otherwise, downloaded files will be saved under names as close as possible to the original name.
|
||||
UseTestDataCenter bool // if set to true, the Telegram test environment will be used instead of the production environment.
|
||||
DatabaseDirectory string // The path to the directory for the persistent database; if empty, the current working directory will be used.
|
||||
FileDirectory string // The path to the directory for storing files; if empty, database_directory will be used.
|
||||
UseFileDatabase bool // If set to true, information about downloaded and uploaded files will be saved between application restarts.
|
||||
UseChatInfoDatabase bool // If set to true, the library will maintain a cache of users, basic groups, supergroups, channels and secret chats. Implies use_file_database.
|
||||
UseMessageDatabase bool // If set to true, the library will maintain a cache of chats and messages. Implies use_chat_info_database.
|
||||
UseSecretChats bool // If set to true, support for secret chats will be enabled.
|
||||
EnableStorageOptimizer bool // If set to true, old files will automatically be deleted.
|
||||
IgnoreFileNames bool // If set to true, original file names will be ignored. Otherwise, downloaded files will be saved under names as close as possible to the original name.
|
||||
SocksProxy *ProxySocks5 // Pass a ProxySocks5 if you need socks5 proxy. Username and password can be nil
|
||||
}
|
||||
|
||||
// NewClient Creates a new instance of TDLib.
|
||||
|
@ -78,6 +82,7 @@ func NewClient(config Config) *Client {
|
|||
client := Client{Client: C.td_json_client_create()}
|
||||
client.receivers = make([]EventReceiver, 0, 1)
|
||||
client.receiverLock = &sync.Mutex{}
|
||||
client.Config = config
|
||||
|
||||
go func() {
|
||||
for {
|
||||
|
@ -154,7 +159,7 @@ func NewClient(config Config) *Client {
|
|||
|
||||
// GetRawUpdatesChannel creates a general channel that fetches every update comming from tdlib
|
||||
func (client *Client) GetRawUpdatesChannel(capacity int) chan UpdateMsg {
|
||||
client.rawUpdates = make(chan UpdateMsg, 1000)
|
||||
client.rawUpdates = make(chan UpdateMsg, capacity)
|
||||
return client.rawUpdates
|
||||
}
|
||||
|
||||
|
@ -298,6 +303,14 @@ func (client *Client) Authorize() (AuthorizationState, error) {
|
|||
if ok == nil || err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if client.Config.SocksProxy != nil {
|
||||
ok, err = client.SetProxy(NewProxySocks5(client.Config.SocksProxy.Server, client.Config.SocksProxy.Port,
|
||||
client.Config.SocksProxy.Username, client.Config.SocksProxy.Password))
|
||||
if ok == nil || err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
authState, err := client.GetAuthorizationState()
|
||||
|
|
Loading…
Reference in New Issue