2019-01-08 04:18:13 +04:00
|
|
|
// 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) {
|
2019-01-09 03:24:12 +04:00
|
|
|
// Where to send announces
|
|
|
|
announcerID := int64(665790161)
|
2019-01-08 06:51:36 +04:00
|
|
|
log.Debug().Msg("Adding receiver for @FWorldBot messages...")
|
2019-01-08 04:18:13 +04:00
|
|
|
// 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")
|
2019-01-08 06:51:36 +04:00
|
|
|
|
2019-01-10 05:36:06 +04:00
|
|
|
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")
|
|
|
|
}
|
2019-01-08 04:18:13 +04:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 05:36:06 +04:00
|
|
|
default:
|
|
|
|
log.Error().Msg("Invalid keyboard type")
|
2019-01-08 04:18:13 +04:00
|
|
|
}
|
2019-01-10 05:36:06 +04:00
|
|
|
log.Debug().Msgf("Battle type: %s", battleType)
|
|
|
|
log.Debug().Msgf("Battle tag: %s", battleTag)
|
2019-01-08 04:18:13 +04:00
|
|
|
|
2019-01-10 05:36:06 +04:00
|
|
|
replyText := battleType + " " + battleTag
|
2019-01-08 04:18:13 +04:00
|
|
|
|
2019-01-10 05:36:06 +04:00
|
|
|
reply := tdlib.NewInputMessageText(tdlib.NewFormattedText(replyText, nil), true, true)
|
|
|
|
_, err := client.SendMessage(announcerID, 0, false, true, nil, reply)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err)
|
|
|
|
}
|
2019-01-08 04:18:13 +04:00
|
|
|
}
|
2019-01-09 15:48:34 +04:00
|
|
|
}
|
2019-01-10 05:36:06 +04:00
|
|
|
}()
|
2019-01-08 04:18:13 +04:00
|
|
|
}
|