192 lines
6.3 KiB
Go
192 lines
6.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"math"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/Arman92/go-tdlib"
|
|
)
|
|
|
|
func ListenCW(c *tdlib.Client) {
|
|
eventFilter := func(msg *tdlib.TdMessage) bool {
|
|
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
|
|
chatID := updateMsg.Message.ChatID
|
|
forwardInfo := updateMsg.Message.ForwardInfo
|
|
if chatID == user_chtwrsbot && forwardInfo == nil {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
|
|
for newMsg := range receiver.Chan {
|
|
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
|
|
senderUserID := updateMsg.Message.SenderUserID
|
|
if updateMsg.Message.Content.GetMessageContentEnum() == tdlib.MessageTextType {
|
|
user, err := c.GetUser(senderUserID)
|
|
if err != nil {
|
|
fmt.Println("ListenCW:", err.Error())
|
|
continue
|
|
}
|
|
|
|
txt := updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
|
|
t := time.Now()
|
|
|
|
m := ChatWarsMessage{
|
|
UserID64: ownUserID64,
|
|
SenderUserID64: int64(updateMsg.Message.SenderUserID),
|
|
ID64: updateMsg.Message.ID,
|
|
ChatID64: updateMsg.Message.ChatID,
|
|
Text: txt,
|
|
}
|
|
|
|
i, err := strconv.ParseInt(int64(updateMsg.Message.Date), 10, 64)
|
|
m.Date = time.Unix(i, 0)
|
|
|
|
MQCWMsgQueue <- m
|
|
|
|
fmt.Printf("[%d-%02d-%02d %02d:%02d:%02d-00:00]", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
|
|
fmt.Println(" === CHATWARS (", user.Username, ") =====================================================================")
|
|
fmt.Println(txt, "\n")
|
|
fmt.Println("************ DETAILS ************")
|
|
fmt.Println("ID : ", updateMsg.Message.ID)
|
|
fmt.Println("Date : ", updateMsg.Message.Date)
|
|
fmt.Println("SenderUserID : ", updateMsg.Message.SenderUserID)
|
|
fmt.Println("ChatID : ", updateMsg.Message.ChatID)
|
|
fmt.Println("SendingState : ", updateMsg.Message.SendingState)
|
|
fmt.Println("IsOutgoing : ", updateMsg.Message.IsOutgoing)
|
|
fmt.Println("CanBeEdited : ", updateMsg.Message.CanBeEdited)
|
|
fmt.Println("CanBeForwarded : ", updateMsg.Message.CanBeForwarded)
|
|
fmt.Println("IsChannelPost : ", updateMsg.Message.IsChannelPost)
|
|
fmt.Println("ContainsUnreadMention : ", updateMsg.Message.ContainsUnreadMention)
|
|
fmt.Println("ForwardInfo : ", updateMsg.Message.ForwardInfo)
|
|
fmt.Println("ReplyToMessageID : ", updateMsg.Message.ReplyToMessageID)
|
|
fmt.Println("ViaBotUserID : ", updateMsg.Message.ViaBotUserID)
|
|
fmt.Println("================================================================================================================")
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
func getHistory(c *tdlib.Client, chatID64 *int64, senderUserID64 *int64) {
|
|
var msgCount int32 = 0
|
|
var msgParsed int32 = 0
|
|
var lastParsedID64 int64 = int64(math.MaxInt64)
|
|
var lastParsedTime time.Time = time.Now()
|
|
var chat int64
|
|
var m ChatWarsMessage
|
|
|
|
if *chatID64 == 0 {
|
|
chat = user_chtwrsbot
|
|
} else {
|
|
chat = *chatID64
|
|
}
|
|
|
|
if *senderUserID64 != 0 {
|
|
userDetails, err := c.GetUser(int32(*senderUserID64))
|
|
failOnError(err, "getHistory : GetUser")
|
|
chatDetails, err := c.GetChat(chat)
|
|
failOnError(err, "getHistory : GetChat")
|
|
fmt.Printf("Exporting historic messages for chat %d (%s) from user %d (%s)...\n", chat, chatDetails.Title, *senderUserID64, userDetails.Username)
|
|
} else {
|
|
chatDetails, err := c.GetChat(chat)
|
|
failOnError(err, "getHistory : GetChat")
|
|
fmt.Printf("Exporting historic messages for chat %d (%s) ...\n", chat, chatDetails.Title)
|
|
}
|
|
|
|
for lastParsedID64 >= 0 {
|
|
msgs, err := c.GetChatHistory(chat, lastParsedID64, 0, 33, false)
|
|
if err != nil {
|
|
if err.Error() == "timeout" {
|
|
logOnError(err, "Waiting...")
|
|
fmt.Printf("Waiting : %d messages retrieved out of %d dating back %s (%d) ...\n", msgCount, msgParsed, lastParsedTime.Format(time.RFC3339), lastParsedID64)
|
|
time.Sleep(5 * time.Second)
|
|
} else {
|
|
logOnError(err, "Cannot get history")
|
|
lastParsedID64 = -1
|
|
}
|
|
} else if msgs.TotalCount > 0 {
|
|
for _, msg := range msgs.Messages {
|
|
msgParsed = msgParsed + 1
|
|
lastParsedTime = time.Unix(int64(msg.Date), 0)
|
|
switch msg.Content.GetMessageContentEnum() {
|
|
case tdlib.MessageTextType:
|
|
if msg.ForwardInfo == nil {
|
|
m = ChatWarsMessage{
|
|
UserID64: ownUserID64,
|
|
SenderUserID64: int64(msg.SenderUserID),
|
|
ID64: msg.ID,
|
|
ChatID64: msg.ChatID,
|
|
Text: msg.Content.(*tdlib.MessageText).Text.Text,
|
|
}
|
|
i, err := strconv.ParseInt(int64(msg.Date), 10, 64)
|
|
m.Date = time.Unix(i, 0)
|
|
} else {
|
|
if msg.ForwardInfo.GetMessageForwardInfoEnum() == tdlib.MessageForwardedFromUserType {
|
|
m = ChatWarsMessage{
|
|
UserID64: int64(msg.SenderUserID),
|
|
SenderUserID64: int64(msg.ForwardInfo.(*tdlib.MessageForwardedFromUser).SenderUserID),
|
|
ID64: msg.ID,
|
|
ChatID64: 0,
|
|
Text: msg.Content.(*tdlib.MessageText).Text.Text,
|
|
}
|
|
i, err := strconv.ParseInt(int64(msg.ForwardInfo.(*tdlib.MessageForwardedFromUser).Date), 10, 64)
|
|
m.Date = time.Unix(i, 0)
|
|
|
|
} else {
|
|
m = ChatWarsMessage{
|
|
ID64: 0,
|
|
}
|
|
}
|
|
}
|
|
|
|
if m.ID64 != 0 && (*senderUserID64 == 0 || m.SenderUserID64 == *senderUserID64) {
|
|
MQCWMsgQueue <- m
|
|
msgCount = msgCount + 1
|
|
}
|
|
default:
|
|
log.Printf("getHistory(%d) : no handler for %s\n", msg.ID, msg.Content.GetMessageContentEnum())
|
|
}
|
|
if m.ID64 < lastParsedID64 {
|
|
lastParsedID64 = msg.ID
|
|
lastParsedTime = m.Date
|
|
}
|
|
if msgParsed%1000 == 0 {
|
|
fmt.Printf("Waiting : %d messages retrieved out of %d dating back %s (%d) ...\n", msgCount, msgParsed, lastParsedTime.Format(time.RFC3339), lastParsedID64)
|
|
}
|
|
|
|
}
|
|
} else {
|
|
lastParsedID64 = -1
|
|
}
|
|
}
|
|
log.Printf("Exported %d messages.\n", msgCount)
|
|
fmt.Printf("Exported %d messages.\n", msgCount)
|
|
}
|
|
|
|
func ForwardMsg(c *tdlib.Client, msgID int64, fromChatID int64, toChatID int64) int64 {
|
|
msgIDs := make([]int64, 1)
|
|
msgIDs[0] = msgID
|
|
msgs, _ := c.ForwardMessages(toChatID, fromChatID, msgIDs, false, false, false)
|
|
if msgs != nil {
|
|
return msgs.Messages[0].ID
|
|
} else {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
func DeleteMsg(c *tdlib.Client, chatID int64, msgID int64) {
|
|
msgIDs := make([]int64, 1)
|
|
msgIDs[0] = msgID
|
|
c.DeleteMessages(chatID, msgIDs, false)
|
|
fmt.Println("Deleting message ", msgID)
|
|
}
|
|
|
|
func OwnUserID(c *tdlib.Client) int32 {
|
|
user, _ := c.GetMe()
|
|
return user.ID
|
|
}
|