gocw2/main.go

130 lines
2.8 KiB
Go
Raw Normal View History

2019-05-03 06:08:15 +02:00
package main
import (
"encoding/json"
2019-05-03 06:40:04 +02:00
"flag"
2019-05-03 06:08:15 +02:00
"fmt"
"log"
"time"
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"
"github.com/streadway/amqp"
)
2019-05-03 09:24:09 +02:00
type MsgDirection string
2019-05-03 06:08:15 +02:00
const (
2019-05-03 10:05:43 +02:00
Incoming MsgDirection = `incoming`
Outgoing MsgDirection = `outgoing`
2019-05-03 06:08:15 +02:00
)
type ChatWarsMessage struct {
2019-05-03 10:00:39 +02:00
MsgID64 int64 `json:"msg_id"`
UserID64 int64 `json:"user_id"`
Direction MsgDirection `json:"direction"`
MsgText string `json:"msg"`
MsgDate int32 `json:"date"`
2019-05-03 06:08:15 +02:00
}
const user_chtwrsbot = 408101137
2019-05-03 06:39:16 +02:00
type Config struct {
Rabbit struct {
User string
Password string
Host string
Queue string
}
}
var (
2019-05-03 11:19:20 +02:00
config = flag.String("config", "gocw2.cfg", "config file path")
history = flag.Bool("history", false, "initialize chat history")
2019-05-03 06:39:16 +02:00
cfg Config
ownUserID64 = int64(0)
ownUserID32 = int32(0)
)
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-03 11:19:20 +02:00
if *history {
getHistory(client)
}
2019-05-03 06:08:15 +02:00
go ListenCW(client)
fmt.Println("Started !")
// Main loop
for {
time.Sleep(30 * time.Second)
}
}