Compare commits
49 commits
fresh-tdli
...
c0re100
Author | SHA1 | Date | |
---|---|---|---|
9a3301fbd7 | |||
83df5e2abb | |||
9e42c4a4ee | |||
af2b93f686 | |||
26f72d96ce | |||
0b8a2f54dd | |||
7a1c7f37a2 | |||
e722882047 | |||
47887c89e1 | |||
993b734553 | |||
d0f51e73ee | |||
869b97df28 | |||
7df275842c | |||
712b01c11c | |||
abdd0dfd48 | |||
65692be746 | |||
3b10777734 | |||
0b0467fbb7 | |||
fffcf86198 | |||
51b9da9304 | |||
52c9a63fab | |||
d5cf5072c8 | |||
bbfcb2f946 | |||
1011dc13ae | |||
94b077458b | |||
36a547a560 | |||
f70d5bf7e6 | |||
1d7608dd7c | |||
f312f1cc07 | |||
f36c0db78b | |||
6400f59b1b | |||
a339293774 | |||
a7ee5501e3 | |||
cc7712e2c4 | |||
7196ad9adc | |||
82bdbff466 | |||
e15362a612 | |||
b535766aa0 | |||
469cae3b44 | |||
b2327b9644 | |||
be6cbfe44d | |||
d194b79c3c | |||
a0548a12c7 | |||
6ae3f83566 | |||
352e5d8968 | |||
187cc0de75 | |||
611fb8a865 | |||
7a29e8eead | |||
3032e21dcd |
2
Makefile
2
Makefile
|
@ -1,4 +1,4 @@
|
|||
TAG := v1.8.14
|
||||
TAG := v1.8.0
|
||||
|
||||
schema-update:
|
||||
curl https://raw.githubusercontent.com/tdlib/td/${TAG}/td/generate/scheme/td_api.tl 2>/dev/null > ./data/td_api.tl
|
||||
|
|
163
README.md
163
README.md
|
@ -1,158 +1,15 @@
|
|||
# go-tdlib
|
||||
## Forked library for [modded TDLib](https://github.com/c0re100/td)
|
||||
|
||||
Go wrapper for [TDLib (Telegram Database Library)](https://github.com/tdlib/td) with full support of TDLib v1.8.14
|
||||
When I'm refactoring my own bot from `Arman92/go-tdlib` to `zelenin/go-tdlib `
|
||||
|
||||
## TDLib installation
|
||||
I realized that zelenin's library doesn't meet my need😕
|
||||
|
||||
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`.
|
||||
So I fork it and make some changes
|
||||
|
||||
### Note: Compatible with TDLib v1.8.14 only!
|
||||
1. Static build by default
|
||||
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)
|
||||
|
||||
### 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)
|
||||
[Here](example) are a few example codes about how to use **c0re100/gotdlib**.
|
|
@ -3,47 +3,14 @@ package client
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
||||
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 {
|
||||
Handle(client *Client, state AuthorizationState) error
|
||||
Close()
|
||||
|
@ -79,7 +46,7 @@ func Authorize(client *Client, authorizationStateHandler AuthorizationStateHandl
|
|||
}
|
||||
|
||||
type clientAuthorizer struct {
|
||||
TdlibParameters chan *TdlibParameters
|
||||
TdlibParameters chan *SetTdlibParametersRequest
|
||||
PhoneNumber chan string
|
||||
Code chan string
|
||||
State chan AuthorizationState
|
||||
|
@ -88,7 +55,7 @@ type clientAuthorizer struct {
|
|||
|
||||
func ClientAuthorizer() *clientAuthorizer {
|
||||
return &clientAuthorizer{
|
||||
TdlibParameters: make(chan *TdlibParameters, 1),
|
||||
TdlibParameters: make(chan *SetTdlibParametersRequest, 1),
|
||||
PhoneNumber: make(chan string, 1),
|
||||
Code: make(chan string, 1),
|
||||
State: make(chan AuthorizationState, 10),
|
||||
|
@ -101,25 +68,7 @@ func (stateHandler *clientAuthorizer) Handle(client *Client, state Authorization
|
|||
|
||||
switch state.AuthorizationStateType() {
|
||||
case TypeAuthorizationStateWaitTdlibParameters:
|
||||
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,
|
||||
})
|
||||
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters)
|
||||
return err
|
||||
|
||||
case TypeAuthorizationStateWaitPhoneNumber:
|
||||
|
@ -133,21 +82,12 @@ func (stateHandler *clientAuthorizer) Handle(client *Client, state Authorization
|
|||
})
|
||||
return err
|
||||
|
||||
case TypeAuthorizationStateWaitEmailAddress:
|
||||
return ErrNotSupportedAuthorizationState
|
||||
|
||||
case TypeAuthorizationStateWaitEmailCode:
|
||||
return ErrNotSupportedAuthorizationState
|
||||
|
||||
case TypeAuthorizationStateWaitCode:
|
||||
_, err := client.CheckAuthenticationCode(&CheckAuthenticationCodeRequest{
|
||||
Code: <-stateHandler.Code,
|
||||
})
|
||||
return err
|
||||
|
||||
case TypeAuthorizationStateWaitOtherDeviceConfirmation:
|
||||
return ErrNotSupportedAuthorizationState
|
||||
|
||||
case TypeAuthorizationStateWaitRegistration:
|
||||
return ErrNotSupportedAuthorizationState
|
||||
|
||||
|
@ -197,12 +137,6 @@ func CliInteractor(clientAuthorizer *clientAuthorizer) {
|
|||
|
||||
clientAuthorizer.PhoneNumber <- phoneNumber
|
||||
|
||||
case TypeAuthorizationStateWaitEmailAddress:
|
||||
return
|
||||
|
||||
case TypeAuthorizationStateWaitEmailCode:
|
||||
return
|
||||
|
||||
case TypeAuthorizationStateWaitCode:
|
||||
var code string
|
||||
|
||||
|
@ -211,18 +145,15 @@ func CliInteractor(clientAuthorizer *clientAuthorizer) {
|
|||
|
||||
clientAuthorizer.Code <- code
|
||||
|
||||
case TypeAuthorizationStateWaitOtherDeviceConfirmation:
|
||||
return
|
||||
|
||||
case TypeAuthorizationStateWaitRegistration:
|
||||
return
|
||||
|
||||
case TypeAuthorizationStateWaitPassword:
|
||||
fmt.Println("Enter password: ")
|
||||
var password string
|
||||
fmt.Scanln(&password)
|
||||
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
clientAuthorizer.Password <- password
|
||||
clientAuthorizer.Password <- string(bytePassword)
|
||||
|
||||
case TypeAuthorizationStateReady:
|
||||
return
|
||||
|
@ -232,14 +163,14 @@ func CliInteractor(clientAuthorizer *clientAuthorizer) {
|
|||
}
|
||||
|
||||
type botAuthorizer struct {
|
||||
TdlibParameters chan *TdlibParameters
|
||||
TdlibParameters chan *SetTdlibParametersRequest
|
||||
Token chan string
|
||||
State chan AuthorizationState
|
||||
}
|
||||
|
||||
func BotAuthorizer(token string) *botAuthorizer {
|
||||
botAuthorizer := &botAuthorizer{
|
||||
TdlibParameters: make(chan *TdlibParameters, 1),
|
||||
TdlibParameters: make(chan *SetTdlibParametersRequest, 1),
|
||||
Token: make(chan string, 1),
|
||||
State: make(chan AuthorizationState, 10),
|
||||
}
|
||||
|
@ -254,25 +185,7 @@ func (stateHandler *botAuthorizer) Handle(client *Client, state AuthorizationSta
|
|||
|
||||
switch state.AuthorizationStateType() {
|
||||
case TypeAuthorizationStateWaitTdlibParameters:
|
||||
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,
|
||||
})
|
||||
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters)
|
||||
return err
|
||||
|
||||
case TypeAuthorizationStateWaitPhoneNumber:
|
||||
|
|
184
client/client.go
184
client/client.go
|
@ -1,20 +1,27 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var pendingUpdateType []Type
|
||||
|
||||
type Client struct {
|
||||
jsonClient *JsonClient
|
||||
extraGenerator ExtraGenerator
|
||||
responses chan *Response
|
||||
listenerStore *listenerStore
|
||||
catchersStore *sync.Map
|
||||
updatesTimeout time.Duration
|
||||
catchTimeout time.Duration
|
||||
jsonClient *JsonClient
|
||||
extraGenerator ExtraGenerator
|
||||
responses chan *Response
|
||||
pendingResp chan *Response
|
||||
listenerStore *listenerStore
|
||||
catchersStore *sync.Map
|
||||
successMsgStore *sync.Map
|
||||
forwardMsgStore *sync.Map
|
||||
updatesTimeout time.Duration
|
||||
catchTimeout time.Duration
|
||||
}
|
||||
|
||||
type Option func(*Client)
|
||||
|
@ -37,18 +44,38 @@ func WithProxy(req *AddProxyRequest) Option {
|
|||
}
|
||||
}
|
||||
|
||||
func WithLogVerbosity(req *SetLogVerbosityLevelRequest) Option {
|
||||
return func(client *Client) {
|
||||
client.SetLogVerbosityLevel(req)
|
||||
func SetLogLevel(level int32) {
|
||||
_, _ = SetLogVerbosityLevel(&SetLogVerbosityLevelRequest{
|
||||
NewVerbosityLevel: level,
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...Option) (*Client, error) {
|
||||
client := &Client{
|
||||
jsonClient: NewJsonClient(),
|
||||
responses: make(chan *Response, 1000),
|
||||
listenerStore: newListenerStore(),
|
||||
catchersStore: &sync.Map{},
|
||||
jsonClient: NewJsonClient(),
|
||||
responses: make(chan *Response, 1000),
|
||||
pendingResp: make(chan *Response, 1000),
|
||||
listenerStore: newListenerStore(),
|
||||
catchersStore: &sync.Map{},
|
||||
successMsgStore: &sync.Map{},
|
||||
forwardMsgStore: &sync.Map{},
|
||||
}
|
||||
|
||||
client.extraGenerator = UuidV4Generator()
|
||||
|
@ -60,6 +87,7 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
|
|||
|
||||
tdlibInstance.addClient(client)
|
||||
|
||||
go client.processPendingResponse()
|
||||
go client.receiver()
|
||||
|
||||
err := Authorize(client, authorizationStateHandler)
|
||||
|
@ -70,31 +98,76 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
|
|||
return client, nil
|
||||
}
|
||||
|
||||
func (client *Client) processResponse(response *Response) {
|
||||
if response.Extra != "" {
|
||||
value, ok := client.catchersStore.Load(response.Extra)
|
||||
if ok {
|
||||
value.(chan *Response) <- response
|
||||
}
|
||||
}
|
||||
|
||||
typ, err := UnmarshalType(response.Data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
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
|
||||
listener.Updates <- typ
|
||||
} else if listener.IsActive() && listener.RawUpdates != nil { // All updates go to RawUpdates channel if filter is empty
|
||||
listener.RawUpdates <- typ
|
||||
} else if !listener.IsActive() { // GC inactive listener
|
||||
needGc = true
|
||||
}
|
||||
}
|
||||
if needGc {
|
||||
client.listenerStore.gc()
|
||||
}
|
||||
}
|
||||
|
||||
func (client *Client) receiver() {
|
||||
for response := range client.responses {
|
||||
if response.Extra != "" {
|
||||
value, ok := client.catchersStore.Load(response.Extra)
|
||||
if ok {
|
||||
value.(chan *Response) <- response
|
||||
}
|
||||
}
|
||||
client.processResponse(response)
|
||||
}
|
||||
}
|
||||
|
||||
typ, err := UnmarshalType(response.Data)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
func (client *Client) processPendingResponse() {
|
||||
// No need to process pending response if no pending list.
|
||||
if len(pendingUpdateType) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
needGc := false
|
||||
for _, listener := range client.listenerStore.Listeners() {
|
||||
if listener.IsActive() {
|
||||
listener.Updates <- typ
|
||||
} else {
|
||||
needGc = true
|
||||
}
|
||||
}
|
||||
if needGc {
|
||||
client.listenerStore.gc()
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,17 +190,56 @@ func (client *Client) Send(req Request) (*Response, error) {
|
|||
|
||||
select {
|
||||
case response := <-catcher:
|
||||
return response, nil
|
||||
if response.Type != "error" && req.Type == "sendMessage" {
|
||||
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():
|
||||
return nil, errors.New("response catching timeout")
|
||||
}
|
||||
}
|
||||
|
||||
func (client *Client) GetListener() *Listener {
|
||||
listener := &Listener{
|
||||
isActive: true,
|
||||
RawUpdates: 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, 1000),
|
||||
Updates: make(chan Type, channelCapacity),
|
||||
Filter: msgType,
|
||||
}
|
||||
client.listenerStore.Add(listener)
|
||||
|
||||
|
|
|
@ -1,20 +1,66 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ExtraGenerator func() string
|
||||
|
||||
func UuidV4Generator() ExtraGenerator {
|
||||
return func() string {
|
||||
var uuid [16]byte
|
||||
rand.Read(uuid[:])
|
||||
|
||||
uuid[6] = (uuid[6] & 0x0f) | 0x40
|
||||
uuid[8] = (uuid[8] & 0x3f) | 0x80
|
||||
|
||||
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", uuid[:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
|
||||
return uuid.NewString()
|
||||
}
|
||||
}
|
||||
|
||||
func IsCommand(text string) bool {
|
||||
if text != "" {
|
||||
if text[0] == '/' {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func CheckCommand(text string, entities []*TextEntity) string {
|
||||
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 ""
|
||||
}
|
||||
|
|
27619
client/function.go
27619
client/function.go
File diff suppressed because it is too large
Load diff
|
@ -45,9 +45,11 @@ func (store *listenerStore) gc() {
|
|||
}
|
||||
|
||||
type Listener struct {
|
||||
mu sync.Mutex
|
||||
isActive bool
|
||||
Updates chan Type
|
||||
mu sync.Mutex
|
||||
isActive bool
|
||||
Updates chan Type
|
||||
RawUpdates chan Type
|
||||
Filter Type
|
||||
}
|
||||
|
||||
func (listener *Listener) Close() {
|
||||
|
@ -55,7 +57,12 @@ func (listener *Listener) Close() {
|
|||
defer listener.mu.Unlock()
|
||||
|
||||
listener.isActive = false
|
||||
close(listener.Updates)
|
||||
if listener.Updates != nil {
|
||||
close(listener.Updates)
|
||||
}
|
||||
if listener.RawUpdates != nil {
|
||||
close(listener.RawUpdates)
|
||||
}
|
||||
}
|
||||
|
||||
func (listener *Listener) IsActive() bool {
|
||||
|
|
|
@ -1,52 +0,0 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
package puller
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var EOP = errors.New("end of pull")
|
|
@ -1,53 +0,0 @@
|
|||
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++
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
//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"
|
|
@ -1,13 +0,0 @@
|
|||
//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"
|
|
@ -1,13 +0,0 @@
|
|||
//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"
|
|
@ -1,10 +1,14 @@
|
|||
package client
|
||||
|
||||
/*
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <td/telegram/td_json_client.h>
|
||||
*/
|
||||
//#cgo linux CFLAGS: -I/usr/local/include
|
||||
//#cgo darwin CFLAGS: -I/usr/local/include
|
||||
//#cgo windows CFLAGS: -IE:/src/tdlib -IE:/src/tdlib/build
|
||||
//#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
|
||||
//#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 (
|
||||
|
@ -84,8 +88,7 @@ func (instance *tdlib) receive(timeout time.Duration) (*Response, error) {
|
|||
return nil, errors.New("update receiving timeout")
|
||||
}
|
||||
|
||||
resultLen := C.strlen(result)
|
||||
data := C.GoBytes(unsafe.Pointer(result), C.int(resultLen))
|
||||
data := []byte(C.GoString(result))
|
||||
|
||||
var resp Response
|
||||
|
||||
|
@ -109,8 +112,7 @@ func Execute(req Request) (*Response, error) {
|
|||
return nil, errors.New("request can't be parsed")
|
||||
}
|
||||
|
||||
resultLen := C.strlen(result)
|
||||
data = C.GoBytes(unsafe.Pointer(result), C.int(resultLen))
|
||||
data = []byte(C.GoString(result))
|
||||
|
||||
var resp Response
|
||||
|
||||
|
|
42016
client/type.go
42016
client/type.go
File diff suppressed because it is too large
Load diff
21886
client/unmarshaler.go
21886
client/unmarshaler.go
File diff suppressed because it is too large
Load diff
|
@ -4,12 +4,11 @@ import (
|
|||
"bufio"
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/zelenin/go-tdlib/codegen"
|
||||
"github.com/zelenin/go-tdlib/tlparser"
|
||||
"github.com/c0re100/gotdlib/codegen"
|
||||
"github.com/c0re100/gotdlib/tlparser"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
|
@ -25,22 +24,21 @@ func main() {
|
|||
var config config
|
||||
|
||||
flag.StringVar(&config.version, "version", "", "TDLib version")
|
||||
flag.StringVar(&config.outputDirPath, "outputDir", "./tdlib", "output directory")
|
||||
flag.StringVar(&config.packageName, "package", "tdlib", "package name")
|
||||
flag.StringVar(&config.outputDirPath, "outputDir", "./client", "output directory")
|
||||
flag.StringVar(&config.packageName, "package", "client", "package name")
|
||||
flag.StringVar(&config.functionFileName, "functionFile", "function.go", "functions filename")
|
||||
flag.StringVar(&config.typeFileName, "typeFile", "type.go", "types filename")
|
||||
flag.StringVar(&config.unmarshalerFileName, "unmarshalerFile", "unmarshaler.go", "unmarshalers filename")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
resp, err := http.Get("https://raw.githubusercontent.com/tdlib/td/" + config.version + "/td/generate/scheme/td_api.tl")
|
||||
tl, err := os.Open("./data/td_api.tl")
|
||||
if err != nil {
|
||||
log.Fatalf("http.Get error: %s", err)
|
||||
log.Fatalf(err.Error())
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
schema, err := tlparser.Parse(resp.Body)
|
||||
schema, err := tlparser.Parse(bufio.NewReader(tl))
|
||||
if err != nil {
|
||||
log.Fatalf("schema parse error: %s", err)
|
||||
return
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"bufio"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"github.com/zelenin/go-tdlib/tlparser"
|
||||
"github.com/c0re100/gotdlib/tlparser"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/zelenin/go-tdlib/tlparser"
|
||||
"github.com/c0re100/gotdlib/tlparser"
|
||||
)
|
||||
|
||||
func GenerateFunctions(schema *tlparser.Schema, packageName string) []byte {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package codegen
|
||||
|
||||
import (
|
||||
"github.com/zelenin/go-tdlib/tlparser"
|
||||
"github.com/c0re100/gotdlib/tlparser"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
|
|
@ -3,7 +3,7 @@ package codegen
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/zelenin/go-tdlib/tlparser"
|
||||
"github.com/c0re100/gotdlib/tlparser"
|
||||
)
|
||||
|
||||
func GenerateTypes(schema *tlparser.Schema, packageName string) []byte {
|
||||
|
|
|
@ -3,7 +3,7 @@ package codegen
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/zelenin/go-tdlib/tlparser"
|
||||
"github.com/c0re100/gotdlib/tlparser"
|
||||
)
|
||||
|
||||
func GenerateUnmarshalers(schema *tlparser.Schema, packageName string) []byte {
|
||||
|
|
8837
data/td_api.json
8837
data/td_api.json
File diff suppressed because it is too large
Load diff
1516
data/td_api.tl
1516
data/td_api.tl
File diff suppressed because it is too large
Load diff
|
@ -1,85 +0,0 @@
|
|||
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"]
|
22
example/README.md
Normal file
22
example/README.md
Normal file
|
@ -0,0 +1,22 @@
|
|||
## 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.
|
105
example/bot/Bot.go
Normal file
105
example/bot/Bot.go
Normal file
|
@ -0,0 +1,105 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
131
example/command/ReplyCommand.go
Normal file
131
example/command/ReplyCommand.go
Normal file
|
@ -0,0 +1,131 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
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)
|
||||
}()
|
||||
}
|
84
example/event/CustomEventFilter.go
Normal file
84
example/event/CustomEventFilter.go
Normal file
|
@ -0,0 +1,84 @@
|
|||
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)
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
module go-tdlib-demo
|
||||
|
||||
go 1.20
|
||||
|
||||
require github.com/zelenin/go-tdlib v0.6.0 // indirect
|
|
@ -1,2 +0,0 @@
|
|||
github.com/zelenin/go-tdlib v0.6.0 h1:dmdaPYcluNPiVuagM7D2FPT/nLYftTmqeKndz30vSDM=
|
||||
github.com/zelenin/go-tdlib v0.6.0/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=
|
148
example/media/Photo_or_Album.go
Normal file
148
example/media/Photo_or_Album.go
Normal file
|
@ -0,0 +1,148 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
BIN
example/media/hisagi_02-1486983199280738309.jpg
Normal file
BIN
example/media/hisagi_02-1486983199280738309.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 153 KiB |
BIN
example/media/myht9-1486821485193084928.jpg
Normal file
BIN
example/media/myht9-1486821485193084928.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 116 KiB |
110
example/pending/PendingUpdate.go
Normal file
110
example/pending/PendingUpdate.go
Normal file
|
@ -0,0 +1,110 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
81
example/raw_update/raw.go
Normal file
81
example/raw_update/raw.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
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
9
go.mod
|
@ -1,3 +1,8 @@
|
|||
module github.com/zelenin/go-tdlib
|
||||
module github.com/c0re100/gotdlib
|
||||
|
||||
go 1.12
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.3.1
|
||||
golang.org/x/crypto v0.13.0
|
||||
)
|
||||
|
|
43
go.sum
Normal file
43
go.sum
Normal file
|
@ -0,0 +1,43 @@
|
|||
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=
|
Loading…
Reference in a new issue