Add working response to zookeeping request
This commit is contained in:
parent
647d9c548b
commit
66638518ea
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// VERSION is the current bot's version
|
// VERSION is the current bot's version
|
||||||
const VERSION = "0.0.1"
|
const VERSION = "0.0.2"
|
||||||
|
|
||||||
// Context is the main application context.
|
// Context is the main application context.
|
||||||
type Context struct {
|
type Context struct {
|
||||||
|
73
domains/announces/v1/announces.go
Normal file
73
domains/announces/v1/announces.go
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
// Fantasy World Zookeeper Helper Bot
|
||||||
|
// Copyright (c) 2018-2019 Vladimir "fat0troll" Hodakov
|
||||||
|
|
||||||
|
package announcesv1
|
||||||
|
|
||||||
|
import (
|
||||||
|
tdlib "github.com/Arman92/go-tdlib"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func fwMessagesFilter(msg *tdlib.TdMessage) bool {
|
||||||
|
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
|
||||||
|
// We need only messages, created by @FWorldBot
|
||||||
|
return updateMsg.Message.ViaBotUserID == 6.74929718e+08
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZookeeperReceiver adds announcement functionality to bot
|
||||||
|
func ZookeeperReceiver(client *tdlib.Client) {
|
||||||
|
log.Debug().Msg("Adding receiver for @FWorld_bot messages...")
|
||||||
|
// 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{}, fwMessagesFilter, 5)
|
||||||
|
log.Debug().Msg("Receiver added")
|
||||||
|
go func() {
|
||||||
|
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, "Я встретил") {
|
||||||
|
log.Debug().Msgf("%s", 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 = "Кабаны!"
|
||||||
|
}
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
log.Error().Msg("Invalid keyboard type")
|
||||||
|
}
|
||||||
|
log.Debug().Msgf("Battle type: %s", battleType)
|
||||||
|
log.Debug().Msgf("Battle tag: %s", battleTag)
|
||||||
|
|
||||||
|
replyText := battleType + " " + battleTag
|
||||||
|
|
||||||
|
reply := tdlib.NewInputMessageText(tdlib.NewFormattedText(replyText, nil), true, true)
|
||||||
|
_, err := client.SendMessage(updateMsg.Message.ChatID, 0, false, true, nil, reply)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
22
domains/announces/v1/exported.go
Normal file
22
domains/announces/v1/exported.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// Fantasy World Zookeeper Helper Bot
|
||||||
|
// Copyright (c) 2018-2019 Vladimir "fat0troll" Hodakov
|
||||||
|
|
||||||
|
package announcesv1
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
"lab.wtfteam.pro/fat0troll/fw_zookeeper_helper/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
c *context.Context
|
||||||
|
log zerolog.Logger
|
||||||
|
)
|
||||||
|
|
||||||
|
// New initializes package
|
||||||
|
func New(cc *context.Context) {
|
||||||
|
c = cc
|
||||||
|
log = c.Logger.With().Str("domain", "announces").Int("version", 1).Logger()
|
||||||
|
|
||||||
|
log.Info().Msg("Adding announces handler to Telegram instance")
|
||||||
|
}
|
@ -24,8 +24,5 @@ func New(cc *context.Context) {
|
|||||||
log.Info().Msg("Starting Telegram MTProto instance")
|
log.Info().Msg("Starting Telegram MTProto instance")
|
||||||
|
|
||||||
Authenticate()
|
Authenticate()
|
||||||
|
Connect()
|
||||||
go func() {
|
|
||||||
Connect()
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
tdlib "github.com/Arman92/go-tdlib"
|
tdlib "github.com/Arman92/go-tdlib"
|
||||||
"lab.wtfteam.pro/fat0troll/fw_zookeeper_helper/context"
|
"lab.wtfteam.pro/fat0troll/fw_zookeeper_helper/context"
|
||||||
"strings"
|
"lab.wtfteam.pro/fat0troll/fw_zookeeper_helper/domains/announces/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Authenticate connects instance to Telegram
|
// Authenticate connects instance to Telegram
|
||||||
@ -70,75 +70,19 @@ func Authenticate() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connect connects into updates chain
|
// Connect connects into updates chain
|
||||||
//
|
|
||||||
func Connect() {
|
func Connect() {
|
||||||
go func() {
|
go func() {
|
||||||
// Create an filter function which will be used to filter out unwanted tdlib messages
|
announcesv1.ZookeeperReceiver(client)
|
||||||
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, "Я встретил") {
|
|
||||||
log.Debug().Msgf("%s", 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 = "Кабаны!"
|
|
||||||
}
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
log.Error().Msg("Invalid keyboard type")
|
|
||||||
}
|
|
||||||
log.Debug().Msgf("Battle type: %s", battleType)
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
rawUpdates := client.GetRawUpdatesChannel(100)
|
rawUpdates := client.GetRawUpdatesChannel(100)
|
||||||
|
log.Debug().Msg("Connection with Telegram established")
|
||||||
for update := range rawUpdates {
|
for update := range rawUpdates {
|
||||||
log.Debug().Msgf("Update of type %s received", update.Data["@type"])
|
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")
|
||||||
|
}
|
||||||
|
9
main.go
9
main.go
@ -5,6 +5,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"lab.wtfteam.pro/fat0troll/fw_zookeeper_helper/context"
|
"lab.wtfteam.pro/fat0troll/fw_zookeeper_helper/context"
|
||||||
|
"lab.wtfteam.pro/fat0troll/fw_zookeeper_helper/domains/announces/v1"
|
||||||
"lab.wtfteam.pro/fat0troll/fw_zookeeper_helper/local/telegram"
|
"lab.wtfteam.pro/fat0troll/fw_zookeeper_helper/local/telegram"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@ -23,8 +24,6 @@ func main() {
|
|||||||
c.Init()
|
c.Init()
|
||||||
c.InitConfiguration()
|
c.InitConfiguration()
|
||||||
|
|
||||||
telegram.New(c)
|
|
||||||
|
|
||||||
// CTRL+C handler.
|
// CTRL+C handler.
|
||||||
interrupt := make(chan os.Signal, 1)
|
interrupt := make(chan os.Signal, 1)
|
||||||
signal.Notify(interrupt)
|
signal.Notify(interrupt)
|
||||||
@ -33,10 +32,16 @@ func main() {
|
|||||||
signalThing := <-interrupt
|
signalThing := <-interrupt
|
||||||
if signalThing == syscall.SIGTERM || signalThing == syscall.SIGINT {
|
if signalThing == syscall.SIGTERM || signalThing == syscall.SIGINT {
|
||||||
c.Logger.Info().Msg("Got " + signalThing.String() + " signal, shutting down...")
|
c.Logger.Info().Msg("Got " + signalThing.String() + " signal, shutting down...")
|
||||||
|
|
||||||
|
telegram.Shutdown()
|
||||||
|
|
||||||
shutdownDone <- true
|
shutdownDone <- true
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
announcesv1.New(c)
|
||||||
|
telegram.New(c)
|
||||||
|
|
||||||
<-shutdownDone
|
<-shutdownDone
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user