gocw2/main.go

151 lines
3.8 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-12-13 03:53:47 +01:00
"runtime"
2019-08-24 07:58:58 +02:00
"sync"
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"
)
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-10-12 07:26:11 +02:00
Listen map[string]*struct {
User int64
Chat int64
2019-05-15 04:18:16 +02:00
}
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
ownUserID64 = int64(0)
ownUserID32 = int32(0)
lastOwnTDMsg time.Time
lastOwnTDMsgMux sync.Mutex
2019-11-05 03:36:16 +01:00
lastChatTDMsg map[int64]time.Time
2019-11-05 03:37:42 +01:00
lastChatTDMsgMux map[int64]*sync.Mutex
lastChatMsgMux sync.RWMutex
MQCWMsgQueue chan ChatWarsMessage
MQTGCmdQueue chan TGCommand
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-15 05:27:09 +02:00
fmt.Printf("MQReceive on %s.\n", cfg.Rabbit.ReceiveQueue)
fmt.Printf("MQSend on %s.\n", cfg.Rabbit.SendQueue)
2019-05-03 06:08:15 +02:00
tdlib.SetLogVerbosityLevel(1)
tdlib.SetFilePath("./errors.txt")
// Create new instance of client
2019-12-13 03:53:47 +01:00
sysv := fmt.Sprintf("%s (%s)", runtime.GOOS, runtime.GOARCH)
2019-05-03 06:08:15 +02:00
client := tdlib.NewClient(tdlib.Config{
APIID: "187786",
APIHash: "e782045df67ba48e441ccb105da8fc85",
SystemLanguageCode: "en",
DeviceModel: "ChatWarsClient",
2019-12-13 03:53:47 +01:00
SystemVersion: sysv,
2019-05-03 06:08:15 +02:00
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)
2019-08-30 11:01:25 +02:00
logOnError(err, "Error sending phone number.")
2019-05-03 06:08:15 +02:00
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitCodeType {
fmt.Print("Enter code: ")
var code string
fmt.Scanln(&code)
_, err := client.SendAuthCode(code)
2019-08-30 11:01:25 +02:00
logOnError(err, "Error sending auth code.")
2019-05-03 06:08:15 +02:00
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPasswordType {
fmt.Print("Enter Password: ")
var password string
fmt.Scanln(&password)
_, err := client.SendAuthPassword(password)
2019-08-30 11:01:25 +02:00
logOnError(err, "Error sending auth password.")
2019-05-03 06:08:15 +02:00
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateReadyType {
fmt.Println("Authorization Ready! Let's rock")
break
}
2019-08-30 10:56:46 +02:00
time.Sleep(1 * time.Second)
2019-05-03 06:08:15 +02:00
}
ownUserID32 = OwnUserID(client)
ownUserID64 = int64(OwnUserID(client))
2019-11-05 03:43:06 +01:00
lastChatTDMsg = make(map[int64]time.Time)
lastChatTDMsgMux = make(map[int64]*sync.Mutex)
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-16 04:07:25 +02:00
MQTGCmdQueue = make(chan TGCommand, 100)
2019-05-14 11:34:15 +02:00
for w := 1; w <= 3; w++ {
2019-05-16 04:07:25 +02:00
go MQReceiveMsgWorker(w, MQTGCmdQueue)
2019-05-14 11:34:15 +02:00
}
2019-05-17 08:22:34 +02:00
go MQKeepAliveWorker()
2019-05-14 11:34:15 +02:00
2019-08-24 07:58:58 +02:00
lastOwnTDMsgMux.Lock()
2019-05-15 04:18:16 +02:00
lastOwnTDMsg = time.Now()
2019-08-24 07:58:58 +02:00
lastOwnTDMsgMux.Unlock()
2019-10-12 07:26:11 +02:00
go ListenTG(client)
2019-05-16 04:07:25 +02:00
go ListenMQ(client, MQTGCmdQueue)
2019-05-15 04:18:16 +02:00
go ListenMe(client)
2019-12-13 08:32:04 +01:00
opt := td.OptionValueBoolean
opt.Value := false
_, err := client.SetOption("online", opt)
logOnError(err, "SetOption(online)")
2019-05-15 04:18:16 +02:00
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
}