gocw2/td.go

310 lines
9.5 KiB
Go
Raw Normal View History

2019-05-03 11:19:20 +02:00
package main
import (
2020-02-01 13:30:12 +01:00
"encoding/json"
2019-05-03 11:19:20 +02:00
"fmt"
"log"
2019-05-03 11:22:17 +02:00
"math"
2019-11-05 03:37:19 +01:00
"sync"
2019-05-03 11:19:20 +02:00
"time"
"github.com/Arman92/go-tdlib"
)
2019-05-15 04:28:06 +02:00
func ListenMe(c *tdlib.Client) {
2019-05-03 11:19:20 +02:00
eventFilter := func(msg *tdlib.TdMessage) bool {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
2019-05-15 04:28:06 +02:00
senderUserID := updateMsg.Message.SenderUserID
if senderUserID == ownUserID32 {
2019-05-03 11:19:20 +02:00
return true
2019-05-15 04:28:06 +02:00
} else {
return false
2019-05-03 11:19:20 +02:00
}
}
2019-05-15 04:18:16 +02:00
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
2019-05-15 04:38:47 +02:00
for _ = range receiver.Chan {
2019-12-12 03:45:19 +01:00
lastOwnTDMsgMux.Lock()
2019-05-15 04:18:16 +02:00
lastOwnTDMsg = time.Now()
2019-12-12 03:45:19 +01:00
lastOwnTDMsgMux.Unlock()
2019-05-15 04:18:16 +02:00
}
}
2019-05-16 04:05:27 +02:00
func ListenMQ(c *tdlib.Client, msgs <-chan TGCommand) {
2019-05-15 04:18:16 +02:00
for m := range msgs {
2019-12-12 03:46:42 +01:00
//b, _ := json.Marshal(m)
2019-12-12 03:45:19 +01:00
//log.Printf("****************************** New MQ command ******************************\n%s\n****************************************************************************\n", string(b))
2019-08-23 07:50:58 +02:00
go clientMsg(c, m)
2019-05-15 04:18:16 +02:00
}
}
2019-10-12 07:26:11 +02:00
func ListenTG(c *tdlib.Client) {
2019-05-15 04:18:16 +02:00
eventFilter := func(msg *tdlib.TdMessage) bool {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
2019-10-12 07:29:15 +02:00
chatID := int64(updateMsg.Message.ChatID)
userID := int64(updateMsg.Message.SenderUserID)
2019-08-23 07:53:54 +02:00
forwardInfo := updateMsg.Message.ForwardInfo
2019-10-12 07:30:36 +02:00
for _, v := range cfg.Listen {
2019-10-12 07:26:11 +02:00
if (v.Chat == chatID || v.Chat == 0) &&
(v.User == userID || v.User == 0) &&
forwardInfo == nil {
return true
}
2019-05-15 04:18:16 +02:00
}
2019-10-12 07:26:11 +02:00
return false
2019-05-15 04:18:16 +02:00
}
2019-05-03 11:19:20 +02:00
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for newMsg := range receiver.Chan {
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
2019-05-04 05:53:35 +02:00
if updateMsg.Message.Content.GetMessageContentEnum() == tdlib.MessageTextType {
2019-05-03 11:19:20 +02:00
txt := updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
t := time.Now()
m := ChatWarsMessage{
2019-07-31 08:46:01 +02:00
TGUserID64: ownUserID64,
2019-07-31 09:32:49 +02:00
TGSenderUserID64: int64(updateMsg.Message.SenderUserID),
2019-07-31 08:46:01 +02:00
ID64: updateMsg.Message.ID,
ChatID64: updateMsg.Message.ChatID,
Text: txt,
2019-12-12 04:17:46 +01:00
Callbacks: nil,
2019-05-03 11:19:20 +02:00
}
m.Date = time.Unix(int64(updateMsg.Message.Date), 0)
2019-12-12 04:17:46 +01:00
if updateMsg.Message.ReplyMarkup != nil {
if updateMsg.Message.ReplyMarkup.GetReplyMarkupEnum() == tdlib.ReplyMarkupInlineKeyboardType {
2019-12-12 04:18:11 +01:00
rm := updateMsg.Message.ReplyMarkup.(*tdlib.ReplyMarkupInlineKeyboard)
2019-12-12 04:35:49 +01:00
for _, v1 := range rm.Rows {
for _, v2 := range v1 {
2019-12-12 04:34:02 +01:00
if v2.Type.GetInlineKeyboardButtonTypeEnum() == tdlib.InlineKeyboardButtonTypeCallbackType {
cwcb := ChatWarsCallback{
Name: v2.Text,
2019-12-12 04:35:30 +01:00
Data: v2.Type.(*tdlib.InlineKeyboardButtonTypeCallback).Data,
2019-12-12 04:34:02 +01:00
}
2019-12-12 04:40:25 +01:00
m.Callbacks = append(m.Callbacks, cwcb)
// fmt.Printf("ReplyMarkupInlineKeyboard : %s => %s\n", cwcb.Name, string(cwcb.Data))
2019-12-12 04:34:02 +01:00
}
2019-12-12 04:20:22 +01:00
}
2019-12-12 04:17:46 +01:00
}
}
}
lastChatMsgMux.RLock()
if _, ok := lastChatTDMsg[m.ChatID64]; !ok {
lastChatMsgMux.RUnlock()
lastChatMsgMux.Lock()
if _, ok := lastChatTDMsg[m.ChatID64]; !ok {
2019-11-05 03:43:06 +01:00
lastChatTDMsg[m.ChatID64] = time.Now().Add(-1 * time.Second)
lastChatTDMsgMux[m.ChatID64] = new(sync.Mutex)
}
lastChatMsgMux.Unlock()
2019-11-05 03:55:12 +01:00
} else {
lastChatMsgMux.RUnlock()
}
for {
lastChatTDMsgMux[m.ChatID64].Lock()
now := time.Now()
if lastChatTDMsg[m.ChatID64].Add(time.Second).Before(now) {
lastChatTDMsg[m.ChatID64] = now
lastChatTDMsgMux[m.ChatID64].Unlock()
break
}
lastChatTDMsgMux[m.ChatID64].Unlock()
2019-11-05 03:49:46 +01:00
time.Sleep(time.Until(now.Add(200 * time.Millisecond)))
}
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(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("ForwardInfo : ", updateMsg.Message.ForwardInfo)
fmt.Println("================================================================================================================")
2019-12-12 03:45:19 +01:00
}
}
}
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-11 11:11:53 +02:00
var loopOverflow 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-10-12 07:29:15 +02:00
chat = *chatID64
2019-05-04 11:55:28 +02:00
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)
}
2019-05-04 12:10:15 +02:00
2019-05-11 10:52:31 +02:00
for lastParsedID64 >= 0 {
2019-05-11 11:06:00 +02:00
prevLastParsedID64 := lastParsedID64
2019-05-11 11:17:36 +02:00
msgs, err := c.GetChatHistory(chat, lastParsedID64, 0, 99, false)
2019-05-03 11:19:20 +02:00
if err != nil {
2019-05-03 12:56:13 +02:00
if err.Error() == "timeout" {
2019-05-11 11:13:29 +02:00
logOnError(err, "Waiting....")
fmt.Printf("Timeout : %d messages retrieved out of %d dating back %s (%d) ...\n", msgCount, msgParsed, lastParsedTime.Format(time.RFC3339), lastParsedID64)
time.Sleep(5 * time.Second)
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-07-31 08:46:01 +02:00
TGUserID64: ownUserID64,
TGSenderUserID64: int64(msg.SenderUserID),
ID64: msg.ID,
ChatID64: msg.ChatID,
Text: msg.Content.(*tdlib.MessageText).Text.Text,
2019-05-04 10:57:14 +02:00
}
m.Date = time.Unix(int64(msg.Date), 0)
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{
2019-07-31 08:46:01 +02:00
TGUserID64: int64(msg.SenderUserID),
TGSenderUserID64: int64(msg.ForwardInfo.(*tdlib.MessageForwardedFromUser).SenderUserID),
ID64: msg.ID,
ChatID64: 0,
Text: msg.Content.(*tdlib.MessageText).Text.Text,
2019-05-04 12:21:53 +02:00
}
m.Date = time.Unix(int64(msg.ForwardInfo.(*tdlib.MessageForwardedFromUser).Date), 0)
2019-05-04 12:21:53 +02:00
} 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
2020-02-01 13:32:11 +01:00
if msg.ID == 22874685440 {
2020-02-01 13:30:12 +01:00
x, err := json.Marshal(msg)
logOnError(err, "Marshal")
2020-02-01 13:30:29 +01:00
fmt.Printf("DUMP:\n%s\n", x)
2020-02-01 13:27:07 +01:00
return
}
2019-07-31 09:32:49 +02:00
if m.ID64 != 0 && (*senderUserID64 == 0 || m.TGSenderUserID64 == *senderUserID64) {
2019-05-04 10:57:14 +02:00
MQCWMsgQueue <- m
msgCount = msgCount + 1
2019-05-03 12:17:05 +02:00
}
default:
log.Printf("getHistory(%d) : no handler for %s\n", msg.ID, 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 = m.Date
2019-05-04 12:52:20 +02:00
}
2019-05-04 12:43:04 +02:00
if msgParsed%1000 == 0 {
2019-05-11 11:13:29 +02:00
fmt.Printf("Waiting : %d messages retrieved out of %d dating back %s (%d) ...\n", msgCount, msgParsed, lastParsedTime.Format(time.RFC3339), lastParsedID64)
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-11 11:06:00 +02:00
if prevLastParsedID64 == lastParsedID64 {
2019-05-11 11:11:53 +02:00
loopOverflow++
if loopOverflow == 5 {
// we should be at the end !
lastParsedID64 = -1
} else {
2019-05-11 11:13:29 +02:00
logOnError(err, "Overflow ...")
fmt.Printf("Overflow : %d messages retrieved out of %d dating back %s (%d) ...\n", msgCount, msgParsed, lastParsedTime.Format(time.RFC3339), lastParsedID64)
2019-05-11 11:11:53 +02:00
time.Sleep(5 * time.Second)
}
2019-05-11 11:14:29 +02:00
} else {
loopOverflow = 0
2019-05-11 11:06:00 +02:00
}
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 OwnUserID(c *tdlib.Client) int32 {
user, _ := c.GetMe()
return user.ID
}
2019-08-23 07:50:58 +02:00
func clientMsg(c *tdlib.Client, m TGCommand) {
2019-08-24 08:00:45 +02:00
if m.Delay != time.Duration(0) {
2019-08-24 08:10:21 +02:00
log.Printf("clientMsg : Delaying message by %.3f seconds.\n", m.Delay.Seconds())
2019-08-24 08:07:35 +02:00
time.Sleep(m.Delay)
2019-08-23 07:53:54 +02:00
}
2019-08-24 07:58:58 +02:00
for {
lastOwnTDMsgMux.Lock()
now := time.Now()
if lastOwnTDMsg.Add(time.Second).Before(now) {
lastOwnTDMsg = now
lastOwnTDMsgMux.Unlock()
break
}
lastOwnTDMsgMux.Unlock()
time.Sleep(time.Until(now.Add(time.Second)))
}
2019-08-23 07:53:54 +02:00
switch m.Type {
2019-08-27 05:32:41 +02:00
case commandForwardMsg:
msgIDs := make([]int64, 1)
2019-08-27 05:34:14 +02:00
msgIDs[0] = m.FromMsgID64
2019-08-27 05:32:41 +02:00
c.ForwardMessages(m.ToChatID64, m.FromChatID64, msgIDs, false, false, false)
2019-08-23 07:53:54 +02:00
case commandSendMsg:
msgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText(m.Text, nil), true, true)
if m.ToChatID64 != 0 {
c.SendMessage(m.ToChatID64, 0, false, false, nil, msgTxt)
} else if m.ToUserID64 != 0 {
c.SendMessage(m.ToUserID64, 0, false, false, nil, msgTxt)
}
2019-08-27 05:39:20 +02:00
case commandDeleteMsg:
msgIDs := make([]int64, 1)
msgIDs[0] = m.FromMsgID64
c.DeleteMessages(m.FromChatID64, msgIDs, false)
2019-08-23 07:53:54 +02:00
case commandRefreshMsg:
u, err := c.GetMessage(m.FromChatID64, m.FromMsgID64)
logOnError(err, "ListenMQ : commandRefreshMsg")
if err == nil && u.Content.GetMessageContentEnum() == tdlib.MessageTextType {
txt := u.Content.(*tdlib.MessageText).Text.Text
2019-08-23 07:50:58 +02:00
2019-08-23 07:53:54 +02:00
r := ChatWarsMessage{
TGUserID64: ownUserID64,
TGSenderUserID64: int64(u.SenderUserID),
ID64: u.ID,
ChatID64: u.ChatID,
Text: txt,
}
2019-08-23 07:50:58 +02:00
2019-08-23 07:53:54 +02:00
r.Date = time.Unix(int64(u.Date), 0)
2019-08-23 07:50:58 +02:00
2019-08-23 07:53:54 +02:00
MQCWMsgQueue <- r
2019-08-23 07:50:58 +02:00
}
2019-08-23 07:53:54 +02:00
default:
log.Printf("ListenMQ : No handler for command %d.\n", m.Type)
}
2019-12-13 08:32:04 +01:00
2019-08-23 07:50:58 +02:00
}