gocw2/td.go

172 lines
5.5 KiB
Go
Raw Normal View History

2019-05-03 11:19:20 +02:00
package main
import (
"fmt"
"log"
2019-05-03 11:22:17 +02:00
"math"
2019-05-03 11:19:20 +02:00
"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
2019-05-04 10:57:14 +02:00
forwardInfo := updateMsg.Message.ForwardInfo
if chatID == user_chtwrsbot && forwardInfo == nil {
2019-05-03 11:19:20 +02:00
return true
}
return false
}
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for newMsg := range receiver.Chan {
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
senderUserID := updateMsg.Message.SenderUserID
2019-05-04 05:53:35 +02:00
if updateMsg.Message.Content.GetMessageContentEnum() == tdlib.MessageTextType {
2019-05-03 11:19:20 +02:00
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{
2019-05-04 11:16:38 +02:00
SenderUserID64: int64(updateMsg.Message.SenderUserID),
2019-05-04 10:57:14 +02:00
Date: updateMsg.Message.Date,
ID64: updateMsg.Message.ID,
ChatID64: updateMsg.Message.ChatID,
Text: txt,
2019-05-03 11:19:20 +02:00
}
2019-05-04 05:53:35 +02:00
MQCWMsgQueue <- m
2019-05-03 11:19:20 +02:00
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("================================================================================================================")
}
}
}
2019-05-04 11:58:35 +02:00
func getHistory(c *tdlib.Client, chatID64 *int64, senderUserID64 *int64) {
2019-05-03 12:31:57 +02:00
var msgCount int32 = 0
2019-05-04 12:42:23 +02:00
var msgParsed int32 = 0
2019-05-04 12:52:20 +02:00
var lastParsedID64 int64 = int64(math.MaxInt64)
2019-05-04 12:42:23 +02:00
var lastParsedTime time.Time = time.Now()
2019-05-04 12:02:46 +02:00
var chat int64
2019-05-04 12:03:25 +02:00
var m ChatWarsMessage
2019-05-03 11:19:20 +02:00
2019-05-04 12:00:54 +02:00
if *chatID64 == 0 {
2019-05-04 12:02:46 +02:00
chat = user_chtwrsbot
2019-05-04 11:55:28 +02:00
} else {
2019-05-04 12:02:46 +02:00
chat = *chatID64
2019-05-04 11:55:28 +02:00
}
2019-05-04 12:10:15 +02:00
fmt.Printf("Exporting historic messages for chat %d from user %d ...\n", chat, *senderUserID64)
2019-05-04 12:52:20 +02:00
for lastParsedID64 >= 0 {
2019-05-04 16:31:47 +02:00
msgs, err := c.GetChatHistory(chat, lastParsedID64, 0, 50, false)
2019-05-03 11:19:20 +02:00
if err != nil {
2019-05-03 12:56:13 +02:00
if err.Error() == "timeout" {
logOnError(err, "Waiting...")
2019-05-04 12:53:34 +02:00
fmt.Printf("Waiting : %d messages retrieved out of %d dating back %s ...\n", msgCount, msgParsed, lastParsedTime.Format(time.RFC3339))
2019-05-03 12:56:13 +02:00
} else {
logOnError(err, "Cannot get history")
2019-05-04 12:53:34 +02:00
lastParsedID64 = -1
2019-05-03 12:56:13 +02:00
}
2019-05-03 13:01:50 +02:00
} else if msgs.TotalCount > 0 {
2019-05-03 11:22:17 +02:00
for _, msg := range msgs.Messages {
2019-05-04 12:42:23 +02:00
msgParsed = msgParsed + 1
lastParsedTime = time.Unix(int64(msg.Date), 0)
2019-05-03 12:29:07 +02:00
switch msg.Content.GetMessageContentEnum() {
2019-05-03 12:17:05 +02:00
case tdlib.MessageTextType:
2019-05-04 10:57:14 +02:00
if msg.ForwardInfo == nil {
2019-05-04 12:03:25 +02:00
m = ChatWarsMessage{
2019-05-04 10:57:14 +02:00
SenderUserID64: int64(msg.SenderUserID),
Date: msg.Date,
ID64: msg.ID,
ChatID64: msg.ChatID,
Text: msg.Content.(*tdlib.MessageText).Text.Text,
}
2019-05-04 11:55:28 +02:00
} else {
2019-05-04 12:21:53 +02:00
if msg.ForwardInfo.GetMessageForwardInfoEnum() == tdlib.MessageForwardedFromUserType {
m = ChatWarsMessage{
SenderUserID64: int64(msg.ForwardInfo.(*tdlib.MessageForwardedFromUser).SenderUserID),
Date: msg.ForwardInfo.(*tdlib.MessageForwardedFromUser).Date,
2019-05-04 12:37:43 +02:00
ID64: msg.ID,
ChatID64: 0,
2019-05-04 12:21:53 +02:00
Text: msg.Content.(*tdlib.MessageText).Text.Text,
}
} else {
m = ChatWarsMessage{
ID64: 0,
}
2019-05-04 12:04:27 +02:00
}
2019-05-04 11:55:28 +02:00
}
2019-05-04 10:57:14 +02:00
2019-05-04 12:10:15 +02:00
if m.ID64 != 0 && (*senderUserID64 == 0 || m.SenderUserID64 == *senderUserID64) {
2019-05-04 10:57:14 +02:00
MQCWMsgQueue <- m
msgCount = msgCount + 1
2019-05-03 12:17:05 +02:00
}
default:
2019-05-03 12:39:13 +02:00
log.Printf("getHistory : no handler for %s\n", msg.Content.GetMessageContentEnum())
2019-05-03 11:19:20 +02:00
}
2019-05-04 12:52:20 +02:00
if m.ID64 < lastParsedID64 {
lastParsedID64 = msg.ID
lastParsedTime = time.Unix(int64(m.Date), 0)
}
2019-05-04 12:43:04 +02:00
if msgParsed%1000 == 0 {
2019-05-04 12:55:19 +02:00
fmt.Printf("Waiting : %d messages retrieved out of %d dating back %s ...\n", msgCount, msgParsed, lastParsedTime.Format(time.RFC3339))
2019-05-04 12:43:04 +02:00
}
2019-05-03 11:19:20 +02:00
}
2019-05-03 13:01:50 +02:00
} else {
2019-05-04 12:52:20 +02:00
lastParsedID64 = -1
2019-05-03 11:19:20 +02:00
}
}
2019-05-03 12:31:57 +02:00
log.Printf("Exported %d messages.\n", msgCount)
2019-05-03 12:39:40 +02:00
fmt.Printf("Exported %d messages.\n", msgCount)
2019-05-03 11:19:20 +02:00
}
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
}