package main import ( "flag" "fmt" "log" "runtime" "sync" "time" "gopkg.in/gcfg.v1" "github.com/Arman92/go-tdlib" ) type Config struct { Rabbit struct { User string Password string Host string SendQueue string ReceiveQueue string } Listen map[string]*struct { User int64 Chat int64 } } var ( config = flag.String("config", "gocw2.cfg", "config file path") history = flag.Bool("history", false, "initialize chat history") historyChatID64 = flag.Int64("chat_id", 0, "chat to historize") historySenderUserID64 = flag.Int64("sender_user_id", 0, "sender_user_id to historize") cfg Config ownUserID64 = int64(0) ownUserID32 = int32(0) lastOwnTDMsg time.Time lastOwnTDMsgMux sync.Mutex lastChatTDMsg map[int64]time.Time lastChatTDMsgMux map[int64]*sync.Mutex lastChatMsgMux sync.RWMutex MQCWMsgQueue chan ChatWarsMessage MQTGCmdQueue chan TGCommand ) func main() { // Parsing config flag.Parse() err := gcfg.ReadFileInto(&cfg, *config) if err != nil { log.Fatalf("Failed to parse gcfg data: %s", err) } fmt.Printf("MQReceive on %s.\n", cfg.Rabbit.ReceiveQueue) fmt.Printf("MQSend on %s.\n", cfg.Rabbit.SendQueue) tdlib.SetLogVerbosityLevel(1) tdlib.SetFilePath("./errors.txt") // Create new instance of client sysv := fmt.Sprintf("%s (%s)", runtime.GOOS, runtime.GOARCH) client := tdlib.NewClient(tdlib.Config{ APIID: "187786", APIHash: "e782045df67ba48e441ccb105da8fc85", SystemLanguageCode: "en", DeviceModel: "ChatWarsClient", SystemVersion: sysv, 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) logOnError(err, "Error sending phone number.") } else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitCodeType { fmt.Print("Enter code: ") var code string fmt.Scanln(&code) _, err := client.SendAuthCode(code) logOnError(err, "Error sending auth code.") } else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPasswordType { fmt.Print("Enter Password: ") var password string fmt.Scanln(&password) _, err := client.SendAuthPassword(password) logOnError(err, "Error sending auth password.") } else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateReadyType { fmt.Println("Authorization Ready! Let's rock") break } time.Sleep(1 * time.Second) } ownUserID32 = OwnUserID(client) ownUserID64 = int64(OwnUserID(client)) lastChatTDMsg = make(map[int64]time.Time) lastChatTDMsgMux = make(map[int64]*sync.Mutex) MQCWMsgQueue = make(chan ChatWarsMessage, 100) for w := 1; w <= 3; w++ { go MQSendMsgWorker(w, MQCWMsgQueue) } if *history { fmt.Printf("Retrieving chat.\n") getHistory(client, historyChatID64, historySenderUserID64) return } MQTGCmdQueue = make(chan TGCommand, 100) for w := 1; w <= 3; w++ { go MQReceiveMsgWorker(w, MQTGCmdQueue) } go MQKeepAliveWorker() lastOwnTDMsgMux.Lock() lastOwnTDMsg = time.Now() lastOwnTDMsgMux.Unlock() go ListenTG(client) go ListenMQ(client, MQTGCmdQueue) go ListenMe(client) opt := tdlib.OptionValueBoolean{ Value: false, } _, err = client.SetOption("online", opt) logOnError(err, "SetOption(online)") fmt.Println("Started !") // Main loop forever := make(chan bool) <-forever }