gocw2/main.go
2019-05-17 14:22:34 +08:00

147 lines
3.5 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"time"
"gopkg.in/gcfg.v1"
"github.com/Arman92/go-tdlib"
)
const (
user_chtwrsbot = 408101137
chat_darkwing = -1001080526540
chat_war = -1001108112459
chat_war_mini = -1001277259728
chat_auction = -1001209424945
)
type Config struct {
Rabbit struct {
User string
Password string
Host string
SendQueue string
ReceiveQueue string
}
Listen struct {
War bool
WarMini bool
Auctions bool
}
}
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
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
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))
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()
lastOwnTDMsg = time.Now()
go ListenCW(client)
go ListenMQ(client, MQTGCmdQueue)
go ListenMe(client)
fmt.Println("Started !")
// Main loop
forever := make(chan bool)
<-forever
}