Update dependencies
This commit is contained in:
9
internal/config/announce.go
Normal file
9
internal/config/announce.go
Normal file
@@ -0,0 +1,9 @@
|
||||
// Fantasy World Zookeeper Helper Bot
|
||||
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package config
|
||||
|
||||
// Announce is a struch which handles announces configuration
|
||||
type Announce struct {
|
||||
ChannelID int64 `yaml:"channel_id"`
|
||||
}
|
11
internal/config/struct.go
Normal file
11
internal/config/struct.go
Normal file
@@ -0,0 +1,11 @@
|
||||
// Fantasy World Zookeeper Helper Bot
|
||||
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package config
|
||||
|
||||
// Struct is a main configuration structure that holds all other
|
||||
// structs within.
|
||||
type Struct struct {
|
||||
TDLib TDLib `yaml:"tdlib"`
|
||||
Announce Announce `yaml:"announce"`
|
||||
}
|
14
internal/config/tdlib.go
Normal file
14
internal/config/tdlib.go
Normal file
@@ -0,0 +1,14 @@
|
||||
// Fantasy World Zookeeper Helper Bot
|
||||
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package config
|
||||
|
||||
// TDLib struct handles all configuration of tdlib
|
||||
// tdlib is an official Telegram MTProto library
|
||||
type TDLib struct {
|
||||
APIID string `yaml:"api_id"`
|
||||
APIHash string `yaml:"api_hash"`
|
||||
DatabaseDirectory string `yaml:"database_directory"`
|
||||
FilesDirectory string `yaml:"files_directory"`
|
||||
ErrorsFile string `yaml:"errors_file"`
|
||||
}
|
28
internal/telegram/exported.go
Normal file
28
internal/telegram/exported.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// Fantasy World Zookeeper Helper Bot
|
||||
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package telegram
|
||||
|
||||
import (
|
||||
tdlib "github.com/Arman92/go-tdlib"
|
||||
"github.com/rs/zerolog"
|
||||
"lab.wtfteam.pro/fat0troll/fw_zookeeper_helper/context"
|
||||
)
|
||||
|
||||
var (
|
||||
c *context.Context
|
||||
log zerolog.Logger
|
||||
|
||||
client *tdlib.Client
|
||||
)
|
||||
|
||||
// New initializes package
|
||||
func New(cc *context.Context) {
|
||||
c = cc
|
||||
log = c.Logger.With().Str("domain", "telegram").Int("version", 1).Logger()
|
||||
|
||||
log.Info().Msg("Starting Telegram MTProto instance")
|
||||
|
||||
Authenticate()
|
||||
Connect()
|
||||
}
|
88
internal/telegram/telegram.go
Normal file
88
internal/telegram/telegram.go
Normal file
@@ -0,0 +1,88 @@
|
||||
// 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"])
|
||||
}
|
||||
}
|
||||
|
||||
// Shutdown disconnects from Telegram
|
||||
func Shutdown() {
|
||||
client.DestroyInstance()
|
||||
log.Info().Msg("Connection with Telegram closed")
|
||||
}
|
Reference in New Issue
Block a user