From 0872e48380faf5680229123eb193528fa5e0849e Mon Sep 17 00:00:00 2001 From: shoopea Date: Fri, 3 May 2019 17:19:20 +0800 Subject: [PATCH] upd --- main.go | 127 ++--------------------------------- td.go | 199 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.go | 17 +++++ 3 files changed, 222 insertions(+), 121 deletions(-) create mode 100644 td.go create mode 100644 utils.go diff --git a/main.go b/main.go index 31f7a6f..f8af0fe 100644 --- a/main.go +++ b/main.go @@ -40,8 +40,8 @@ type Config struct { } var ( - config = flag.String("config", "gocw2.cfg", "config file path") - initdb = flag.Bool("initdb", false, "initialize tracker database") + config = flag.String("config", "gocw2.cfg", "config file path") + history = flag.Bool("history", false, "initialize chat history") cfg Config @@ -113,6 +113,10 @@ func main() { ownUserID32 = OwnUserID(client) ownUserID64 = int64(OwnUserID(client)) + if *history { + getHistory(client) + } + go ListenCW(client) fmt.Println("Started !") @@ -123,122 +127,3 @@ func main() { } } - -func ListenCW(c *tdlib.Client) { - eventFilter := func(msg *tdlib.TdMessage) bool { - updateMsg := (*msg).(*tdlib.UpdateNewMessage) - chatID := updateMsg.Message.ChatID - if chatID == user_chtwrsbot { - return true - } - return false - } - - conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue) - if err != nil { - log.Fatal(err) - } - defer conn.Close() - - ch, err := conn.Channel() - if err != nil { - log.Fatal(err) - } - defer ch.Close() - - q, err := ch.QueueDeclare( - "msg", // name - false, // durable - false, // delete when unused - false, // exclusive - false, // no-wait - nil, // arguments - ) - if err != nil { - log.Fatal(err) - } - - receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100) - for newMsg := range receiver.Chan { - updateMsg := (newMsg).(*tdlib.UpdateNewMessage) - senderUserID := updateMsg.Message.SenderUserID - mType := updateMsg.Message.Content.GetMessageContentEnum() - if mType == "messageText" { - 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, - MsgDate: updateMsg.Message.Date, - MsgText: string(txt), - MsgID64: updateMsg.Message.ID, - } - if senderUserID == ownUserID32 { - m.Direction = Outgoing - } else { - m.Direction = Incoming - } - - b, err := json.Marshal(m) - - _ = ch.Publish( - "", // exchange - q.Name, // routing key - false, // mandatory - false, // immediate - amqp.Publishing{ - ContentType: "application/json", - Body: []byte(b), - }) - - 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 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 -} diff --git a/td.go b/td.go new file mode 100644 index 0000000..9720152 --- /dev/null +++ b/td.go @@ -0,0 +1,199 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "log" + "time" + + "gopkg.in/gcfg.v1" + + "github.com/Arman92/go-tdlib" + "github.com/streadway/amqp" +) + +func ListenCW(c *tdlib.Client) { + eventFilter := func(msg *tdlib.TdMessage) bool { + updateMsg := (*msg).(*tdlib.UpdateNewMessage) + chatID := updateMsg.Message.ChatID + if chatID == user_chtwrsbot { + return true + } + return false + } + + conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue) + if err != nil { + log.Fatal(err) + } + defer conn.Close() + + ch, err := conn.Channel() + if err != nil { + log.Fatal(err) + } + defer ch.Close() + + q, err := ch.QueueDeclare( + "msg", // name + false, // durable + false, // delete when unused + false, // exclusive + false, // no-wait + nil, // arguments + ) + if err != nil { + log.Fatal(err) + } + + receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100) + for newMsg := range receiver.Chan { + updateMsg := (newMsg).(*tdlib.UpdateNewMessage) + senderUserID := updateMsg.Message.SenderUserID + mType := updateMsg.Message.Content.GetMessageContentEnum() + if mType == "messageText" { + 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, + MsgDate: updateMsg.Message.Date, + MsgText: string(txt), + MsgID64: updateMsg.Message.ID, + } + if senderUserID == ownUserID32 { + m.Direction = Outgoing + } else { + m.Direction = Incoming + } + + b, err := json.Marshal(m) + + _ = ch.Publish( + "", // exchange + q.Name, // routing key + false, // mandatory + false, // immediate + amqp.Publishing{ + ContentType: "application/json", + Body: []byte(b), + }) + + 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) { + var lastMsgID64 int64 = int64(math.MaxInt64) + + conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue) + if err != nil { + log.Fatal(err) + } + defer conn.Close() + + ch, err := conn.Channel() + if err != nil { + log.Fatal(err) + } + defer ch.Close() + + q, err := ch.QueueDeclare( + "msg", // name + false, // durable + false, // delete when unused + false, // exclusive + false, // no-wait + nil, // arguments + ) + if err != nil { + log.Fatal(err) + } + + for lastMsgID64 >= 0 { + msgs, err := c.GetChatHistory(user_chtwrsbot, lastMsgID64, 0, 100, false) + if err != nil { + logOnError(err, "Cannot get history") + lastMsgID64 = -1 + } else { + for msg := range msgs.Messages { + + m := ChatWarsMessage{ + UserID64: int64(msg.SenderUserID), + MsgDate: msg.Date, + MsgText: msg.Content.(*tdlib.MessageText).Text.Text, + MsgID64: msg.ID, + } + if senderUserID == ownUserID32 { + m.Direction = Outgoing + } else { + m.Direction = Incoming + } + + b, err := json.Marshal(m) + + _ = ch.Publish( + "", // exchange + q.Name, // routing key + false, // mandatory + false, // immediate + amqp.Publishing{ + ContentType: "application/json", + Body: []byte(b), + }) + if msg.ID < lastMsgID64 { + lastMsgID64 = msg.ID + } + } + } + } +} + +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 +} diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..59e408e --- /dev/null +++ b/utils.go @@ -0,0 +1,17 @@ +package main + +import ( + "log" +) + +func failOnError(err error, msg string) { + if err != nil { + log.Fatalf("%s: %s", msg, err) + } +} + +func logOnError(err error, msg string) { + if err != nil { + log.Printf("%s: %s", msg, err) + } +}