2018-10-09 03:46:15 +00:00
|
|
|
package puller
|
|
|
|
|
|
|
|
import (
|
2018-10-23 12:49:10 +00:00
|
|
|
"github.com/zelenin/go-tdlib/client"
|
2018-10-09 03:46:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ChatHistory(tdlibClient *client.Client, chatId int64) (chan *client.Message, chan error) {
|
2018-10-23 12:49:10 +00:00
|
|
|
messageChan := make(chan *client.Message, 10)
|
|
|
|
errChan := make(chan error, 1)
|
2018-10-09 03:46:15 +00:00
|
|
|
|
2018-10-23 12:49:10 +00:00
|
|
|
var fromMessageId int64 = 0
|
|
|
|
var offset int32 = 0
|
|
|
|
var limit int32 = 100
|
2018-10-09 03:46:15 +00:00
|
|
|
|
2018-10-23 12:49:10 +00:00
|
|
|
go chatHistory(tdlibClient, messageChan, errChan, chatId, fromMessageId, offset, limit, false)
|
2018-10-09 03:46:15 +00:00
|
|
|
|
2018-10-23 12:49:10 +00:00
|
|
|
return messageChan, errChan
|
2018-10-09 03:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func chatHistory(tdlibClient *client.Client, messageChan chan *client.Message, errChan chan error, chatId int64, fromMessageId int64, offset int32, limit int32, onlyLocal bool) {
|
2018-10-23 12:49:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2018-10-09 03:46:15 +00:00
|
|
|
}
|