Compare commits

...

49 commits

Author SHA1 Message Date
c0re100
9a3301fbd7
Update to TDLib 1.8.21 2023-11-07 05:47:05 +08:00
ɥnsʞʎ
83df5e2abb
Fix word spelling, update new UUID package 2023-09-19 00:21:53 +08:00
DukeAnn
9e42c4a4ee 修正单词拼写,更新新的UUID包。(Fix word spelling, update new UUID package) 2023-09-18 21:22:23 +08:00
c0re100
af2b93f686
Update to TDLib 1.8.18 2023-09-16 15:09:40 +08:00
c0re100
26f72d96ce
Restore sendChatScreenshotTakenNotification 2023-08-30 03:53:53 +08:00
c0re100
0b8a2f54dd
Update to TDLib 1.8.16 2023-08-16 02:28:13 +08:00
c0re100
7a1c7f37a2
Update to TDLib 1.8.15 2023-07-25 02:35:55 +08:00
c0re100
e722882047
Update to TDLib 1.8.13 2023-03-26 17:37:06 +08:00
c0re100
47887c89e1
Update to TDLib 1.8.12 2023-03-11 19:13:56 +08:00
c0re100
993b734553
Update example 2023-03-11 19:13:40 +08:00
c0re100
d0f51e73ee
Fix tl scheme 2023-02-05 07:22:52 +08:00
c0re100
869b97df28
Update to TDLib 1.8.11 2023-02-05 06:54:46 +08:00
c0re100
7df275842c
Update to TDLib 1.8.10 2022-12-31 02:28:59 +08:00
c0re100
712b01c11c
Update to TDLib 1.8.9 2022-12-08 04:04:16 +08:00
c0re100
abdd0dfd48
Update to TDLib 1.8.8 2022-11-08 02:50:52 +08:00
c0re100
65692be746
Update authorization state 2022-09-18 09:49:37 +08:00
c0re100
3b10777734
Update to TDLib 1.8.6 2022-09-18 09:34:29 +08:00
c0re100
0b0467fbb7
Remove forwardMessages handler
Bad implementation and it should handled by client side
2022-08-14 22:01:42 +08:00
c0re100
fffcf86198
Update to TDLib 1.8.5 2022-08-13 23:24:07 +08:00
c0re100
51b9da9304
Improve forwardMessages catcher 2022-08-03 09:52:06 +08:00
c0re100
52c9a63fab
Handle updateMessageSendSucceeded if client forward a message 2022-08-02 03:56:09 +08:00
c0re100
d5cf5072c8
Replace message id strictly 2022-08-02 03:53:04 +08:00
c0re100
bbfcb2f946
Update to TDLib 1.8.4 2022-06-22 02:10:07 +08:00
c0re100
1011dc13ae
Update to TDLib master 2022-05-09 19:35:19 +08:00
c0re100
94b077458b
Update to TDLib 1.8.3 2022-04-25 04:33:39 +08:00
c0re100
36a547a560
Update scheme 2022-03-31 16:54:35 +08:00
c0re100
f70d5bf7e6
Update to latest TDLib 2022-03-13 12:04:12 +08:00
c0re100
1d7608dd7c
Update to TDLib 1.8.1 2022-02-07 02:36:17 +08:00
c0re100
f312f1cc07
Update README.md 2022-02-06 11:26:42 +08:00
c0re100
f36c0db78b
Add one more example for command args 2022-02-06 11:25:46 +08:00
c0re100
6400f59b1b
Support pending updates 2022-02-06 11:22:20 +08:00
c0re100
a339293774
Close instance
It should not destroy :P
2022-02-06 10:39:26 +08:00
c0re100
a7ee5501e3
Fix closed channel 2022-02-01 03:17:59 +08:00
c0re100
cc7712e2c4
Implement terminal style password input 2022-01-30 21:48:54 +08:00
c0re100
7196ad9adc
Add some example code 2022-01-30 03:51:49 +08:00
c0re100
82bdbff466
Command Parser 2022-01-30 01:05:51 +08:00
c0re100
e15362a612
Handle updateMessageSendSucceeded if client send a text/dice message 2022-01-30 01:04:23 +08:00
c0re100
b535766aa0
Implement message type filter for listener 2022-01-29 09:20:54 +08:00
c0re100
469cae3b44
Make TDLib log output easily 2022-01-28 07:45:45 +08:00
c0re100
b2327b9644
Update function 2022-01-28 07:45:31 +08:00
c0re100
be6cbfe44d
Generate to client dir 2022-01-28 07:44:54 +08:00
c0re100
d194b79c3c
Merge branch 'master' of https://github.com/zelenin/go-tdlib 2022-01-28 07:33:36 +08:00
c0re100
a0548a12c7
Make TDLib log output easily 2022-01-27 13:41:34 +08:00
c0re100
6ae3f83566
No longer needed 2022-01-27 08:22:40 +08:00
c0re100
352e5d8968
Always static build 2022-01-27 08:08:21 +08:00
c0re100
187cc0de75
Update all function 2022-01-27 05:06:41 +08:00
c0re100
611fb8a865
Local tl parser 2022-01-27 05:05:22 +08:00
c0re100
7a29e8eead
Update td_api.tl 2022-01-27 05:05:08 +08:00
c0re100
3032e21dcd
Rename 2022-01-27 04:48:37 +08:00
34 changed files with 76415 additions and 47483 deletions

