vegapokerbot/internal/handler/group/poll.go
Neur0toxine 1e6022df08
All checks were successful
continuous-integration/drone/push Build is passing
vote for the task via reply
2024-05-23 15:30:10 +03:00

113 lines
3.1 KiB
Go

package group
import (
"fmt"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/model"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/store"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/util"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/integration"
"github.com/mymmrac/telego"
tu "github.com/mymmrac/telego/telegoutil"
"golang.org/x/text/language"
"strings"
)
type Poll struct {
iface.Base
}
func NewPoll(app iface.App, userID, chatID int64) *Poll {
return &Poll{iface.NewBase(app, userID, chatID)}
}
func (h *Poll) Handle(wh telego.Update) error {
if wh.Message.Chat.Type == telego.ChatTypePrivate {
_, err := h.App.TG().SendMessage(&telego.SendMessageParams{
ChatID: tu.ID(wh.Message.Chat.ID),
Text: h.Localizer(wh.Message.From.LanguageCode).Message("use_this_command_in_group"),
ParseMode: telego.ModeMarkdown,
})
return err
}
chat, err := h.App.DB().ForChat().ByTelegramIDWithIntegrations(wh.Message.Chat.ID)
if err != nil {
_ = util.SendInternalError(h.App.TG(), wh.Message.Chat.ID, h.Localizer(language.English.String()))
return err
}
user, err := h.App.DB().ForUser().ByID(chat.UserID)
if err != nil {
_ = util.SendInternalError(h.App.TG(), wh.Message.Chat.ID, h.Localizer(language.English.String()))
return err
}
loc := h.Localizer(user.Language)
if len(wh.Message.Entities) == 0 ||
(len(wh.Message.Entities) > 0 && wh.Message.Entities[0].Type != telego.EntityTypeBotCommand) ||
(len(wh.Message.Entities) > 0 && wh.Message.Entities[0].Offset != 0) {
return nil
}
taskInfo := strings.TrimSpace(wh.Message.Text[wh.Message.Entities[0].Length:])
if taskInfo == "" && wh.Message.ReplyToMessage != nil {
taskInfo = wh.Message.ReplyToMessage.Text
}
var (
taskID int
canRedmine bool
)
if taskInfo != "" {
for _, integrationData := range chat.Integrations {
id, info := integration.New(integrationData, h.App.Log()).GetTaskInfo(taskInfo)
if id > 0 {
taskID = id
taskInfo = info
canRedmine = integrationData.Type == model.RedmineIntegration
break
}
}
}
dotOrColon := "."
if taskInfo != "" {
dotOrColon = ": "
}
var kb telego.ReplyMarkup
switch chat.KeyboardType {
case model.StandardKeyboard:
kb = util.StandardVoteKeyboard(wh.Message.Chat.ID, loc)
case model.StoryPointsKeyboard:
kb = util.StoryPointsVoteKeyboard(wh.Message.Chat.ID, loc)
}
msg, err := h.App.TG().SendMessage(&telego.SendMessageParams{
ChatID: tu.ID(wh.Message.Chat.ID),
Text: fmt.Sprintf("%s\n%s",
loc.Template("poker_start", map[string]interface{}{
"Name": wh.Message.From.Username,
"DotOrColon": dotOrColon,
"Subject": taskInfo,
}),
loc.Message("no_votes_yet"),
),
ReplyMarkup: kb,
ParseMode: telego.ModeMarkdown,
})
if err != nil {
return err
}
store.Polls.Set(msg.MessageID, store.PollState{
Initiator: wh.Message.From.Username,
Subject: taskInfo,
TaskID: taskID,
Votes: []store.MemberVote{},
Result: store.PollResult{},
CanRedmine: canRedmine,
})
return nil
}