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 |
150
README.md
150
README.md
|
@ -1,145 +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.0
|
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
|
||||||
|
|
||||||
### Windows
|
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)
|
||||||
|
|
||||||
Build with environment variables:
|
[Here](example) are a few example codes about how to use **c0re100/gotdlib**.
|
||||||
|
|
||||||
```
|
|
||||||
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)
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
## 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)
|
|
|
@ -3,7 +3,10 @@ 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")
|
||||||
|
@ -43,7 +46,7 @@ func Authorize(client *Client, authorizationStateHandler AuthorizationStateHandl
|
||||||
}
|
}
|
||||||
|
|
||||||
type clientAuthorizer struct {
|
type clientAuthorizer struct {
|
||||||
TdlibParameters chan *TdlibParameters
|
TdlibParameters chan *SetTdlibParametersRequest
|
||||||
PhoneNumber chan string
|
PhoneNumber chan string
|
||||||
Code chan string
|
Code chan string
|
||||||
State chan AuthorizationState
|
State chan AuthorizationState
|
||||||
|
@ -52,7 +55,7 @@ type clientAuthorizer struct {
|
||||||
|
|
||||||
func ClientAuthorizer() *clientAuthorizer {
|
func ClientAuthorizer() *clientAuthorizer {
|
||||||
return &clientAuthorizer{
|
return &clientAuthorizer{
|
||||||
TdlibParameters: make(chan *TdlibParameters, 1),
|
TdlibParameters: make(chan *SetTdlibParametersRequest, 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),
|
||||||
|
@ -65,13 +68,7 @@ func (stateHandler *clientAuthorizer) Handle(client *Client, state Authorization
|
||||||
|
|
||||||
switch state.AuthorizationStateType() {
|
switch state.AuthorizationStateType() {
|
||||||
case TypeAuthorizationStateWaitTdlibParameters:
|
case TypeAuthorizationStateWaitTdlibParameters:
|
||||||
_, err := client.SetTdlibParameters(&SetTdlibParametersRequest{
|
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters)
|
||||||
Parameters: <-stateHandler.TdlibParameters,
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
|
|
||||||
case TypeAuthorizationStateWaitEncryptionKey:
|
|
||||||
_, err := client.CheckDatabaseEncryptionKey(&CheckDatabaseEncryptionKeyRequest{})
|
|
||||||
return err
|
return err
|
||||||
|
|
||||||
case TypeAuthorizationStateWaitPhoneNumber:
|
case TypeAuthorizationStateWaitPhoneNumber:
|
||||||
|
@ -150,10 +147,13 @@ func CliInteractor(clientAuthorizer *clientAuthorizer) {
|
||||||
|
|
||||||
case TypeAuthorizationStateWaitPassword:
|
case TypeAuthorizationStateWaitPassword:
|
||||||
fmt.Println("Enter password: ")
|
fmt.Println("Enter password: ")
|
||||||
var password string
|
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
|
||||||
fmt.Scanln(&password)
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
clientAuthorizer.Password <- password
|
clientAuthorizer.Password <- string(bytePassword)
|
||||||
|
|
||||||
case TypeAuthorizationStateReady:
|
case TypeAuthorizationStateReady:
|
||||||
return
|
return
|
||||||
|
@ -163,14 +163,14 @@ func CliInteractor(clientAuthorizer *clientAuthorizer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type botAuthorizer struct {
|
type botAuthorizer struct {
|
||||||
TdlibParameters chan *TdlibParameters
|
TdlibParameters chan *SetTdlibParametersRequest
|
||||||
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 *TdlibParameters, 1),
|
TdlibParameters: make(chan *SetTdlibParametersRequest, 1),
|
||||||
Token: make(chan string, 1),
|
Token: make(chan string, 1),
|
||||||
State: make(chan AuthorizationState, 10),
|
State: make(chan AuthorizationState, 10),
|
||||||
}
|
}
|
||||||
|
@ -185,13 +185,7 @@ func (stateHandler *botAuthorizer) Handle(client *Client, state AuthorizationSta
|
||||||
|
|
||||||
switch state.AuthorizationStateType() {
|
switch state.AuthorizationStateType() {
|
||||||
case TypeAuthorizationStateWaitTdlibParameters:
|
case TypeAuthorizationStateWaitTdlibParameters:
|
||||||
_, err := client.SetTdlibParameters(&SetTdlibParametersRequest{
|
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters)
|
||||||
Parameters: <-stateHandler.TdlibParameters,
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
|
|
||||||
case TypeAuthorizationStateWaitEncryptionKey:
|
|
||||||
_, err := client.CheckDatabaseEncryptionKey(&CheckDatabaseEncryptionKeyRequest{})
|
|
||||||
return err
|
return err
|
||||||
|
|
||||||
case TypeAuthorizationStateWaitPhoneNumber:
|
case TypeAuthorizationStateWaitPhoneNumber:
|
||||||
|
|
184
client/client.go
184
client/client.go
|
@ -1,20 +1,27 @@
|
||||||
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
|
||||||
listenerStore *listenerStore
|
pendingResp chan *Response
|
||||||
catchersStore *sync.Map
|
listenerStore *listenerStore
|
||||||
updatesTimeout time.Duration
|
catchersStore *sync.Map
|
||||||
catchTimeout time.Duration
|
successMsgStore *sync.Map
|
||||||
|
forwardMsgStore *sync.Map
|
||||||
|
updatesTimeout time.Duration
|
||||||
|
catchTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(*Client)
|
type Option func(*Client)
|
||||||
|
@ -37,18 +44,38 @@ func WithProxy(req *AddProxyRequest) Option {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithLogVerbosity(req *SetLogVerbosityLevelRequest) Option {
|
func SetLogLevel(level int32) {
|
||||||
return func(client *Client) {
|
_, _ = SetLogVerbosityLevel(&SetLogVerbosityLevelRequest{
|
||||||
client.SetLogVerbosityLevel(req)
|
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) {
|
func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...Option) (*Client, error) {
|
||||||
client := &Client{
|
client := &Client{
|
||||||
jsonClient: NewJsonClient(),
|
jsonClient: NewJsonClient(),
|
||||||
responses: make(chan *Response, 1000),
|
responses: make(chan *Response, 1000),
|
||||||
listenerStore: newListenerStore(),
|
pendingResp: make(chan *Response, 1000),
|
||||||
catchersStore: &sync.Map{},
|
listenerStore: newListenerStore(),
|
||||||
|
catchersStore: &sync.Map{},
|
||||||
|
successMsgStore: &sync.Map{},
|
||||||
|
forwardMsgStore: &sync.Map{},
|
||||||
}
|
}
|
||||||
|
|
||||||
client.extraGenerator = UuidV4Generator()
|
client.extraGenerator = UuidV4Generator()
|
||||||
|
@ -60,6 +87,7 @@ 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)
|
||||||
|
@ -70,31 +98,76 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
|
||||||
return client, nil
|
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() {
|
func (client *Client) receiver() {
|
||||||
for response := range client.responses {
|
for response := range client.responses {
|
||||||
if response.Extra != "" {
|
client.processResponse(response)
|
||||||
value, ok := client.catchersStore.Load(response.Extra)
|
}
|
||||||
if ok {
|
}
|
||||||
value.(chan *Response) <- response
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
typ, err := UnmarshalType(response.Data)
|
func (client *Client) processPendingResponse() {
|
||||||
if err != nil {
|
// No need to process pending response if no pending list.
|
||||||
continue
|
if len(pendingUpdateType) == 0 {
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
needGc := false
|
// Wait for listener to be ready.
|
||||||
for _, listener := range client.listenerStore.Listeners() {
|
for {
|
||||||
if listener.IsActive() {
|
if len(client.listenerStore.Listeners()) > 0 {
|
||||||
listener.Updates <- typ
|
break
|
||||||
} else {
|
|
||||||
needGc = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if needGc {
|
|
||||||
client.listenerStore.gc()
|
|
||||||
}
|
}
|
||||||
|
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 {
|
select {
|
||||||
case response := <-catcher:
|
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():
|
case <-ctx.Done():
|
||||||
return nil, errors.New("response catching timeout")
|
return nil, errors.New("response catching timeout")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) GetListener() *Listener {
|
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{
|
listener := &Listener{
|
||||||
isActive: true,
|
isActive: true,
|
||||||
Updates: make(chan Type, 1000),
|
Updates: make(chan Type, channelCapacity),
|
||||||
|
Filter: msgType,
|
||||||
}
|
}
|
||||||
client.listenerStore.Add(listener)
|
client.listenerStore.Add(listener)
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,66 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"strings"
|
||||||
"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 {
|
||||||
var uuid [16]byte
|
return uuid.NewString()
|
||||||
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:])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 ""
|
||||||
|
}
|
||||||
|
|
25114
client/function.go
25114
client/function.go
File diff suppressed because it is too large
Load diff
|
@ -45,9 +45,11 @@ func (store *listenerStore) gc() {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Listener struct {
|
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() {
|
||||||
|
@ -55,7 +57,12 @@ func (listener *Listener) Close() {
|
||||||
defer listener.mu.Unlock()
|
defer listener.mu.Unlock()
|
||||||
|
|
||||||
listener.isActive = false
|
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 {
|
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,9 +1,14 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
/*
|
//#cgo linux CFLAGS: -I/usr/local/include
|
||||||
#include <stdlib.h>
|
//#cgo darwin CFLAGS: -I/usr/local/include
|
||||||
#include <td/telegram/td_json_client.h>
|
//#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 "C"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
63437
client/type.go
63437
client/type.go
File diff suppressed because it is too large
Load diff
27412
client/unmarshaler.go
27412
client/unmarshaler.go
File diff suppressed because it is too large
Load diff
|
@ -4,12 +4,11 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/zelenin/go-tdlib/codegen"
|
"github.com/c0re100/gotdlib/codegen"
|
||||||
"github.com/zelenin/go-tdlib/tlparser"
|
"github.com/c0re100/gotdlib/tlparser"
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
|
@ -25,22 +24,21 @@ 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", "./tdlib", "output directory")
|
flag.StringVar(&config.outputDirPath, "outputDir", "./client", "output directory")
|
||||||
flag.StringVar(&config.packageName, "package", "tdlib", "package name")
|
flag.StringVar(&config.packageName, "package", "client", "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()
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
log.Fatalf("http.Get error: %s", err)
|
log.Fatalf(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
schema, err := tlparser.Parse(resp.Body)
|
schema, err := tlparser.Parse(bufio.NewReader(tl))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("schema parse error: %s", err)
|
log.Fatalf("schema parse error: %s", err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/zelenin/go-tdlib/tlparser"
|
"github.com/c0re100/gotdlib/tlparser"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/zelenin/go-tdlib/tlparser"
|
"github.com/c0re100/gotdlib/tlparser"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateFunctions(schema *tlparser.Schema, packageName string) []byte {
|
func GenerateFunctions(schema *tlparser.Schema, packageName string) []byte {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package codegen
|
package codegen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/zelenin/go-tdlib/tlparser"
|
"github.com/c0re100/gotdlib/tlparser"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,7 +3,7 @@ package codegen
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/zelenin/go-tdlib/tlparser"
|
"github.com/c0re100/gotdlib/tlparser"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateTypes(schema *tlparser.Schema, packageName string) []byte {
|
func GenerateTypes(schema *tlparser.Schema, packageName string) []byte {
|
||||||
|
|
|
@ -3,7 +3,7 @@ package codegen
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/zelenin/go-tdlib/tlparser"
|
"github.com/c0re100/gotdlib/tlparser"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateUnmarshalers(schema *tlparser.Schema, packageName string) []byte {
|
func GenerateUnmarshalers(schema *tlparser.Schema, packageName string) []byte {
|
||||||
|
|
5716
data/td_api.tl
5716
data/td_api.tl
File diff suppressed because it is too large
Load diff
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
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)
|
||||||
|
}
|
||||||
|
}
|
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