gocw2/main.go

171 lines
4.1 KiB
Go
Raw Normal View History

2019-05-03 06:08:15 +02:00
package main
import (
2019-05-03 06:40:04 +02:00
"flag"
2019-05-03 06:08:15 +02:00
"fmt"
"log"
2019-05-09 12:31:35 +02:00
"time"
2019-05-03 06:08:15 +02:00
2019-05-03 06:40:04 +02:00
"gopkg.in/gcfg.v1"
2019-05-03 06:08:15 +02:00
"github.com/Arman92/go-tdlib"
)
type ChatWarsMessage struct {
2019-05-09 12:31:35 +02:00
UserID64 int64 `json:"user_id"`
SenderUserID64 int64 `json:"sender_user_id"`
Date time.Time `json:"date"`
ID64 int64 `json:"id"`
ChatID64 int64 `json:"chat_id"`
Text string `json:"text"`
2019-05-03 06:08:15 +02:00
}
2019-05-14 11:34:15 +02:00
type ChatWarsCommand struct {
Type int64 `json:"type"`
FromChatID64 int64 `json:"from_chat_id"`
FromUserID64 int64 `json:"from_user_id"`
FromMsgID64 int64 `json:"from_msg_id"`
ToChatID64 int64 `json:"to_chat_id"`
ToUserID64 int64 `json:"to_user_id"`
Text int64 `json:"text"`
}
const (
commandForwardMsg = 1
commandReplyMsg = 2
commandSendMsg = 3
commandDeleteMsg = 4
commandRefreshMsg = 5
)
const (
user_chtwrsbot = 408101137
chat_darkwing = -1001080526540
chat_war = -1001108112459
chat_war_mini = -1001277259728
chat_auction = -1001209424945
)
2019-05-03 06:08:15 +02:00
2019-05-03 06:39:16 +02:00
type Config struct {
Rabbit struct {
2019-05-14 11:34:15 +02:00
User string
Password string
Host string
SendQueue string
ReceiveQueue string
2019-05-03 06:39:16 +02:00
}
2019-05-15 04:18:16 +02:00
Listen struct {
War bool
WarMini bool
Auctions bool
}
2019-05-03 06:39:16 +02:00
}
var (
2019-05-04 11:55:28 +02:00
config = flag.String("config", "gocw2.cfg", "config file path")
history = flag.Bool("history", false, "initialize chat history")
2019-05-04 11:57:45 +02:00
historyChatID64 = flag.Int64("chat_id", 0, "chat to historize")
historySenderUserID64 = flag.Int64("sender_user_id", 0, "sender_user_id to historize")
2019-05-03 06:39:16 +02:00
cfg Config
2019-05-04 05:53:35 +02:00
ownUserID64 = int64(0)
ownUserID32 = int32(0)
2019-05-15 04:19:43 +02:00
lastOwnTDMsg time.Time
2019-05-04 05:53:35 +02:00
MQCWMsgQueue chan ChatWarsMessage
2019-05-14 11:34:15 +02:00
MQCWCmdQueue chan ChatWarsCommand
2019-05-03 06:39:16 +02:00
)
2019-05-03 06:08:15 +02:00
func main() {
2019-05-03 06:39:16 +02:00
// Parsing config
flag.Parse()
2019-05-03 06:40:43 +02:00
err := gcfg.ReadFileInto(&cfg, *config)
2019-05-03 06:39:16 +02:00
if err != nil {
log.Fatalf("Failed to parse gcfg data: %s", err)
}
2019-05-03 06:08:15 +02:00
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))
2019-05-04 05:53:35 +02:00
MQCWMsgQueue = make(chan ChatWarsMessage, 100)
for w := 1; w <= 3; w++ {
go MQSendMsgWorker(w, MQCWMsgQueue)
}
2019-05-03 11:19:20 +02:00
if *history {
2019-05-03 18:03:05 +02:00
fmt.Printf("Retrieving chat.\n")
2019-05-04 11:55:28 +02:00
getHistory(client, historyChatID64, historySenderUserID64)
2019-05-04 06:50:04 +02:00
return
2019-05-03 11:19:20 +02:00
}
2019-05-14 11:34:15 +02:00
MQCWCmdQueue = make(chan ChatWarsCommand, 100)
for w := 1; w <= 3; w++ {
go MQReceiveMsgWorker(w, MQCWCmdQueue)
}
2019-05-15 04:18:16 +02:00
lastOwnTDMsg = time.Now()
2019-05-03 18:03:05 +02:00
go ListenCW(client)
2019-05-15 04:18:16 +02:00
go ListenMQ(client, MQCWCmdQueue)
go ListenMe(client)
2019-05-04 06:50:04 +02:00
fmt.Println("Started !")
2019-05-03 06:08:15 +02:00
2019-05-04 06:30:02 +02:00
// Main loop
2019-05-04 05:53:35 +02:00
forever := make(chan bool)
<-forever
2019-05-03 06:08:15 +02:00
}