forked from Arman92/go-tdlib
/
getChats.go
110 lines (95 loc) · 2.63 KB
/
getChats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"math"
"os"
"os/signal"
"syscall"
"time"
"github.com/ST380011A/go-tdlib"
)
var allChats []*tdlib.Chat
var haveFullChatList bool
func main() {
tdlib.SetLogVerbosityLevel(1)
tdlib.SetFilePath("./errors.txt")
// Create new instance of client
client := tdlib.NewClient(tdlib.Config{
APIID: "187786",
APIHash: "e782045df67ba48e441ccb105da8fc85",
SystemLanguageCode: "en",
DeviceModel: "Server",
SystemVersion: "1.0.0",
ApplicationVersion: "1.0.0",
UseMessageDatabase: true,
UseFileDatabase: true,
UseChatInfoDatabase: true,
UseTestDataCenter: false,
DatabaseDirectory: "./tdlib-db",
FileDirectory: "./tdlib-files",
IgnoreFileNames: false,
})
// Handle Ctrl+C , Gracefully exit and shutdown tdlib
ch := make(chan os.Signal, 2)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
go func() {
<-ch
client.DestroyInstance()
os.Exit(1)
}()
// Wait while we get AuthorizationReady!
// Note: See authorization example for complete auhtorization sequence example
currentState, _ := client.Authorize()
for ; currentState.GetAuthorizationStateEnum() != tdlib.AuthorizationStateReadyType; currentState, _ = client.Authorize() {
time.Sleep(300 * time.Millisecond)
}
// get at most 1000 chats list
getChatList(client, 1000)
fmt.Printf("got %d chats\n", len(allChats))
for _, chat := range allChats {
fmt.Printf("Chat title: %s \n", chat.Title)
}
for {
time.Sleep(1 * time.Second)
}
}
// see https://stackoverflow.com/questions/37782348/how-to-use-getchats-in-tdlib
func getChatList(client *tdlib.Client, limit int) error {
if !haveFullChatList && limit > len(allChats) {
offsetOrder := int64(math.MaxInt64)
offsetChatID := int64(0)
var chatList = tdlib.NewChatListMain()
var lastChat *tdlib.Chat
if len(allChats) > 0 {
lastChat = allChats[len(allChats)-1]
for i := 0; i < len(lastChat.Positions); i++ {
//Find the main chat list
if lastChat.Positions[i].List.GetChatListEnum() == tdlib.ChatListMainType {
offsetOrder = int64(lastChat.Positions[i].Order)
}
}
offsetChatID = lastChat.ID
}
// get chats (ids) from tdlib
chats, err := client.GetChats(chatList, tdlib.JSONInt64(offsetOrder),
offsetChatID, int32(limit-len(allChats)))
if err != nil {
return err
}
if len(chats.ChatIDs) == 0 {
haveFullChatList = true
return nil
}
for _, chatID := range chats.ChatIDs {
// get chat info from tdlib
chat, err := client.GetChat(chatID)
if err == nil {
allChats = append(allChats, chat)
} else {
return err
}
}
return getChatList(client, limit)
}
return nil
}