1
This repository has been archived on 2022-11-04. You can view files and clone it, but cannot push or open issues or pull requests.
fwzookeeper_helper/local/telegram/telegram.go

145 lines
4.6 KiB
Go
Raw Normal View History

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"
"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, "Я встретил") {
2018-12-22 18:03:53 +04:00
log.Debug().Msgf("%s", msgText.Text.Text)
2018-12-04 08:32:11 +04:00
battleType := ""
battleTag := ""
if strings.Contains(msgText.Text.Text, "Огров") {
battleType = "Огры!"
}
if strings.Contains(msgText.Text.Text, "Буйволов") {
battleType = "Буйволы!"
}
if strings.Contains(msgText.Text.Text, "Кабанов") {
battleType = "Кабаны!"
}
2018-12-22 18:03:53 +04:00
switch updateMsg.Message.ReplyMarkup.(type) {
case *tdlib.ReplyMarkupInlineKeyboard:
keyboard := updateMsg.Message.ReplyMarkup.(*tdlib.ReplyMarkupInlineKeyboard)
if len(keyboard.Rows) > 0 {
if len(keyboard.Rows[0]) > 0 {
button := keyboard.Rows[0][0]
switch button.Type.(type) {
case *tdlib.InlineKeyboardButtonTypeSwitchInline:
buttonQuery := button.Type.(*tdlib.InlineKeyboardButtonTypeSwitchInline)
battleTag = string(buttonQuery.Query)
default:
log.Error().Msg("Invalid button type")
}
}
2018-12-04 08:32:11 +04:00
}
2018-12-22 18:03:53 +04:00
default:
log.Error().Msg("Invalid keyboard type")
2018-12-04 08:32:11 +04:00
}
log.Debug().Msgf("Battle type: %s", battleType)
2018-12-22 18:03:53 +04:00
log.Debug().Msgf("Battle tag: %s", battleTag)
reply := tdlib.InputMessageText{
DisableWebPagePreview: true,
Text: &tdlib.FormattedText{
Text: "Вызываю на помощь!",
},
}
_, err := client.SendMessage(updateMsg.Message.ChatID, 0, false, false, nil, &reply)
if err != nil {
log.Error().Err(err)
}
2018-12-04 08:32:11 +04:00
}
}
}()
rawUpdates := client.GetRawUpdatesChannel(100)
for update := range rawUpdates {
log.Debug().Msgf("Update of type %s received", update.Data["@type"])
}
}