Compare commits

..

4 commits

Author SHA1 Message Date
Aleksey Obukhov 47da331806 Адаптация к tdlib версии 1.8.14 2023-07-29 22:11:36 -04:00
Aleksandr Zelenin 063c3471b0 readme fix 2023-03-31 05:56:57 +03:00
Aleksandr Zelenin ff46ac12bc readme fix 2023-03-24 05:32:34 +03:00
Aleksandr Zelenin c54167dc40 add example 2023-03-24 05:29:41 +03:00
40 changed files with 50179 additions and 53474 deletions

View file

@ -1,4 +1,4 @@
TAG := v1.8.0 TAG := v1.8.14
schema-update: schema-update:
curl https://raw.githubusercontent.com/tdlib/td/${TAG}/td/generate/scheme/td_api.tl 2>/dev/null > ./data/td_api.tl curl https://raw.githubusercontent.com/tdlib/td/${TAG}/td/generate/scheme/td_api.tl 2>/dev/null > ./data/td_api.tl

163
README.md
View file

@ -1,15 +1,158 @@
## Forked library for [modded TDLib](https://github.com/c0re100/td) # go-tdlib
When I'm refactoring my own bot from `Arman92/go-tdlib` to `zelenin/go-tdlib ` Go wrapper for [TDLib (Telegram Database Library)](https://github.com/tdlib/td) with full support of TDLib v1.8.14
I realized that zelenin's library doesn't meet my need😕 ## TDLib installation
So I fork it and make some changes Use [TDLib build instructions](https://tdlib.github.io/td/build.html) with checkmarked `Install built TDLib to /usr/local instead of placing the files to td/tdlib`.
1. Static build by default ### Note: Compatible with TDLib v1.8.14 only!
2. Add update [event filter](example#event-filter)
3. Add [command](example#command) parser
4. Receive correct message id to patch text/dice message response.
5. Add [Pending updates](example#pending-updates)
[Here](example) are a few example codes about how to use **c0re100/gotdlib**. ### Windows
Build with environment variables:
```
CGO_CFLAGS=-IC:/path/to/tdlib/build/tdlib/include
CGO_LDFLAGS=-LC:/path/to/tdlib/build/tdlib/bin -ltdjson
```
Example for PowerShell:
```powershell
$env:CGO_CFLAGS="-IC:/td/tdlib/include"; $env:CGO_LDFLAGS="-LC:/td/tdlib/bin -ltdjson"; go build -trimpath -ldflags="-s -w" -o demo.exe .\cmd\demo.go
```
## Usage
### Client
[Register an application](https://my.telegram.org/apps) to obtain an api_id and api_hash
```go
package main
import (
"log"
"path/filepath"
"github.com/zelenin/go-tdlib/client"
)
func main() {
// client authorizer
authorizer := client.ClientAuthorizer()
go client.CliInteractor(authorizer)
// or bot authorizer
// botToken := "000000000:gsVCGG5YbikxYHC7bP5vRvmBqJ7Xz6vG6td"
// authorizer := client.BotAuthorizer(botToken)
const (
apiId = 00000
apiHash = "8pu9yg32qkuukj83ozaqo5zzjwhkxhnk"
)
authorizer.TdlibParameters <- &client.TdlibParameters{
UseTestDc: false,
DatabaseDirectory: filepath.Join(".tdlib", "database"),
FilesDirectory: filepath.Join(".tdlib", "files"),
UseFileDatabase: true,
UseChatInfoDatabase: true,
UseMessageDatabase: true,
UseSecretChats: false,
ApiId: apiId,
ApiHash: apiHash,
SystemLanguageCode: "en",
DeviceModel: "Server",
SystemVersion: "1.0.0",
ApplicationVersion: "1.0.0",
EnableStorageOptimizer: true,
IgnoreFileNames: false,
}
_, err := client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{
NewVerbosityLevel: 1,
})
if err != nil {
log.Fatalf("SetLogVerbosityLevel error: %s", err)
}
tdlibClient, err := client.NewClient(authorizer)
if err != nil {
log.Fatalf("NewClient error: %s", err)
}
optionValue, err := tdlibClient.GetOption(&client.GetOptionRequest{
Name: "version",
})
if err != nil {
log.Fatalf("GetOption error: %s", err)
}
log.Printf("TDLib version: %s", optionValue.(*client.OptionValueString).Value)
me, err := tdlibClient.GetMe()
if err != nil {
log.Fatalf("GetMe error: %s", err)
}
log.Printf("Me: %s %s [%s]", me.FirstName, me.LastName, me.Username)
}
```
### Receive updates
```go
tdlibClient, err := client.NewClient(authorizer)
if err != nil {
log.Fatalf("NewClient error: %s", err)
}
listener := tdlibClient.GetListener()
defer listener.Close()
for update := range listener.Updates {
if update.GetClass() == client.ClassUpdate {
log.Printf("%#v", update)
}
}
```
### Proxy support
```go
proxy := client.WithProxy(&client.AddProxyRequest{
Server: "1.1.1.1",
Port: 1080,
Enable: true,
Type: &client.ProxyTypeSocks5{
Username: "username",
Password: "password",
},
})
tdlibClient, err := client.NewClient(authorizer, proxy)
```
## Example
[Example application](https://github.com/zelenin/go-tdlib/tree/master/example)
```
cd example
docker build --network host --build-arg TD_TAG=v1.8.14 --tag tdlib-test .
docker run --rm -it -e "API_ID=00000" -e "API_HASH=abcdef0123456789" tdlib-test ash
./app
```
## Notes
* WIP. Library API can be changed in the future
* The package includes a .tl-parser and generated [json-schema](https://github.com/zelenin/go-tdlib/tree/master/data) for creating libraries in other languages
## Author
[Aleksandr Zelenin](https://github.com/zelenin/), e-mail: [aleksandr@zelenin.me](mailto:aleksandr@zelenin.me)

View file

@ -3,14 +3,47 @@ package client
import ( import (
"errors" "errors"
"fmt" "fmt"
"syscall"
"time" "time"
"golang.org/x/crypto/ssh/terminal"
) )
var ErrNotSupportedAuthorizationState = errors.New("not supported state") var ErrNotSupportedAuthorizationState = errors.New("not supported state")
// Contains parameters for TDLib initialization
type TdlibParameters struct {
// Pass true to use Telegram test environment instead of the production environment
UseTestDc bool `json:"use_test_dc"`
// The path to the directory for the persistent database; if empty, the current working directory will be used
DatabaseDirectory string `json:"database_directory"`
// The path to the directory for storing files; if empty, database_directory will be used
FilesDirectory string `json:"files_directory"`
// Encryption key for the database. If the encryption key is invalid, then an error with code 401 will be returned
DatabaseEncryptionKey []byte `json:"database_encryption_key"`
// Pass true to keep information about downloaded and uploaded files between application restarts
UseFileDatabase bool `json:"use_file_database"`
// Pass true to keep cache of users, basic groups, supergroups, channels and secret chats between restarts. Implies use_file_database
UseChatInfoDatabase bool `json:"use_chat_info_database"`
// Pass true to keep cache of chats and messages between restarts. Implies use_chat_info_database
UseMessageDatabase bool `json:"use_message_database"`
// Pass true to enable support for secret chats
UseSecretChats bool `json:"use_secret_chats"`
// Application identifier for Telegram API access, which can be obtained at https://my.telegram.org
ApiId int32 `json:"api_id"`
// Application identifier hash for Telegram API access, which can be obtained at https://my.telegram.org
ApiHash string `json:"api_hash"`
// IETF language tag of the user's operating system language; must be non-empty
SystemLanguageCode string `json:"system_language_code"`
// Model of the device the application is being run on; must be non-empty
DeviceModel string `json:"device_model"`
// Version of the operating system the application is being run on. If empty, the version is automatically detected by TDLib
SystemVersion string `json:"system_version"`
// Application version; must be non-empty
ApplicationVersion string `json:"application_version"`
// Pass true to automatically delete old files in background
EnableStorageOptimizer bool `json:"enable_storage_optimizer"`
// Pass true to ignore original file names for downloaded files. Otherwise, downloaded files are saved under names as close as possible to the original name
IgnoreFileNames bool `json:"ignore_file_names"`
}
type AuthorizationStateHandler interface { type AuthorizationStateHandler interface {
Handle(client *Client, state AuthorizationState) error Handle(client *Client, state AuthorizationState) error
Close() Close()
@ -46,7 +79,7 @@ func Authorize(client *Client, authorizationStateHandler AuthorizationStateHandl
} }
type clientAuthorizer struct { type clientAuthorizer struct {
TdlibParameters chan *SetTdlibParametersRequest TdlibParameters chan *TdlibParameters
PhoneNumber chan string PhoneNumber chan string
Code chan string Code chan string
State chan AuthorizationState State chan AuthorizationState
@ -55,7 +88,7 @@ type clientAuthorizer struct {
func ClientAuthorizer() *clientAuthorizer { func ClientAuthorizer() *clientAuthorizer {
return &clientAuthorizer{ return &clientAuthorizer{
TdlibParameters: make(chan *SetTdlibParametersRequest, 1), TdlibParameters: make(chan *TdlibParameters, 1),
PhoneNumber: make(chan string, 1), PhoneNumber: make(chan string, 1),
Code: make(chan string, 1), Code: make(chan string, 1),
State: make(chan AuthorizationState, 10), State: make(chan AuthorizationState, 10),
@ -68,7 +101,25 @@ func (stateHandler *clientAuthorizer) Handle(client *Client, state Authorization
switch state.AuthorizationStateType() { switch state.AuthorizationStateType() {
case TypeAuthorizationStateWaitTdlibParameters: case TypeAuthorizationStateWaitTdlibParameters:
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters) p := <-stateHandler.TdlibParameters
_, err := client.SetTdlibParameters(&SetTdlibParametersRequest{
UseTestDc: p.UseTestDc,
DatabaseDirectory: p.DatabaseDirectory,
FilesDirectory: p.FilesDirectory,
DatabaseEncryptionKey: p.DatabaseEncryptionKey,
UseFileDatabase: p.UseFileDatabase,
UseChatInfoDatabase: p.UseChatInfoDatabase,
UseMessageDatabase: p.UseMessageDatabase,
UseSecretChats: p.UseSecretChats,
ApiId: p.ApiId,
ApiHash: p.ApiHash,
SystemLanguageCode: p.SystemLanguageCode,
DeviceModel: p.DeviceModel,
SystemVersion: p.SystemVersion,
ApplicationVersion: p.ApplicationVersion,
EnableStorageOptimizer: p.EnableStorageOptimizer,
IgnoreFileNames: p.IgnoreFileNames,
})
return err return err
case TypeAuthorizationStateWaitPhoneNumber: case TypeAuthorizationStateWaitPhoneNumber:
@ -82,12 +133,21 @@ func (stateHandler *clientAuthorizer) Handle(client *Client, state Authorization
}) })
return err return err
case TypeAuthorizationStateWaitEmailAddress:
return ErrNotSupportedAuthorizationState
case TypeAuthorizationStateWaitEmailCode:
return ErrNotSupportedAuthorizationState
case TypeAuthorizationStateWaitCode: case TypeAuthorizationStateWaitCode:
_, err := client.CheckAuthenticationCode(&CheckAuthenticationCodeRequest{ _, err := client.CheckAuthenticationCode(&CheckAuthenticationCodeRequest{
Code: <-stateHandler.Code, Code: <-stateHandler.Code,
}) })
return err return err
case TypeAuthorizationStateWaitOtherDeviceConfirmation:
return ErrNotSupportedAuthorizationState
case TypeAuthorizationStateWaitRegistration: case TypeAuthorizationStateWaitRegistration:
return ErrNotSupportedAuthorizationState return ErrNotSupportedAuthorizationState
@ -137,6 +197,12 @@ func CliInteractor(clientAuthorizer *clientAuthorizer) {
clientAuthorizer.PhoneNumber <- phoneNumber clientAuthorizer.PhoneNumber <- phoneNumber
case TypeAuthorizationStateWaitEmailAddress:
return
case TypeAuthorizationStateWaitEmailCode:
return
case TypeAuthorizationStateWaitCode: case TypeAuthorizationStateWaitCode:
var code string var code string
@ -145,15 +211,18 @@ func CliInteractor(clientAuthorizer *clientAuthorizer) {
clientAuthorizer.Code <- code clientAuthorizer.Code <- code
case TypeAuthorizationStateWaitOtherDeviceConfirmation:
return
case TypeAuthorizationStateWaitRegistration:
return
case TypeAuthorizationStateWaitPassword: case TypeAuthorizationStateWaitPassword:
fmt.Println("Enter password: ") fmt.Println("Enter password: ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin)) var password string
if err != nil { fmt.Scanln(&password)
fmt.Println(err)
continue
}
clientAuthorizer.Password <- string(bytePassword) clientAuthorizer.Password <- password
case TypeAuthorizationStateReady: case TypeAuthorizationStateReady:
return return
@ -163,14 +232,14 @@ func CliInteractor(clientAuthorizer *clientAuthorizer) {
} }
type botAuthorizer struct { type botAuthorizer struct {
TdlibParameters chan *SetTdlibParametersRequest TdlibParameters chan *TdlibParameters
Token chan string Token chan string
State chan AuthorizationState State chan AuthorizationState
} }
func BotAuthorizer(token string) *botAuthorizer { func BotAuthorizer(token string) *botAuthorizer {
botAuthorizer := &botAuthorizer{ botAuthorizer := &botAuthorizer{
TdlibParameters: make(chan *SetTdlibParametersRequest, 1), TdlibParameters: make(chan *TdlibParameters, 1),
Token: make(chan string, 1), Token: make(chan string, 1),
State: make(chan AuthorizationState, 10), State: make(chan AuthorizationState, 10),
} }
@ -185,7 +254,25 @@ func (stateHandler *botAuthorizer) Handle(client *Client, state AuthorizationSta
switch state.AuthorizationStateType() { switch state.AuthorizationStateType() {
case TypeAuthorizationStateWaitTdlibParameters: case TypeAuthorizationStateWaitTdlibParameters:
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters) p := <-stateHandler.TdlibParameters
_, err := client.SetTdlibParameters(&SetTdlibParametersRequest{
UseTestDc: p.UseTestDc,
DatabaseDirectory: p.DatabaseDirectory,
FilesDirectory: p.FilesDirectory,
DatabaseEncryptionKey: p.DatabaseEncryptionKey,
UseFileDatabase: p.UseFileDatabase,
UseChatInfoDatabase: p.UseChatInfoDatabase,
UseMessageDatabase: p.UseMessageDatabase,
UseSecretChats: p.UseSecretChats,
ApiId: p.ApiId,
ApiHash: p.ApiHash,
SystemLanguageCode: p.SystemLanguageCode,
DeviceModel: p.DeviceModel,
SystemVersion: p.SystemVersion,
ApplicationVersion: p.ApplicationVersion,
EnableStorageOptimizer: p.EnableStorageOptimizer,
IgnoreFileNames: p.IgnoreFileNames,
})
return err return err
case TypeAuthorizationStateWaitPhoneNumber: case TypeAuthorizationStateWaitPhoneNumber:

View file

@ -1,25 +1,18 @@
package client package client
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"strconv"
"sync" "sync"
"time" "time"
) )
var pendingUpdateType []Type
type Client struct { type Client struct {
jsonClient *JsonClient jsonClient *JsonClient
extraGenerator ExtraGenerator extraGenerator ExtraGenerator
responses chan *Response responses chan *Response
pendingResp chan *Response
listenerStore *listenerStore listenerStore *listenerStore
catchersStore *sync.Map catchersStore *sync.Map
successMsgStore *sync.Map
forwardMsgStore *sync.Map
updatesTimeout time.Duration updatesTimeout time.Duration
catchTimeout time.Duration catchTimeout time.Duration
} }
@ -44,26 +37,9 @@ func WithProxy(req *AddProxyRequest) Option {
} }
} }
func SetLogLevel(level int32) { func WithLogVerbosity(req *SetLogVerbosityLevelRequest) Option {
_, _ = SetLogVerbosityLevel(&SetLogVerbosityLevelRequest{ return func(client *Client) {
NewVerbosityLevel: level, client.SetLogVerbosityLevel(req)
})
}
func SetFilePath(path string) {
_, _ = SetLogStream(&SetLogStreamRequest{
LogStream: &LogStreamFile{
Path: path,
MaxFileSize: 10485760,
RedirectStderr: true,
},
})
}
// Keep specific update type in memory when listener is not ready.
func SetPendingUpdateType(update ...Type) {
for _, v := range update {
pendingUpdateType = append(pendingUpdateType, v)
} }
} }
@ -71,11 +47,8 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
client := &Client{ client := &Client{
jsonClient: NewJsonClient(), jsonClient: NewJsonClient(),
responses: make(chan *Response, 1000), responses: make(chan *Response, 1000),
pendingResp: make(chan *Response, 1000),
listenerStore: newListenerStore(), listenerStore: newListenerStore(),
catchersStore: &sync.Map{}, catchersStore: &sync.Map{},
successMsgStore: &sync.Map{},
forwardMsgStore: &sync.Map{},
} }
client.extraGenerator = UuidV4Generator() client.extraGenerator = UuidV4Generator()
@ -87,7 +60,6 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
tdlibInstance.addClient(client) tdlibInstance.addClient(client)
go client.processPendingResponse()
go client.receiver() go client.receiver()
err := Authorize(client, authorizationStateHandler) err := Authorize(client, authorizationStateHandler)
@ -98,7 +70,8 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
return client, nil return client, nil
} }
func (client *Client) processResponse(response *Response) { func (client *Client) receiver() {
for response := range client.responses {
if response.Extra != "" { if response.Extra != "" {
value, ok := client.catchersStore.Load(response.Extra) value, ok := client.catchersStore.Load(response.Extra)
if ok { if ok {
@ -108,35 +81,14 @@ func (client *Client) processResponse(response *Response) {
typ, err := UnmarshalType(response.Data) typ, err := UnmarshalType(response.Data)
if err != nil { if err != nil {
return continue
}
if typ.GetType() == (&UpdateMessageSendSucceeded{}).GetType() {
sendVal, sOk := client.successMsgStore.Load(typ.(*UpdateMessageSendSucceeded).OldMessageId)
if sOk {
sendVal.(chan *Response) <- response
}
forwardVal, fOk := client.forwardMsgStore.Load(typ.(*UpdateMessageSendSucceeded).OldMessageId)
if fOk {
forwardVal.(chan *Response) <- response
}
}
if len(client.listenerStore.Listeners()) == 0 {
for _, p := range pendingUpdateType {
if typ.GetType() == p.GetType() {
client.pendingResp <- response
}
}
} }
needGc := false needGc := false
for _, listener := range client.listenerStore.Listeners() { for _, listener := range client.listenerStore.Listeners() {
if listener.IsActive() && listener.Updates != nil && typ.GetType() == listener.Filter.GetType() { // All updates go to Updates channel if type == filter if listener.IsActive() {
listener.Updates <- typ listener.Updates <- typ
} else if listener.IsActive() && listener.RawUpdates != nil { // All updates go to RawUpdates channel if filter is empty } else {
listener.RawUpdates <- typ
} else if !listener.IsActive() { // GC inactive listener
needGc = true needGc = true
} }
} }
@ -144,31 +96,6 @@ func (client *Client) processResponse(response *Response) {
client.listenerStore.gc() client.listenerStore.gc()
} }
} }
func (client *Client) receiver() {
for response := range client.responses {
client.processResponse(response)
}
}
func (client *Client) processPendingResponse() {
// No need to process pending response if no pending list.
if len(pendingUpdateType) == 0 {
return
}
// Wait for listener to be ready.
for {
if len(client.listenerStore.Listeners()) > 0 {
break
}
time.Sleep(1 * time.Second)
}
// Start processing pending response
for response := range client.pendingResp {
client.processResponse(response)
}
} }
func (client *Client) Send(req Request) (*Response, error) { func (client *Client) Send(req Request) (*Response, error) {
@ -190,36 +117,8 @@ func (client *Client) Send(req Request) (*Response, error) {
select { select {
case response := <-catcher: case response := <-catcher:
if response.Type != "error" && req.Type == "sendMessage" { return response, nil
m, err := UnmarshalMessage(response.Data)
if err != nil {
return nil, err
}
if m.Content.MessageContentType() == "messageText" || m.Content.MessageContentType() == "messageDice" {
successCatcher := make(chan *Response, 1)
client.successMsgStore.Store(m.Id, successCatcher)
defer (func() {
client.successMsgStore.Delete(m.Id)
close(successCatcher)
})()
select {
case modResponse := <-successCatcher:
m2, err2 := UnmarshalUpdateMessageSendSucceeded(modResponse.Data)
if err2 != nil {
return response, nil
}
response.Data = bytes.Replace(response.Data, []byte("{\"@type\":\"messageSendingStatePending\"}"), []byte("{\"@type\":\"updateMessageSendSucceeded\"}"), 1)
response.Data = bytes.Replace(response.Data, []byte("\"id\":"+strconv.FormatInt(m.Id, 10)), []byte("\"id\":"+strconv.FormatInt(m2.Message.Id, 10)), 1)
return response, nil
case <-time.After(1 * time.Second):
return response, nil
}
}
}
return response, nil
case <-ctx.Done(): case <-ctx.Done():
return nil, errors.New("response catching timeout") return nil, errors.New("response catching timeout")
} }
@ -228,18 +127,7 @@ func (client *Client) Send(req Request) (*Response, error) {
func (client *Client) GetListener() *Listener { func (client *Client) GetListener() *Listener {
listener := &Listener{ listener := &Listener{
isActive: true, isActive: true,
RawUpdates: make(chan Type, 1000), Updates: make(chan Type, 1000),
}
client.listenerStore.Add(listener)
return listener
}
func (client *Client) AddEventReceiver(msgType Type, channelCapacity int) *Listener {
listener := &Listener{
isActive: true,
Updates: make(chan Type, channelCapacity),
Filter: msgType,
} }
client.listenerStore.Add(listener) client.listenerStore.Add(listener)

View file

@ -1,66 +1,20 @@
package client package client
import ( import (
"strings" "fmt"
"math/rand"
"github.com/google/uuid"
) )
type ExtraGenerator func() string type ExtraGenerator func() string
func UuidV4Generator() ExtraGenerator { func UuidV4Generator() ExtraGenerator {
return func() string { return func() string {
return uuid.NewString() var uuid [16]byte
} rand.Read(uuid[:])
}
func IsCommand(text string) bool { uuid[6] = (uuid[6] & 0x0f) | 0x40
if text != "" { uuid[8] = (uuid[8] & 0x3f) | 0x80
if text[0] == '/' {
return true
}
}
return false
}
func CheckCommand(text string, entities []*TextEntity) string { return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", uuid[:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
if IsCommand(text) {
// Check text entities and make bot happy!
if len(entities) >= 1 {
// Get first command
if entities[0].Type.TextEntityTypeType() == "textEntityTypeBotCommand" {
// e.g.: { "text": "/hello@world_bot", "textEntity": { offset: 0, length: 16 } }
// Result: "/hello"
if i := strings.Index(text[:entities[0].Length], "@"); i != -1 {
return text[:i]
}
return text[:entities[0].Length]
}
} else {
// Since userbot does not have bot command entities in Private Chat, so make userbot happy too!
// e.g.: ["/hello@world_bot", "/hello@", "/hello@123"]
// Result: "/hello"
if i := strings.Index(text, "@"); i != -1 {
return text[:i]
}
// e.g. ["/hello 123", "/hell o 123"]
// Result: "/hello", "/hell"
if i := strings.Index(text, " "); i != -1 {
return text[:i]
}
return text
} }
} }
return ""
}
func CommandArgument(text string) string {
if IsCommand(text) {
// e.g. ["/hello 123", "/hell o 123"]
// Result: "123", "o 123"
if i := strings.Index(text, " "); i != -1 {
return text[i+1:]
}
}
return ""
}

File diff suppressed because it is too large Load diff

View file

@ -48,8 +48,6 @@ type Listener struct {
mu sync.Mutex mu sync.Mutex
isActive bool isActive bool
Updates chan Type Updates chan Type
RawUpdates chan Type
Filter Type
} }
func (listener *Listener) Close() { func (listener *Listener) Close() {
@ -57,13 +55,8 @@ func (listener *Listener) Close() {
defer listener.mu.Unlock() defer listener.mu.Unlock()
listener.isActive = false listener.isActive = false
if listener.Updates != nil {
close(listener.Updates) close(listener.Updates)
} }
if listener.RawUpdates != nil {
close(listener.RawUpdates)
}
}
func (listener *Listener) IsActive() bool { func (listener *Listener) IsActive() bool {
listener.mu.Lock() listener.mu.Lock()

52
client/puller/chat.go Normal file
View file

@ -0,0 +1,52 @@
package puller
import (
"github.com/zelenin/go-tdlib/client"
)
func ChatHistory(tdlibClient *client.Client, chatId int64) (chan *client.Message, chan error) {
messageChan := make(chan *client.Message, 10)
errChan := make(chan error, 1)
var fromMessageId int64 = 0
var offset int32 = 0
var limit int32 = 100
go chatHistory(tdlibClient, messageChan, errChan, chatId, fromMessageId, offset, limit, false)
return messageChan, errChan
}
func chatHistory(tdlibClient *client.Client, messageChan chan *client.Message, errChan chan error, chatId int64, fromMessageId int64, offset int32, limit int32, onlyLocal bool) {
defer func() {
close(messageChan)
close(errChan)
}()
for {
messages, err := tdlibClient.GetChatHistory(&client.GetChatHistoryRequest{
ChatId: chatId,
FromMessageId: fromMessageId,
Offset: offset,
Limit: limit,
OnlyLocal: onlyLocal,
})
if err != nil {
errChan <- err
return
}
if len(messages.Messages) == 0 {
errChan <- EOP
break
}
for _, message := range messages.Messages {
fromMessageId = message.Id
messageChan <- message
}
}
}

62
client/puller/chats.go Normal file
View file

@ -0,0 +1,62 @@
package puller
import (
"math"
"github.com/zelenin/go-tdlib/client"
)
func Chats(tdlibClient *client.Client) (chan *client.Chat, chan error) {
chatChan := make(chan *client.Chat, 10)
errChan := make(chan error, 1)
var offsetOrder client.JsonInt64 = math.MaxInt64
var offsetChatId int64 = 0
var limit int32 = 100
go chats(tdlibClient, chatChan, errChan, offsetOrder, offsetChatId, limit)
return chatChan, errChan
}
func chats(tdlibClient *client.Client, chatChan chan *client.Chat, errChan chan error, offsetOrder client.JsonInt64, offsetChatId int64, limit int32) {
defer func() {
close(chatChan)
close(errChan)
}()
for {
chats, err := tdlibClient.GetChats(&client.GetChatsRequest{
OffsetOrder: offsetOrder,
OffsetChatId: offsetChatId,
Limit: limit,
})
if err != nil {
errChan <- err
return
}
if len(chats.ChatIds) == 0 {
errChan <- EOP
break
}
for _, chatId := range chats.ChatIds {
chat, err := tdlibClient.GetChat(&client.GetChatRequest{
ChatId: chatId,
})
if err != nil {
errChan <- err
return
}
offsetOrder = chat.Order
offsetChatId = chat.Id
chatChan <- chat
}
}
}

7
client/puller/error.go Normal file
View file

@ -0,0 +1,7 @@
package puller
import (
"errors"
)
var EOP = errors.New("end of pull")

View file

@ -0,0 +1,53 @@
package puller
import (
"github.com/zelenin/go-tdlib/client"
)
func SupergroupMembers(tdlibClient *client.Client, supergroupId int64) (chan *client.ChatMember, chan error) {
chatMemberChan := make(chan *client.ChatMember, 10)
errChan := make(chan error, 1)
var filter client.SupergroupMembersFilter = nil
var offset int32 = 0
var limit int32 = 200
go supergroupMembers(tdlibClient, chatMemberChan, errChan, supergroupId, filter, offset, limit)
return chatMemberChan, errChan
}
func supergroupMembers(tdlibClient *client.Client, chatMemberChan chan *client.ChatMember, errChan chan error, supergroupId int64, filter client.SupergroupMembersFilter, offset int32, limit int32) {
defer func() {
close(chatMemberChan)
close(errChan)
}()
var page int32 = 0
for {
chatMembers, err := tdlibClient.GetSupergroupMembers(&client.GetSupergroupMembersRequest{
SupergroupId: supergroupId,
Filter: filter,
Offset: page*limit + offset,
Limit: limit,
})
if err != nil {
errChan <- err
return
}
if len(chatMembers.Members) == 0 {
errChan <- EOP
break
}
for _, member := range chatMembers.Members {
chatMemberChan <- member
}
page++
}
}

10
client/tdjson.go Normal file
View file

@ -0,0 +1,10 @@
//go:build darwin
// +build darwin
package client
/*
#cgo darwin CFLAGS: -I/usr/local/include
#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
*/
import "C"

13
client/tdjson_dynamic.go Normal file
View file

@ -0,0 +1,13 @@
//go:build libtdjson && (linux || darwin)
// +build libtdjson
// +build linux darwin
package client
/*
#cgo linux CFLAGS: -I/usr/local/include
#cgo linux LDFLAGS: -L/usr/local/lib -ltdjson -lstdc++ -lssl -lcrypto -ldl -lz -lm
#cgo darwin CFLAGS: -I/usr/local/include
#cgo darwin LDFLAGS: -L/usr/local/lib -ltdjson -lstdc++ -lssl -lcrypto -ldl -lz -lm
*/
import "C"

13
client/tdjson_static.go Normal file
View file

@ -0,0 +1,13 @@
//go:build !libtdjson && (linux || darwin)
// +build !libtdjson
// +build linux darwin
package client
/*
#cgo linux CFLAGS: -I/usr/local/include
#cgo linux LDFLAGS: -L/usr/local/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdactor -ltdapi -ltddb -ltdsqlite -ltdnet -ltdutils -lstdc++ -lssl -lcrypto -ldl -lz -lm
#cgo darwin CFLAGS: -I/usr/local/include
#cgo darwin LDFLAGS: -L/usr/local/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdactor -ltdapi -ltddb -ltdsqlite -ltdnet -ltdutils -lstdc++ -lssl -lcrypto -ldl -lz -lm
*/
import "C"

View file

@ -1,14 +1,10 @@
package client package client
//#cgo linux CFLAGS: -I/usr/local/include /*
//#cgo darwin CFLAGS: -I/usr/local/include #include <stdlib.h>
//#cgo windows CFLAGS: -IE:/src/tdlib -IE:/src/tdlib/build #include <string.h>
//#cgo linux LDFLAGS: -L/usr/local/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdapi -ltdactor -ltddb -ltdsqlite -ltdnet -ltdutils -lstdc++ -lssl -lcrypto -ldl -lz -lm #include <td/telegram/td_json_client.h>
//#cgo darwin LDFLAGS: -L/usr/local/lib -L/usr/local/opt/openssl/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdapi -ltdactor -ltddb -ltdsqlite -ltdnet -ltdutils -lstdc++ -lssl -lcrypto -ldl -lz -lm */
//#cgo windows LDFLAGS: -LE:/src/tdlib/build/Release -ltdjson
//#include <stdlib.h>
//#include <td/telegram/td_json_client.h>
//#include <td/telegram/td_log.h>
import "C" import "C"
import ( import (
@ -88,7 +84,8 @@ func (instance *tdlib) receive(timeout time.Duration) (*Response, error) {
return nil, errors.New("update receiving timeout") return nil, errors.New("update receiving timeout")
} }
data := []byte(C.GoString(result)) resultLen := C.strlen(result)
data := C.GoBytes(unsafe.Pointer(result), C.int(resultLen))
var resp Response var resp Response
@ -112,7 +109,8 @@ func Execute(req Request) (*Response, error) {
return nil, errors.New("request can't be parsed") return nil, errors.New("request can't be parsed")
} }
data = []byte(C.GoString(result)) resultLen := C.strlen(result)
data = C.GoBytes(unsafe.Pointer(result), C.int(resultLen))
var resp Response var resp Response

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -4,11 +4,12 @@ import (
"bufio" "bufio"
"flag" "flag"
"log" "log"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"github.com/c0re100/gotdlib/codegen" "github.com/zelenin/go-tdlib/codegen"
"github.com/c0re100/gotdlib/tlparser" "github.com/zelenin/go-tdlib/tlparser"
) )
type config struct { type config struct {
@ -24,21 +25,22 @@ func main() {
var config config var config config
flag.StringVar(&config.version, "version", "", "TDLib version") flag.StringVar(&config.version, "version", "", "TDLib version")
flag.StringVar(&config.outputDirPath, "outputDir", "./client", "output directory") flag.StringVar(&config.outputDirPath, "outputDir", "./tdlib", "output directory")
flag.StringVar(&config.packageName, "package", "client", "package name") flag.StringVar(&config.packageName, "package", "tdlib", "package name")
flag.StringVar(&config.functionFileName, "functionFile", "function.go", "functions filename") flag.StringVar(&config.functionFileName, "functionFile", "function.go", "functions filename")
flag.StringVar(&config.typeFileName, "typeFile", "type.go", "types filename") flag.StringVar(&config.typeFileName, "typeFile", "type.go", "types filename")
flag.StringVar(&config.unmarshalerFileName, "unmarshalerFile", "unmarshaler.go", "unmarshalers filename") flag.StringVar(&config.unmarshalerFileName, "unmarshalerFile", "unmarshaler.go", "unmarshalers filename")
flag.Parse() flag.Parse()
tl, err := os.Open("./data/td_api.tl") resp, err := http.Get("https://raw.githubusercontent.com/tdlib/td/" + config.version + "/td/generate/scheme/td_api.tl")
if err != nil { if err != nil {
log.Fatalf(err.Error()) log.Fatalf("http.Get error: %s", err)
return return
} }
defer resp.Body.Close()
schema, err := tlparser.Parse(bufio.NewReader(tl)) schema, err := tlparser.Parse(resp.Body)
if err != nil { if err != nil {
log.Fatalf("schema parse error: %s", err) log.Fatalf("schema parse error: %s", err)
return return

View file

@ -4,7 +4,7 @@ import (
"bufio" "bufio"
"encoding/json" "encoding/json"
"flag" "flag"
"github.com/c0re100/gotdlib/tlparser" "github.com/zelenin/go-tdlib/tlparser"
"log" "log"
"net/http" "net/http"
"os" "os"

View file

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/c0re100/gotdlib/tlparser" "github.com/zelenin/go-tdlib/tlparser"
) )
func GenerateFunctions(schema *tlparser.Schema, packageName string) []byte { func GenerateFunctions(schema *tlparser.Schema, packageName string) []byte {

View file

@ -1,7 +1,7 @@
package codegen package codegen
import ( import (
"github.com/c0re100/gotdlib/tlparser" "github.com/zelenin/go-tdlib/tlparser"
"log" "log"
"strings" "strings"
) )

View file

@ -3,7 +3,7 @@ package codegen
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/c0re100/gotdlib/tlparser" "github.com/zelenin/go-tdlib/tlparser"
) )
func GenerateTypes(schema *tlparser.Schema, packageName string) []byte { func GenerateTypes(schema *tlparser.Schema, packageName string) []byte {

View file

@ -3,7 +3,7 @@ package codegen
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/c0re100/gotdlib/tlparser" "github.com/zelenin/go-tdlib/tlparser"
) )
func GenerateUnmarshalers(schema *tlparser.Schema, packageName string) []byte { func GenerateUnmarshalers(schema *tlparser.Schema, packageName string) []byte {

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

85
example/Dockerfile Normal file
View file

@ -0,0 +1,85 @@
FROM alpine:3.17 as tdlib-builder
ENV LANG en_US.UTF-8
ENV TZ UTC
ARG TD_TAG
RUN apk update && \
apk upgrade && \
apk add --update \
build-base \
ca-certificates \
ccache \
cmake \
git \
gperf \
linux-headers \
openssl-dev \
php \
php-ctype \
readline-dev \
zlib-dev && \
git clone -b "${TD_TAG}" "https://github.com/tdlib/td.git" /src && \
mkdir /src/build && \
cd /src/build && \
cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX:PATH=/usr/local \
.. && \
cmake --build . --target prepare_cross_compiling && \
cd .. && \
php SplitSource.php && \
cd build && \
cmake --build . --target install && \
ls -lah /usr/local
FROM golang:1.20.2-alpine3.17 as go-builder
ENV LANG en_US.UTF-8
ENV TZ UTC
RUN set -eux && \
apk update && \
apk upgrade && \
apk add \
bash \
build-base \
ca-certificates \
curl \
git \
linux-headers \
openssl-dev \
zlib-dev
WORKDIR /src
COPY --from=tdlib-builder /usr/local/include/td /usr/local/include/td/
COPY --from=tdlib-builder /usr/local/lib/libtd* /usr/local/lib/
COPY . /src
RUN go build \
-a \
-trimpath \
-ldflags "-s -w" \
-o app \
"./demo.go" && \
ls -lah
FROM alpine:3.17
ENV LANG en_US.UTF-8
ENV TZ UTC
RUN apk upgrade --no-cache && \
apk add --no-cache \
ca-certificates \
libstdc++
WORKDIR /app
COPY --from=go-builder /src/app .
CMD ["./app"]

View file

@ -1,22 +0,0 @@
## Example
### Bot
Login to bot account.
### Command
Handle user command and reply it.
### Event Filter
Since we can have many update type in updates.
So we need to filter update events, like UpdateNewMessage, UpdateMessageSendSucceeded, UpdateMessageSendFailed, etc.
### Media
Send photo or album to chat.
### Pending updates
When starting a bot, we may have some updates that are missed to process when a listener IS NOT ready.
So we need to keep specific update types in memory until a listener is set, then we can process those updates again.
### Raw Update
Get update without event filter.

View file

@ -1,105 +0,0 @@
package main
import (
"log"
"os"
"os/signal"
"syscall"
tdlib "github.com/c0re100/gotdlib/client"
)
func GetTdParameters() *tdlib.SetTdlibParametersRequest {
return &tdlib.SetTdlibParametersRequest{
UseTestDc: false,
DatabaseDirectory: "./tdlib-db",
FilesDirectory: "./tdlib-files",
UseFileDatabase: true,
UseChatInfoDatabase: true,
UseMessageDatabase: true,
UseSecretChats: false,
ApiId: 132712,
ApiHash: "e82c07ad653399a37baca8d1e498e472",
SystemLanguageCode: "en",
DeviceModel: "HuskyNG",
SystemVersion: "3.0",
ApplicationVersion: "3.0",
EnableStorageOptimizer: true,
IgnoreFileNames: false,
}
}
func main() {
tdlib.SetLogLevel(0)
tdlib.SetFilePath("./errors.txt")
botToken := "your_bot_token"
authorizer := tdlib.BotAuthorizer(botToken)
authorizer.TdlibParameters <- GetTdParameters()
client, err := tdlib.NewClient(authorizer)
if err != nil {
log.Fatalf("NewClient error: %s", err)
}
// Handle SIGINT
ch := make(chan os.Signal, 2)
signal.Notify(ch, os.Interrupt, syscall.SIGINT)
signal.Notify(ch, os.Interrupt, syscall.SIGKILL)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
signal.Notify(ch, os.Interrupt, syscall.SIGQUIT)
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
go func() {
<-ch
client.Close()
}()
me, err := client.GetMe()
if err != nil {
log.Fatalf("GetMe error: %s", err)
}
log.Printf("%v connected", me.Usernames)
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
defer listener.Close()
for update := range listener.Updates {
updateMsg := update.(*tdlib.UpdateNewMessage)
chatId := updateMsg.Message.ChatId
msgId := updateMsg.Message.Id
var msgText string
var msgEnt []*tdlib.TextEntity
switch updateMsg.Message.Content.MessageContentType() {
case "messageText":
msgText = updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
msgEnt = updateMsg.Message.Content.(*tdlib.MessageText).Text.Entities
cmd := tdlib.CheckCommand(msgText, msgEnt)
switch cmd {
case "/ping":
text, _ := tdlib.ParseTextEntities(&tdlib.ParseTextEntitiesRequest{
Text: "<b>pong!</b>",
ParseMode: &tdlib.TextParseModeHTML{},
})
m, err := client.SendMessage(&tdlib.SendMessageRequest{
ChatId: chatId,
ReplyTo: &tdlib.InputMessageReplyToMessage{
ChatId: chatId,
MessageId: msgId,
},
InputMessageContent: &tdlib.InputMessageText{
Text: text,
},
})
if err != nil {
continue
}
log.Printf("Message sent, ID: %d", m.Id)
}
}
}
}

View file

@ -1,131 +0,0 @@
package main
import (
"log"
"os"
"os/signal"
"syscall"
tdlib "github.com/c0re100/gotdlib/client"
)
func GetSenderId(sender tdlib.MessageSender) int64 {
if sender.MessageSenderType() == "messageSenderUser" {
return sender.(*tdlib.MessageSenderUser).UserId
} else {
return sender.(*tdlib.MessageSenderChat).ChatId
}
}
func GetTdParameters() *tdlib.SetTdlibParametersRequest {
return &tdlib.SetTdlibParametersRequest{
UseTestDc: false,
DatabaseDirectory: "./tdlib-db",
FilesDirectory: "./tdlib-files",
UseFileDatabase: true,
UseChatInfoDatabase: true,
UseMessageDatabase: true,
UseSecretChats: false,
ApiId: 132712,
ApiHash: "e82c07ad653399a37baca8d1e498e472",
SystemLanguageCode: "en",
DeviceModel: "HuskyNG",
SystemVersion: "3.0",
ApplicationVersion: "3.0",
EnableStorageOptimizer: true,
IgnoreFileNames: false,
}
}
func main() {
tdlib.SetLogLevel(0)
tdlib.SetFilePath("./errors.txt")
authorizer := tdlib.ClientAuthorizer()
go tdlib.CliInteractor(authorizer)
authorizer.TdlibParameters <- GetTdParameters()
client, err := tdlib.NewClient(authorizer)
if err != nil {
log.Fatalf("NewClient error: %s", err)
}
// Handle SIGINT
ch := make(chan os.Signal, 2)
signal.Notify(ch, os.Interrupt, syscall.SIGINT)
signal.Notify(ch, os.Interrupt, syscall.SIGKILL)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
signal.Notify(ch, os.Interrupt, syscall.SIGQUIT)
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
go func() {
<-ch
client.Close()
}()
me, err := client.GetMe()
if err != nil {
log.Fatalf("GetMe error: %s", err)
}
log.Printf("%v connected", me.Usernames)
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
defer listener.Close()
for update := range listener.Updates {
updateMsg := update.(*tdlib.UpdateNewMessage)
chatId := updateMsg.Message.ChatId
senderId := GetSenderId(updateMsg.Message.SenderId)
msgId := updateMsg.Message.Id
if senderId == me.Id {
var msgText string
var msgEnt []*tdlib.TextEntity
switch updateMsg.Message.Content.MessageContentType() {
case "messageText":
msgText = updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
msgEnt = updateMsg.Message.Content.(*tdlib.MessageText).Text.Entities
}
cmd := tdlib.CheckCommand(msgText, msgEnt)
switch cmd {
case "/test":
text, _ := tdlib.ParseTextEntities(&tdlib.ParseTextEntitiesRequest{
Text: "<b>Hi test user</b>",
ParseMode: &tdlib.TextParseModeHTML{},
})
m, err := client.SendMessage(&tdlib.SendMessageRequest{
ChatId: chatId,
ReplyTo: &tdlib.InputMessageReplyToMessage{
ChatId: chatId,
MessageId: msgId,
},
InputMessageContent: &tdlib.InputMessageText{
Text: text,
},
})
if err != nil {
continue
}
log.Printf("Message sent, ID: %d", m.Id)
case "/repeat":
m, err := client.SendMessage(&tdlib.SendMessageRequest{
ChatId: chatId,
ReplyTo: &tdlib.InputMessageReplyToMessage{
ChatId: chatId,
MessageId: msgId,
},
InputMessageContent: &tdlib.InputMessageText{
Text: &tdlib.FormattedText{Text: tdlib.CommandArgument(msgText)},
},
})
if err != nil {
continue
}
log.Printf("Message sent, ID: %d", m.Id)
}
}
}
}

83
example/demo.go Normal file
View file

@ -0,0 +1,83 @@
package main
import (
"log"
"os"
"os/signal"
"path/filepath"
"strconv"
"syscall"
"github.com/zelenin/go-tdlib/client"
)
func main() {
authorizer := client.ClientAuthorizer()
go client.CliInteractor(authorizer)
var (
apiIdRaw = os.Getenv("API_ID")
apiHash = os.Getenv("API_HASH")
)
apiId64, err := strconv.ParseInt(apiIdRaw, 10, 32)
if err != nil {
log.Fatalf("strconv.Atoi error: %s", err)
}
apiId := int32(apiId64)
authorizer.TdlibParameters <- &client.TdlibParameters{
UseTestDc: false,
DatabaseDirectory: filepath.Join(".tdlib", "database"),
FilesDirectory: filepath.Join(".tdlib", "files"),
UseFileDatabase: true,
UseChatInfoDatabase: true,
UseMessageDatabase: true,
UseSecretChats: false,
ApiId: apiId,
ApiHash: apiHash,
SystemLanguageCode: "en",
DeviceModel: "Server",
SystemVersion: "1.0.0",
ApplicationVersion: "1.0.0",
EnableStorageOptimizer: true,
IgnoreFileNames: false,
}
_, err = client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{
NewVerbosityLevel: 1,
})
if err != nil {
log.Fatalf("SetLogVerbosityLevel error: %s", err)
}
tdlibClient, err := client.NewClient(authorizer)
if err != nil {
log.Fatalf("NewClient error: %s", err)
}
optionValue, err := tdlibClient.GetOption(&client.GetOptionRequest{
Name: "version",
})
if err != nil {
log.Fatalf("GetOption error: %s", err)
}
log.Printf("TDLib version: %s", optionValue.(*client.OptionValueString).Value)
me, err := tdlibClient.GetMe()
if err != nil {
log.Fatalf("GetMe error: %s", err)
}
log.Printf("Me: %s %s [%s]", me.FirstName, me.LastName, me.Username)
ch := make(chan os.Signal, 2)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
go func() {
<-ch
tdlibClient.Stop()
os.Exit(1)
}()
}

View file

@ -1,84 +0,0 @@
package main
import (
"log"
"os"
"os/signal"
"syscall"
tdlib "github.com/c0re100/gotdlib/client"
)
func GetSenderId(sender tdlib.MessageSender) int64 {
if sender.MessageSenderType() == "messageSenderUser" {
return sender.(*tdlib.MessageSenderUser).UserId
} else {
return sender.(*tdlib.MessageSenderChat).ChatId
}
}
func GetTdParameters() *tdlib.SetTdlibParametersRequest {
return &tdlib.SetTdlibParametersRequest{
UseTestDc: false,
DatabaseDirectory: "./tdlib-db",
FilesDirectory: "./tdlib-files",
UseFileDatabase: true,
UseChatInfoDatabase: true,
UseMessageDatabase: true,
UseSecretChats: false,
ApiId: 132712,
ApiHash: "e82c07ad653399a37baca8d1e498e472",
SystemLanguageCode: "en",
DeviceModel: "HuskyNG",
SystemVersion: "3.0",
ApplicationVersion: "3.0",
EnableStorageOptimizer: true,
IgnoreFileNames: false,
}
}
func main() {
tdlib.SetLogLevel(0)
tdlib.SetFilePath("./errors.txt")
authorizer := tdlib.ClientAuthorizer()
go tdlib.CliInteractor(authorizer)
authorizer.TdlibParameters <- GetTdParameters()
client, err := tdlib.NewClient(authorizer)
if err != nil {
log.Fatalf("NewClient error: %s", err)
}
// Handle SIGINT
ch := make(chan os.Signal, 2)
signal.Notify(ch, os.Interrupt, syscall.SIGINT)
signal.Notify(ch, os.Interrupt, syscall.SIGKILL)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
signal.Notify(ch, os.Interrupt, syscall.SIGQUIT)
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
go func() {
<-ch
client.Close()
}()
me, err := client.GetMe()
if err != nil {
log.Fatalf("GetMe error: %s", err)
}
log.Printf("%v connected", me.Usernames)
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
defer listener.Close()
for update := range listener.Updates {
updateMsg := update.(*tdlib.UpdateNewMessage)
chatId := updateMsg.Message.ChatId
senderId := GetSenderId(updateMsg.Message.SenderId)
msgId := updateMsg.Message.Id
log.Printf("[Received new message from chat %d]: Sender ID: %d, Message ID: %d", chatId, senderId, msgId)
}
}

5
example/go.mod Normal file
View file

@ -0,0 +1,5 @@
module go-tdlib-demo
go 1.20
require github.com/zelenin/go-tdlib v0.6.0 // indirect

2
example/go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/zelenin/go-tdlib v0.6.0 h1:dmdaPYcluNPiVuagM7D2FPT/nLYftTmqeKndz30vSDM=
github.com/zelenin/go-tdlib v0.6.0/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=

View file

@ -1,148 +0,0 @@
package main
import (
"log"
"os"
"os/signal"
"syscall"
tdlib "github.com/c0re100/gotdlib/client"
)
func GetSenderId(sender tdlib.MessageSender) int64 {
if sender.MessageSenderType() == "messageSenderUser" {
return sender.(*tdlib.MessageSenderUser).UserId
} else {
return sender.(*tdlib.MessageSenderChat).ChatId
}
}
func GetTdParameters() *tdlib.SetTdlibParametersRequest {
return &tdlib.SetTdlibParametersRequest{
UseTestDc: false,
DatabaseDirectory: "./tdlib-db",
FilesDirectory: "./tdlib-files",
UseFileDatabase: true,
UseChatInfoDatabase: true,
UseMessageDatabase: true,
UseSecretChats: false,
ApiId: 132712,
ApiHash: "e82c07ad653399a37baca8d1e498e472",
SystemLanguageCode: "en",
DeviceModel: "HuskyNG",
SystemVersion: "3.0",
ApplicationVersion: "3.0",
EnableStorageOptimizer: true,
IgnoreFileNames: false,
}
}
func main() {
tdlib.SetLogLevel(0)
tdlib.SetFilePath("./errors.txt")
authorizer := tdlib.ClientAuthorizer()
go tdlib.CliInteractor(authorizer)
authorizer.TdlibParameters <- GetTdParameters()
client, err := tdlib.NewClient(authorizer)
if err != nil {
log.Fatalf("NewClient error: %s", err)
}
// Handle SIGINT
ch := make(chan os.Signal, 2)
signal.Notify(ch, os.Interrupt, syscall.SIGINT)
signal.Notify(ch, os.Interrupt, syscall.SIGKILL)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
signal.Notify(ch, os.Interrupt, syscall.SIGQUIT)
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
go func() {
<-ch
client.Close()
}()
me, err := client.GetMe()
if err != nil {
log.Fatalf("GetMe error: %s", err)
}
log.Printf("%v connected", me.Usernames)
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
defer listener.Close()
for update := range listener.Updates {
updateMsg := update.(*tdlib.UpdateNewMessage)
chatId := updateMsg.Message.ChatId
senderId := GetSenderId(updateMsg.Message.SenderId)
msgId := updateMsg.Message.Id
if senderId == me.Id {
var msgText string
var msgEnt []*tdlib.TextEntity
switch updateMsg.Message.Content.MessageContentType() {
case "messageText":
msgText = updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
msgEnt = updateMsg.Message.Content.(*tdlib.MessageText).Text.Entities
}
cmd := tdlib.CheckCommand(msgText, msgEnt)
switch cmd {
case "/photo":
text, _ := tdlib.ParseTextEntities(&tdlib.ParseTextEntitiesRequest{
Text: "<b>test photo</b>",
ParseMode: &tdlib.TextParseModeHTML{},
})
m, err := client.SendMessage(&tdlib.SendMessageRequest{
ChatId: chatId,
ReplyTo: &tdlib.InputMessageReplyToMessage{
ChatId: chatId,
MessageId: msgId,
},
InputMessageContent: &tdlib.InputMessagePhoto{
Photo: &tdlib.InputFileLocal{
Path: "./myht9-1486821485193084928.jpg",
},
Caption: text,
},
})
if err != nil {
continue
}
log.Printf("Photo sent, ID: %d", m.Id)
case "/album":
text, _ := tdlib.ParseTextEntities(&tdlib.ParseTextEntitiesRequest{
Text: "<b>test album</b>",
ParseMode: &tdlib.TextParseModeHTML{},
})
m, err := client.SendMessageAlbum(&tdlib.SendMessageAlbumRequest{
ChatId: chatId,
ReplyTo: &tdlib.InputMessageReplyToMessage{
ChatId: chatId,
MessageId: msgId,
},
InputMessageContents: []tdlib.InputMessageContent{
&tdlib.InputMessagePhoto{
Photo: &tdlib.InputFileLocal{
Path: "./myht9-1486821485193084928.jpg",
},
Caption: text,
},
&tdlib.InputMessagePhoto{
Photo: &tdlib.InputFileLocal{
Path: "./hisagi_02-1486983199280738309.jpg",
},
},
},
})
if err != nil {
continue
}
log.Printf("Media album sent, Album ID: %v", m.Messages[0].MediaAlbumId)
}
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 153 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 116 KiB

View file

@ -1,110 +0,0 @@
package main
import (
"log"
"os"
"os/signal"
"syscall"
tdlib "github.com/c0re100/gotdlib/client"
)
func GetTdParameters() *tdlib.SetTdlibParametersRequest {
return &tdlib.SetTdlibParametersRequest{
UseTestDc: false,
DatabaseDirectory: "./tdlib-db",
FilesDirectory: "./tdlib-files",
UseFileDatabase: true,
UseChatInfoDatabase: true,
UseMessageDatabase: true,
UseSecretChats: false,
ApiId: 132712,
ApiHash: "e82c07ad653399a37baca8d1e498e472",
SystemLanguageCode: "en",
DeviceModel: "HuskyNG",
SystemVersion: "3.0",
ApplicationVersion: "3.0",
EnableStorageOptimizer: true,
IgnoreFileNames: false,
}
}
func main() {
tdlib.SetLogLevel(0)
tdlib.SetFilePath("./errors.txt")
// Set pending update list
tdlib.SetPendingUpdateType(&tdlib.UpdateNewMessage{})
// Of coz, you can set more than one type, depending on your needs
//tdlib.SetPendingUpdateType(&tdlib.UpdateNewMessage{}, &tdlib.UpdateMessageEdited{}, &tdlib.UpdateDeleteMessages{})
botToken := "your_bot_token"
authorizer := tdlib.BotAuthorizer(botToken)
authorizer.TdlibParameters <- GetTdParameters()
client, err := tdlib.NewClient(authorizer)
if err != nil {
log.Fatalf("NewClient error: %s", err)
}
// Handle SIGINT
ch := make(chan os.Signal, 2)
signal.Notify(ch, os.Interrupt, syscall.SIGINT)
signal.Notify(ch, os.Interrupt, syscall.SIGKILL)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
signal.Notify(ch, os.Interrupt, syscall.SIGQUIT)
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
go func() {
<-ch
client.Close()
}()
me, err := client.GetMe()
if err != nil {
log.Fatalf("GetMe error: %s", err)
}
log.Printf("%v connected", me.Usernames)
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
defer listener.Close()
for update := range listener.Updates {
updateMsg := update.(*tdlib.UpdateNewMessage)
chatId := updateMsg.Message.ChatId
msgId := updateMsg.Message.Id
var msgText string
var msgEnt []*tdlib.TextEntity
switch updateMsg.Message.Content.MessageContentType() {
case "messageText":
msgText = updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
msgEnt = updateMsg.Message.Content.(*tdlib.MessageText).Text.Entities
cmd := tdlib.CheckCommand(msgText, msgEnt)
switch cmd {
case "/ping":
text, _ := tdlib.ParseTextEntities(&tdlib.ParseTextEntitiesRequest{
Text: "<b>pong!</b>",
ParseMode: &tdlib.TextParseModeHTML{},
})
m, err := client.SendMessage(&tdlib.SendMessageRequest{
ChatId: chatId,
ReplyTo: &tdlib.InputMessageReplyToMessage{
ChatId: chatId,
MessageId: msgId,
},
InputMessageContent: &tdlib.InputMessageText{
Text: text,
},
})
if err != nil {
continue
}
log.Printf("Message sent, ID: %d", m.Id)
}
}
}
}

View file

@ -1,81 +0,0 @@
package main
import (
"log"
"os"
"os/signal"
"syscall"
tdlib "github.com/c0re100/gotdlib/client"
)
func GetSenderId(sender tdlib.MessageSender) int64 {
if sender.MessageSenderType() == "messageSenderUser" {
return sender.(*tdlib.MessageSenderUser).UserId
} else {
return sender.(*tdlib.MessageSenderChat).ChatId
}
}
func GetTdParameters() *tdlib.SetTdlibParametersRequest {
return &tdlib.SetTdlibParametersRequest{
UseTestDc: false,
DatabaseDirectory: "./tdlib-db",
FilesDirectory: "./tdlib-files",
UseFileDatabase: true,
UseChatInfoDatabase: true,
UseMessageDatabase: true,
UseSecretChats: false,
ApiId: 132712,
ApiHash: "e82c07ad653399a37baca8d1e498e472",
SystemLanguageCode: "en",
DeviceModel: "HuskyNG",
SystemVersion: "3.0",
ApplicationVersion: "3.0",
EnableStorageOptimizer: true,
IgnoreFileNames: false,
}
}
func main() {
tdlib.SetLogLevel(0)
tdlib.SetFilePath("./errors.txt")
authorizer := tdlib.ClientAuthorizer()
go tdlib.CliInteractor(authorizer)
authorizer.TdlibParameters <- GetTdParameters()
client, err := tdlib.NewClient(authorizer)
if err != nil {
log.Fatalf("NewClient error: %s", err)
}
// Handle SIGINT
ch := make(chan os.Signal, 2)
signal.Notify(ch, os.Interrupt, syscall.SIGINT)
signal.Notify(ch, os.Interrupt, syscall.SIGKILL)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
signal.Notify(ch, os.Interrupt, syscall.SIGQUIT)
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
go func() {
<-ch
client.Close()
}()
me, err := client.GetMe()
if err != nil {
log.Fatalf("GetMe error: %s", err)
}
log.Printf("%v connected", me.Usernames)
listener := client.GetListener()
defer listener.Close()
for update := range listener.RawUpdates {
if update.GetClass() == tdlib.ClassUpdate {
log.Printf("%#v", update)
}
}
}

9
go.mod
View file

@ -1,8 +1,3 @@
module github.com/c0re100/gotdlib module github.com/zelenin/go-tdlib
go 1.16 go 1.12
require (
github.com/google/uuid v1.3.1
golang.org/x/crypto v0.13.0
)

43
go.sum
View file

@ -1,43 +0,0 @@
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=