150
README.md
View file

@ -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:
```
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)
[Here](example) are a few example codes about how to use **c0re100/gotdlib**.

View file

@ -3,7 +3,10 @@ package client
import (
"errors"
"fmt"
"syscall"
"time"
"golang.org/x/crypto/ssh/terminal"
)
var ErrNotSupportedAuthorizationState = errors.New("not supported state")
@ -43,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
@ -52,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),
@ -65,13 +68,7 @@ func (stateHandler *clientAuthorizer) Handle(client *Client, state Authorization
switch state.AuthorizationStateType() {
case TypeAuthorizationStateWaitTdlibParameters:
_, err := client.SetTdlibParameters(&SetTdlibParametersRequest{
Parameters: <-stateHandler.TdlibParameters,
})
return err
case TypeAuthorizationStateWaitEncryptionKey:
_, err := client.CheckDatabaseEncryptionKey(&CheckDatabaseEncryptionKeyRequest{})
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters)
return err
case TypeAuthorizationStateWaitPhoneNumber:
@ -150,10 +147,13 @@ func CliInteractor(clientAuthorizer *clientAuthorizer) {
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
@ -163,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),
}
@ -185,13 +185,7 @@ func (stateHandler *botAuthorizer) Handle(client *Client, state AuthorizationSta
switch state.AuthorizationStateType() {
case TypeAuthorizationStateWaitTdlibParameters:
_, err := client.SetTdlibParameters(&SetTdlibParametersRequest{
Parameters: <-stateHandler.TdlibParameters,
})
return err
case TypeAuthorizationStateWaitEncryptionKey:
_, err := client.CheckDatabaseEncryptionKey(&CheckDatabaseEncryptionKeyRequest{})
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters)
return err
case TypeAuthorizationStateWaitPhoneNumber:

View file

@ -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)

View file

@ -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 ""
}

File diff suppressed because it is too large Load diff

View file

@ -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 {

View file

@ -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
}
}
}

View file

@ -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
}
}
}

View file

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

View file

@ -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++
}
}

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -1,9 +1,14 @@
package client
/*
#include <stdlib.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 (

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -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

View file

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

View file

@ -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 {

View file

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

View file

@ -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 {

View file

@ -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 {

File diff suppressed because it is too large Load diff

22
example/README.md Normal file
View 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
View 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)
}
}
}
}

View 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)
}
}
}
}

View 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)
}
}

View 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)
}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

View 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
View 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
View file

@ -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
View 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=