diff --git a/main.go b/main.go new file mode 100644 index 0000000..00071bf --- /dev/null +++ b/main.go @@ -0,0 +1,222 @@ +package main + +import ( + "fmt" + "regexp" + "strconv" + "strings" + "sync" + "time" + "encoding/json" + + "github.com/Arman92/go-tdlib" + "github.com/streadway/amqp" +) + +type MsgDirection int + +const ( + Incoming MsgDirection = 1 + Outgoing MsgDirection = 2 +) + +type ChatWarsMessage struct { + UserID64 int64 `json:"user_id"` + Direction MsgDirection `json:"direction"` + MsgText string `json:"msg"` + MsgID64 int64 `json:"msg_id"` +} + +const user_chtwrsbot = 408101137 + +var ownUserID64 = int64(0) +var ownUserID32 = int32(0) + + +func main() { +// msgMutex = &sync.Mutex{} + tdlib.SetLogVerbosityLevel(1) + tdlib.SetFilePath("./errors.txt") + + // Create new instance of client + client := tdlib.NewClient(tdlib.Config{ + APIID: "187786", + APIHash: "e782045df67ba48e441ccb105da8fc85", + SystemLanguageCode: "en", + DeviceModel: "ChatWarsClient", + SystemVersion: "0.1", + ApplicationVersion: "0.1", + UseMessageDatabase: true, + UseFileDatabase: true, + UseChatInfoDatabase: true, + UseTestDataCenter: false, + DatabaseDirectory: "./tdlib-db", + FileDirectory: "./tdlib-files", + IgnoreFileNames: false, + }) + + for { + currentState, _ := client.Authorize() + if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPhoneNumberType { + fmt.Print("Enter phone: ") + var number string + fmt.Scanln(&number) + _, err := client.SendPhoneNumber(number) + if err != nil { + fmt.Printf("Error sending phone number: %v", err) + } + } else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitCodeType { + fmt.Print("Enter code: ") + var code string + fmt.Scanln(&code) + _, err := client.SendAuthCode(code) + if err != nil { + fmt.Printf("Error sending auth code : %v", err) + } + } else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPasswordType { + fmt.Print("Enter Password: ") + var password string + fmt.Scanln(&password) + _, err := client.SendAuthPassword(password) + if err != nil { + fmt.Printf("Error sending auth password: %v", err) + } + } else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateReadyType { + fmt.Println("Authorization Ready! Let's rock") + break + } + } + + ownUserID32 = OwnUserID(client) + ownUserID64 = int64(OwnUserID(client)) + + go ListenCW(client) + + fmt.Println("Started !") + + // Main loop + for { + time.Sleep(30 * time.Second) + } +} + +func ListenCW(c *tdlib.Client) { + var msgDisplay, msgDelete bool + eventFilter := func(msg *tdlib.TdMessage) bool { + updateMsg := (*msg).(*tdlib.UpdateNewMessage) + chatID := updateMsg.Message.ChatID + senderUserID := updateMsg.Message.SenderUserID + if (chatID == user_chtwrsbot) { + return true + } + return false + } + + conn, _ := amqp.Dial("amqp://shoopea:UmDd5g4WRa2MzqOHsG2T@localhost:5672/chatwars/" + ownUserID64) + defer conn.Close() + + ch, _ := conn.Channel() + defer ch.Close() + + q, _ := ch.QueueDeclare( + "msg", // name + false, // durable + false, // delete when unused + false, // exclusive + false, // no-wait + nil, // arguments + ) + + 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 + } + + m := ChatWarsMessage{ + UserID64 : ownUserID64, + MsgText : string(txt), + MsgID64 : updateMsg.Message.ID, + } + if senderUserID = ownUserID64 { + m.Direction = Outgoing + } else + n.Direction = Incoming + } + + username := "null" + if senderUserID != 0 { + user, err := c.GetUser(senderUserID) + if err != nil { + fmt.Println("Listen:", err.Error()) + continue + } else { + + } + } + + b, err := json.Marshal(m) + + _ = ch.Publish( + "", // exchange + q.Name, // routing key + false, // mandatory + false, // immediate + amqp.Publishing{ + ContentType: "application/json", + Body: []byte(b), + }) + + t := time.Now() + txt := updateMsg.Message.Content.(*tdlib.MessageText).Text.Text + 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("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; +} +