2018-12-04 08:32:11 +04:00
|
|
|
// Fantasy World Zookeeper Helper Bot
|
|
|
|
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
tdlib "github.com/Arman92/go-tdlib"
|
2019-10-08 05:40:29 +04:00
|
|
|
"source.hodakov.me/fat0troll/fwzookeeper_helper/context"
|
|
|
|
"source.hodakov.me/fat0troll/fwzookeeper_helper/domains/announces/v1"
|
2018-12-04 08:32:11 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Authenticate connects instance to Telegram
|
|
|
|
// At first launch we need to login manually - there is no way to automate
|
|
|
|
// Telegram login
|
|
|
|
func Authenticate() {
|
2019-03-02 19:32:48 +04:00
|
|
|
if c.Config.TDLib.APIHash == "" {
|
|
|
|
log.Error().Msg("APIHash is empty")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.Config.TDLib.APIID == "" {
|
|
|
|
log.Error().Msg("APIID is empty")
|
|
|
|
return
|
|
|
|
}
|
2018-12-04 08:32:11 +04:00
|
|
|
tdlib.SetLogVerbosityLevel(1)
|
|
|
|
tdlib.SetFilePath(c.Config.TDLib.ErrorsFile)
|
|
|
|
|
|
|
|
// Create new instance of client
|
|
|
|
client = tdlib.NewClient(tdlib.Config{
|
|
|
|
APIID: c.Config.TDLib.APIID,
|
|
|
|
APIHash: c.Config.TDLib.APIHash,
|
|
|
|
SystemLanguageCode: "en",
|
|
|
|
DeviceModel: "fw_zookeeper_helper",
|
|
|
|
SystemVersion: context.VERSION,
|
|
|
|
ApplicationVersion: context.VERSION,
|
|
|
|
UseMessageDatabase: true,
|
|
|
|
UseFileDatabase: true,
|
|
|
|
UseChatInfoDatabase: true,
|
|
|
|
UseTestDataCenter: false,
|
|
|
|
DatabaseDirectory: c.Config.TDLib.DatabaseDirectory,
|
|
|
|
FileDirectory: c.Config.TDLib.FilesDirectory,
|
|
|
|
IgnoreFileNames: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
for {
|
|
|
|
currentState, _ := client.Authorize()
|
|
|
|
log.Info().Msg("Starting Telegram authorization...")
|
|
|
|
if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPhoneNumberType {
|
|
|
|
fmt.Print("Enter phone: ")
|
|
|
|
var number string
|
|
|
|
fmt.Scanln(&number)
|
|
|
|
_, err := client.SendPhoneNumber(number)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error sending phone number")
|
|
|
|
}
|
|
|
|
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitCodeType {
|
|
|
|
fmt.Print("Enter code: ")
|
|
|
|
var code string
|
|
|
|
fmt.Scanln(&code)
|
|
|
|
_, err := client.SendAuthCode(code)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error sending auth code")
|
|
|
|
}
|
|
|
|
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPasswordType {
|
|
|
|
fmt.Print("Enter Password: ")
|
|
|
|
var password string
|
|
|
|
fmt.Scanln(&password)
|
|
|
|
_, err := client.SendAuthPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error sending auth password")
|
|
|
|
}
|
|
|
|
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateReadyType {
|
|
|
|
me, _ := client.GetMe()
|
|
|
|
log.Info().Msgf("Logged into Telegram as @%s", me.Username)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect connects into updates chain
|
|
|
|
func Connect() {
|
|
|
|
go func() {
|
2019-01-08 04:18:13 +04:00
|
|
|
announcesv1.ZookeeperReceiver(client)
|
2018-12-04 08:32:11 +04:00
|
|
|
}()
|
|
|
|
rawUpdates := client.GetRawUpdatesChannel(100)
|
2019-01-08 04:18:13 +04:00
|
|
|
log.Debug().Msg("Connection with Telegram established")
|
2018-12-04 08:32:11 +04:00
|
|
|
for update := range rawUpdates {
|
|
|
|
log.Debug().Msgf("Update of type %s received", update.Data["@type"])
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 04:18:13 +04:00
|
|
|
|
|
|
|
// Shutdown disconnects from Telegram
|
|
|
|
func Shutdown() {
|
2019-01-09 03:24:12 +04:00
|
|
|
client.DestroyInstance()
|
2019-01-08 04:18:13 +04:00
|
|
|
log.Info().Msg("Connection with Telegram closed")
|
|
|
|
}
|