122 lines
4.0 KiB
Go
122 lines
4.0 KiB
Go
// 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"
|
|
"strings"
|
|
)
|
|
|
|
// 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() {
|
|
// Create an filter function which will be used to filter out unwanted tdlib messages
|
|
eventFilter := func(msg *tdlib.TdMessage) bool {
|
|
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
|
|
// We need only messages, created by @FWorldBot
|
|
return updateMsg.Message.ViaBotUserID == 6.74929718e+08
|
|
}
|
|
|
|
// Here we can add a receiver to retreive any message type we want
|
|
// We like to get UpdateNewMessage events and with a specific FilterFunc
|
|
receiver := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 5)
|
|
for newMsg := range receiver.Chan {
|
|
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
|
|
// Check if message text contains needed battle data
|
|
msgText := updateMsg.Message.Content.(*tdlib.MessageText)
|
|
if strings.HasPrefix(msgText.Text.Text, "Я встретил") {
|
|
battleType := ""
|
|
battleTag := ""
|
|
if strings.Contains(msgText.Text.Text, "Огров") {
|
|
battleType = "Огры!"
|
|
}
|
|
if strings.Contains(msgText.Text.Text, "Буйволов") {
|
|
battleType = "Буйволы!"
|
|
}
|
|
if strings.Contains(msgText.Text.Text, "Кабанов") {
|
|
battleType = "Кабаны!"
|
|
}
|
|
keyboard := updateMsg.Message.ReplyMarkup.(*tdlib.ReplyMarkupInlineKeyboard)
|
|
if len(keyboard.Rows) > 0 {
|
|
if len(keyboard.Rows[0]) > 0 {
|
|
button := keyboard.Rows[0][0]
|
|
buttonQuery := button.Type.(*tdlib.InlineKeyboardButtonTypeCallback)
|
|
battleTag = string(buttonQuery.Data)
|
|
}
|
|
}
|
|
log.Debug().Msgf("Battle type: %s", battleType)
|
|
log.Debug().Msgf("Battle type: %s", battleTag)
|
|
}
|
|
}
|
|
|
|
}()
|
|
|
|
rawUpdates := client.GetRawUpdatesChannel(100)
|
|
for update := range rawUpdates {
|
|
log.Debug().Msgf("Update of type %s received", update.Data["@type"])
|
|
}
|
|
}
|