// Fantasy World Zookeeper Helper Bot // Copyright (c) 2018 Vladimir "fat0troll" Hodakov package telegram import ( "fmt" tdlib "github.com/Arman92/go-tdlib" "lab.wtfteam.pro/fat0troll/fw_zookeeper_helper/context" "lab.wtfteam.pro/fat0troll/fw_zookeeper_helper/domains/announces/v1" ) // Authenticate connects instance to Telegram // At first launch we need to login manually - there is no way to automate // Telegram login func Authenticate() { 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() { announcesv1.ZookeeperReceiver(client) }() rawUpdates := client.GetRawUpdatesChannel(100) log.Debug().Msg("Connection with Telegram established") for update := range rawUpdates { log.Debug().Msgf("Update of type %s received", update.Data["@type"]) <-c.ShutdownDone return } } // Shutdown disconnects from Telegram func Shutdown() { client.DestroyInstance() log.Info().Msg("Connection with Telegram closed") }