package main import ( //"encoding/json" "fmt" "log" "math" "sync" "time" "github.com/Arman92/go-tdlib" ) func ListenMe(c *tdlib.Client) { eventFilter := func(msg *tdlib.TdMessage) bool { updateMsg := (*msg).(*tdlib.UpdateNewMessage) senderUserID := updateMsg.Message.SenderUserID if senderUserID == ownUserID32 { return true } else { return false } } receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100) for _ = range receiver.Chan { lastOwnTDMsgMux.Lock() lastOwnTDMsg = time.Now() lastOwnTDMsgMux.Unlock() } } func ListenMQ(c *tdlib.Client, msgs <-chan TGCommand) { for m := range msgs { //b, _ := json.Marshal(m) //log.Printf("****************************** New MQ command ******************************\n%s\n****************************************************************************\n", string(b)) go clientMsg(c, m) } } func ListenTG(c *tdlib.Client) { eventFilter := func(msg *tdlib.TdMessage) bool { updateMsg := (*msg).(*tdlib.UpdateNewMessage) chatID := int64(updateMsg.Message.ChatID) userID := int64(updateMsg.Message.SenderUserID) forwardInfo := updateMsg.Message.ForwardInfo for _, v := range cfg.Listen { if (v.Chat == chatID || v.Chat == 0) && (v.User == userID || v.User == 0) && forwardInfo == nil { return true } } return false } receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100) for newMsg := range receiver.Chan { updateMsg := (newMsg).(*tdlib.UpdateNewMessage) if updateMsg.Message.Content.GetMessageContentEnum() == tdlib.MessageTextType { txt := updateMsg.Message.Content.(*tdlib.MessageText).Text.Text t := time.Now() m := ChatWarsMessage{ TGUserID64: ownUserID64, TGSenderUserID64: int64(updateMsg.Message.SenderUserID), ID64: updateMsg.Message.ID, ChatID64: updateMsg.Message.ChatID, Text: txt, Callbacks: nil, } m.Date = time.Unix(int64(updateMsg.Message.Date), 0) if updateMsg.Message.ReplyMarkup != nil { if updateMsg.Message.ReplyMarkup.GetReplyMarkupEnum() == tdlib.ReplyMarkupInlineKeyboardType { rm := updateMsg.Message.ReplyMarkup.(*tdlib.ReplyMarkupInlineKeyboard) for k1, v1 := range rm.Rows { for k2, v2 := range v1 { if v2.Type.GetInlineKeyboardButtonTypeEnum() == tdlib.InlineKeyboardButtonTypeCallbackType { cwcb := ChatWarsCallback{ Name: v2.Text, Data: v2.Type.(*tdlib.InlineKeyboardButtonTypeCallback).Data, } fmt.Printf("ReplyMarkupInlineKeyboard : %s => %s\n", cwcb.Name, string(cwcb.Data)) } } } } } lastChatMsgMux.RLock() if _, ok := lastChatTDMsg[m.ChatID64]; !ok { lastChatMsgMux.RUnlock() lastChatMsgMux.Lock() if _, ok := lastChatTDMsg[m.ChatID64]; !ok { lastChatTDMsg[m.ChatID64] = time.Now().Add(-1 * time.Second) lastChatTDMsgMux[m.ChatID64] = new(sync.Mutex) } lastChatMsgMux.Unlock() } 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() time.Sleep(time.Until(now.Add(200 * time.Millisecond))) } 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(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("================================================================================================================") } } } func getHistory(c *tdlib.Client, chatID64 *int64, senderUserID64 *int64) { var msgCount int32 = 0 var msgParsed int32 = 0 var loopOverflow int32 = 0 var lastParsedID64 int64 = int64(math.MaxInt64) var lastParsedTime time.Time = time.Now() var chat int64 var m ChatWarsMessage 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 { prevLastParsedID64 := lastParsedID64 msgs, err := c.GetChatHistory(chat, lastParsedID64, 0, 99, false) if err != nil { if err.Error() == "timeout" { 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) } 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{ TGUserID64: ownUserID64, TGSenderUserID64: int64(msg.SenderUserID), ID64: msg.ID, ChatID64: msg.ChatID, Text: msg.Content.(*tdlib.MessageText).Text.Text, } m.Date = time.Unix(int64(msg.Date), 0) } else { if msg.ForwardInfo.GetMessageForwardInfoEnum() == tdlib.MessageForwardedFromUserType { m = ChatWarsMessage{ TGUserID64: int64(msg.SenderUserID), TGSenderUserID64: int64(msg.ForwardInfo.(*tdlib.MessageForwardedFromUser).SenderUserID), ID64: msg.ID, ChatID64: 0, Text: msg.Content.(*tdlib.MessageText).Text.Text, } m.Date = time.Unix(int64(msg.ForwardInfo.(*tdlib.MessageForwardedFromUser).Date), 0) } else { m = ChatWarsMessage{ ID64: 0, } } } if m.ID64 != 0 && (*senderUserID64 == 0 || m.TGSenderUserID64 == *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 } if prevLastParsedID64 == lastParsedID64 { loopOverflow++ if loopOverflow == 5 { // we should be at the end ! lastParsedID64 = -1 } else { logOnError(err, "Overflow ...") fmt.Printf("Overflow : %d messages retrieved out of %d dating back %s (%d) ...\n", msgCount, msgParsed, lastParsedTime.Format(time.RFC3339), lastParsedID64) time.Sleep(5 * time.Second) } } else { loopOverflow = 0 } } log.Printf("Exported %d messages.\n", msgCount) fmt.Printf("Exported %d messages.\n", msgCount) } func OwnUserID(c *tdlib.Client) int32 { user, _ := c.GetMe() return user.ID } func clientMsg(c *tdlib.Client, m TGCommand) { if m.Delay != time.Duration(0) { log.Printf("clientMsg : Delaying message by %.3f seconds.\n", m.Delay.Seconds()) time.Sleep(m.Delay) } 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))) } switch m.Type { case commandForwardMsg: msgIDs := make([]int64, 1) msgIDs[0] = m.FromMsgID64 c.ForwardMessages(m.ToChatID64, m.FromChatID64, msgIDs, false, false, false) 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) } case commandDeleteMsg: msgIDs := make([]int64, 1) msgIDs[0] = m.FromMsgID64 c.DeleteMessages(m.FromChatID64, msgIDs, false) 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 r := ChatWarsMessage{ TGUserID64: ownUserID64, TGSenderUserID64: int64(u.SenderUserID), ID64: u.ID, ChatID64: u.ChatID, Text: txt, } r.Date = time.Unix(int64(u.Date), 0) MQCWMsgQueue <- r } default: log.Printf("ListenMQ : No handler for command %d.\n", m.Type) } }