Archived
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.
i2_bot/lib/orders/orders.go

119 lines
2.9 KiB
Go
Raw Normal View History

// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package orders
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
"lab.pztrn.name/fat0troll/i2_bot/lib/dbmapping"
"strconv"
"strings"
)
// Internal functions
func (o *Orders) getOrderByID(orderID int) (dbmapping.Order, bool) {
order := dbmapping.Order{}
err := c.Db.Get(&order, c.Db.Rebind("SELECT * FROM orders WHERE id=?"), orderID)
if err != nil {
c.Log.Error(err.Error())
return order, false
}
return order, true
}
func (o *Orders) sendOrder(order *dbmapping.Order) string {
targetChats := []dbmapping.Chat{}
ok := false
if order.TargetSquads == "all" {
targetChats, ok = c.Squader.GetAllSquadChats()
if !ok {
return "fail"
}
2017-11-26 17:44:41 +04:00
// Adding Bastion chat as it's the zero chat
bastionGroupID, _ := strconv.ParseInt(c.Cfg.SpecialChats.BastionID, 10, 64)
bastionChat := dbmapping.Chat{}
err := c.Db.Get(&bastionChat, c.Db.Rebind("SELECT * FROM chats WHERE telegram_id=?"), bastionGroupID)
if err != nil {
return "fail"
}
targetChats = append(targetChats, bastionChat)
} else {
targetChats, ok = c.Squader.GetSquadChatsBySquadsIDs(order.TargetSquads)
if !ok {
return "fail"
}
2017-11-26 17:44:41 +04:00
targetChatsIDs := strings.Split(order.TargetSquads, ",")
for i := range targetChatsIDs {
if targetChatsIDs[i] == "0" {
bastionGroupID, _ := strconv.ParseInt(c.Cfg.SpecialChats.BastionID, 10, 64)
bastionChat := dbmapping.Chat{}
err := c.Db.Get(&bastionChat, c.Db.Rebind("SELECT * FROM chats WHERE telegram_id=?"), bastionGroupID)
if err != nil {
return "fail"
}
targetChats = append(targetChats, bastionChat)
}
}
}
for i := range targetChats {
message := "Поступил приказ:"
msg := tgbotapi.NewMessage(targetChats[i].TelegramID, message)
keyboard := tgbotapi.InlineKeyboardMarkup{}
var row []tgbotapi.InlineKeyboardButton
btn := tgbotapi.NewInlineKeyboardButtonSwitch("В атаку!", strconv.Itoa(order.ID))
row = append(row, btn)
keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, row)
msg.ReplyMarkup = keyboard
msg.ParseMode = "Markdown"
pinnableMessage, err := c.Bot.Send(msg)
if err != nil {
c.Log.Error(err.Error())
} else {
pinChatMessageConfig := tgbotapi.PinChatMessageConfig{
ChatID: pinnableMessage.Chat.ID,
MessageID: pinnableMessage.MessageID,
DisableNotification: true,
}
_, err = c.Bot.PinChatMessage(pinChatMessageConfig)
if err != nil {
c.Log.Error(err.Error())
}
}
}
return "ok"
}
// External functions
// SendOrder sends order to selected or all squads
func (o *Orders) SendOrder(update *tgbotapi.Update) string {
command := update.Message.Command()
orderNumber := strings.TrimPrefix(command, "send_order")
orderID, _ := strconv.Atoi(orderNumber)
if orderID == 0 {
return "fail"
}
order, ok := o.getOrderByID(orderID)
if !ok {
return "fail"
}
return o.sendOrder(&order)
}